Forum rules - please read before posting.

How to iterate through the array of AC variables?

edited November 2021 in Technical Q&A

Hello fellow AC users! It's been awhile :)
I have been working on my next game just by using AC scripting guide and getting better and better at it. Fantastic tool.
But I need a helping hand on this one.

I have 100 level Ui unity icons and you can skip levels in the game but if some of them are won I want to set a wining cup image visible over those level icons that got unlocked.
I have an array of 100 cups images. And 100 global LevelWon AC variables, like 19: LevelWon/Level01, 20:LevelWon/Level02 , 21:LevelWon/Level03 and so on.

So how do I iterate through AC variables? How do I declare an array of AC variables to go through?
I need to go through the variables and if the level is won, go through the array Cup icons and set visible the ones corresponding to the specific variable .

Below is the modification of a script I made to work on unlocking level icons and now trying to use it for unlocking specific Cup icons over levels buttons that have been won.

namespace AC { }
public class LevelSelection : MonoBehaviour
{
public GameObject[] CupIcons;

void Start()
    {

    }

    void UnlockCupIcons()
    {
        int levelAt = AC.GlobalVariables.GetIntegerValue(3); // Need to change this to some how declare AC variable array instead to iterate through

    for (int i = 0; i < lvlButtons.Length; i++)
        {

            if (i + 0 <= levelAt)
            {
                CupIcons[i].SetActive(true);

            //Debug.Log("LevelNow" + levelPick);

             }
        }
    }
}

The closest about how to iterate through the variables I could fine on the forums is this https://adventurecreator.org/forum/discussion/9820/using-script-to-get-a-list-of-objectives

Using:
AC v1.73.3
Unity 2021.16f1

Comments

  • As your variables all rely on a shared naming convention, it may be possible to instead iterate through CupIcons instead and work out its associated variable from its index number. Something like:

    void UnlockCupIcons()
    {
        for (int i = 0; i < CupIcons.Length; i++)
        {
            int variableNameIndex = i + 1; // Start from LevelWon/Level01, not LevelWon/Level00
            string variableName = "LevelWon/Level" + variableNameIndex.ToString ("00"); // Convert e.g. 1 to 01, 2 to 02 etc
            AC.GVar variable = AC.GlobalVariables.GetVariable (variableName);
            int variableValue = variable.IntegerValue;
        }
    }
    

    If you were interested in taking things a little further in complexity, it would be possible to condense these values into a single String variable that represents all level variable values in one. It would be tricky to combine these values into a string when saving, and extract them again when loading, but it would be feasible.

  • Marvelous way to go through AC variables! Thank you Chris :)

    And just for those who might wanna use it, here is the complete function-method with check condition.

    void UnlockCupIcons()
    {
        for (int i = 0; i < CupIcons.Length; i++)
        {
            int variableNameIndex = i + 1; // Start from LevelWon/Level01, not  LevelWon/Level00
            string variableName = "LevelWon/Level" + variableNameIndex.ToString("00"); // Convert e.g. 1 to 01, 2 to 02 etc
            AC.GVar variable = AC.GlobalVariables.GetVariable(variableName);
            int variableValue = variable.IntegerValue;
    
                if (0 != variableValue)
                 {
                   CupIcons[i].SetActive(true);
                   Debug.Log("CupWon" + variableNameIndex);
                }
        }
    }
    
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.