Forum rules - please read before posting.

Variable at solving puzzle

edited March 2021 in Technical Q&A

Ok, I feel very dump asking this, but i have tried all changes possible and I don't get the correct answer :'( . I have follow this tutorial for make a simple puzzle done in my game, since the other ones prefab doesn't work correctly. First of all, is there a way to make this possible with just AC? if not, I make it it work, but I'm not getting to make the code to accept my variable. I have read the manual and don't get a sample of this code :'(

Turorial I follow.

If you see the video the code is simple, it only makes than when it get all the pieces rotate in the correct angle it pop ups a WinText, in this case I change WinText with my variable for using with my ActionList.

This are the codes I'm using:

This one for the pieces making move:

public class TouchRotate : MonoBehaviour {

private void OnMouseDown()
{
    if (!GameControl.youWin)
        transform.Rotate(0f, 0f, 90f);
}

}

And this one for adding the variable, I change the win text to my own variable, but Im totally lost how to make it work :(

public class GameControl : MonoBehaviour
{
[SerializeField]
private Transform[] pictures;

public static bool youWin;

GVar PuzzleSolved;

// Start is called before the first frame update

void Start()
{

    youWin = false;
    PuzzleSolved = AC.GlobalVariables.GetVariable(2);
    PuzzleSolved.BooleanValue = false;
}

// Update is called once per frame
void Update()
{
    if (pictures[0].rotation.z == 0 &&
       pictures[1].rotation.z == 0 &&
       pictures[2].rotation.z == 0 &&
       pictures[3].rotation.z == 0 &&
       pictures[4].rotation.z == 0 &&
       pictures[5].rotation.z == 0 &&
       pictures[6].rotation.z == 0 &&
       pictures[7].rotation.z == 0 &&
       pictures[8].rotation.z == 0)
    {
        youWin = true;
        PuzzleSolved.BooleanValue = true;

    }
}

}

Here some screenshots:

Any help?

Comments

  • You could do it with AC ActionLists, but it'd be a bit of a spider-web of Actions. If you already have the code to do it, better to just use that.

    I'm not clear on your actual problem though - what actually happens / doesn't happen that's causing an issue? Are you getting an error message?

    You don't need to set the AC Global Variable's "Link to" field to "Custom Script" just to be able to access it - that feature is more for automating its value with another system. If you're just getting/setting its value at specific times in your script, set it back to "None".

    I'd also recommend only checking for the win state if youWin is False:

    void Update ()
    {
        if (!youWin &&
            pictures[0].rotation.z == 0 &&
            pictures[1].rotation.z == 0 &&
            pictures[2].rotation.z == 0 &&
            pictures[3].rotation.z == 0 &&
            pictures[4].rotation.z == 0 &&
            pictures[5].rotation.z == 0 &&
            pictures[6].rotation.z == 0 &&
            pictures[7].rotation.z == 0 &&
            pictures[8].rotation.z == 0)
        {
            youWin = true;
            PuzzleSolved.BooleanValue = true;
        }
    }
    

    This way, you'll only be updating the variable once, as opposed to every frame.

  • The only issue is that I can't make the variable work. It just pop up as it is begin as true. I really get lost on this :S.

    Screenshot of code because I cant paste it correctly.

    This is my Action List, maybe Im doing somehow wrong? When I get play it got directly to Done! Text

  • I tested it with a text popping up as the original tutorial and it worked but when I try to put a AC variable didn't read it.

  • Have you now cleared the variable's "Link to" property, and is its "Initial value" still set to "False"?

    If so, then it's being set to True either by a "Variable: Set" Action, or by the Update function in your script. If the latter, then it's an issue with the way you're checking the win-state condition. The way you're getting/setting the AC variable is fine.

    To confirm how/when the variable is set by the script, use a Debug.Log statement at the same time you set the variable to True:

    Debug.Log ("Setting variable to true");
    

    Does this appear in the Console prematurely?

  • Ok, I have checked and i don't know why this is not working :(. I had checked all the elements are fine. The ActionList is using the PuzzleSolved Variable, my boolean variable is setting to False, and my log is showing the "Setting variable to true" legend.



    Also I added some notes on my code and call again the Variable at the void Update:

    The code is working fine, the only thing i can't make it work is the AC variable :'(

  • The code does indeed look correct.

    How and when are you running the ActionList? Are you setting the variable's value by any other means?

    Make sure that you have Show realtime values? checked in the Variables Manager - is the variable updating in the Manager window?

  • I think I might have run into something like this before. Because I'm not a very good coder, I just assumed I wanted the code to do something it wasn't supposed to do.

    PuzzleSolved = true;
    AC.GlobalVariables.SetBooleanValue(2, PuzzleSolved);
    

    This seemed to do the job for me.

  • Both should be viable approaches, but it's worth trying.

  • I tryed the @Rairun code but it show me this warning:

    Assets\RikasAdventures\Sprites\Puzzles\GameControl.cs(45,51): error CS1503: Argument 2: cannot convert from 'AC.GVar' to 'bool'

    I-m running this action as a **OnStart **Scene Action List. The code I'm using is fine, when I rotate all the image correctly I get the:

    Setting variable to true
    UnityEngine.Debug:Log (object)

    But I'm not making work the Action List, is the only thing I can't connect to the code. My variable is a Global Variable (I want to use it for other puzzles in the future). Or do i need to do a local one?

  • Also, here my variable manager inspector with the Variable I'm using.

    I checked up the Var Preset "Update Runtime" but nothing happen :(

  • Try this single line of code instead of the two lines Rairun suggested:

    AC.GlobalVariables.SetBooleanValue(2, true);
    

    Aside from the custom script, this is still otherwise an AC scene, with all of its systems enabled?

    It does sound like there's some other element at play - the code above should certainly work, and does for me, without issue.

    Let's see if we can use the OnVariableChange event to diagnose the issue. Create a new C# script named VariableEventTest, paste in the code below, and attach to an empty GameObject in your scene:

    using UnityEngine;
    using AC;
    
    public class VariableEventTest : MonoBehaviour
    {
    
        private void OnEnable () { EventManager.OnVariableChange += OnVariableChange; }
        private void OnDisable () { EventManager.OnVariableChange -= OnVariableChange; }
    
        private void OnVariableChange (GVar variable)
        {
            if (variable.label == "PuzzleSolved")
            {
                Debug.Log ("Updated " + variable.label + " value to " + variable.BooleanValue);
            }
        }
    }
    

    That will print a message in the Console any time the "PuzzleSolved" variable is modified. What gets shown exactly in the Console?

  • Ok, I changed the code with the line you told me:

    Also added the new script and this is what console said to me:

  • edited April 2021

    That suggests all is working - your code is being run, and the AC variable is being updated.

    Make sure that Show runtime values? is checked in your Variables, and then share a screenshot of your variable in the Manager once the Console has been updated in the above way.

    How and when are you determining that the Variable is False? What happens if you click Run now in the Inspector of the ActionList you've shared a screenshot of, after the above messages have been displayed?

  • Ok, I see the issue now. The problem is that variable begin as False, so when it Check the Variable it read directly False and stop running. So when I solve the puzzle it get to true but the Action List has already stop running.

    So i do that if variable not met it would repeat again, so finally it working doing this. Sorry for the issue, I didn't realize that the ActionList stop working when it set to false and i needed to make it read it again until is solved.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.