Forum rules - please read before posting.

How to access Global Variables in Editor mode

Hello! Today I come with a rather specific request, I asked it on the discord and was encouraged to post this on the forum.

I have a scriptable object that I want to access the list of Global Variables from the AC Game Editor. I am trying to get this list so that I can create a drop down menu from this list of variables (in editor, not at runtime) that will allow me to browse it easier and then fill in other data points like "needs to be true" or "needs to be an int > 15"

Here's a mockup example of why I want this to work in editor:
So, for example, lets say I have a scriptableobject called "character" and they have a list of dialogues

  • Dialogue A, B, C, etc.

I put this on an npc in the world and when I go to talk to them, it pools "character" and looks through each to see if the "flag" or Global variable is valid

  • Dialogue A Requires boolean "hasTalkedToCarl" to be True

If this global variable is true, then Dialogue A will appear as a valid conversation option and can be selected and everything will run as normal.

I have currently tried adding a button to the inspector that runs the following line of code:

myVars = AC.GlobalVariables.GetAllVars();

This line of code returns null unless the project is running. When it runs I can get the data without any problem, and when it's done running it stays because it's a scriptable object. The problem is that I have to run the game and click this button to get it to work and when I plan to have a lot of Global variables you can see how this might become a problem. I'm not sure if Adventure Creator can support this because GetAllVars() relies on RuntimeVariables, which means it's likely that it just can't work in the editor. My current workaround that I'm implementing just to continue progressing while I wait is just to use the int _id, run the game and hit the button to get a temporary string that just checks and sees if it's accurate.

But what if I accidentally delete a variable? Or intentionally do so, that will shift the entire list!

Thank you for taking a moment to read this, any suggestions from anyone towards a solution to my issue would be appreciated!

Comments

  • Welcome to the community, @Edmango.

    The variables returned by GlobalVariables.GetAllVars are a copy of the original set in the Variables Manager - they won't exist in Edit mode, but neither will be referencing those listed at runtime.

    To read the original variables listed in the Variables Manager, either in edit mode or runtime, you can read:

    AC.KickStarter.variablesManager.vars
    

    But what if I accidentally delete a variable? Or intentionally do so, that will shift the entire list!

    Unless externally modified, a variable's id value is consistent regardless of what happens to others in the list. So long as you make sure that any serialized references you make to a variable are through its id value, then deleting others shouldn't affect it.

    If you wanted to expose a drop-down list to choose variables from, but record the ID instead of the index, you can iterate through the variables before hand to get the index value, i.e.:

    List<GVar> myVars = KickStarter.variablesManager.vars;
    List<string> labelList = new List<string> ();
    int index = -1;
    for (int i = 0; i < myVars.Count; i++)
    {
        labelList.Add (myVars[i].label);
        if (myVars[i].id == variableID)
        {
            index = i;
        }
    }
    
    index = UnityEditor.EditorGUILayout.Popup ("Variable:", index, labelList.ToArray());
    variableID = myVars [index].id;
    
  • Thank you so much for the response, using the Kickstarter.variablesManager.vars I was able to accomplish what I wanted. I've spent the majority of the day just fiddling around with how it looks and works, but here's a screenshot of the end result:

    https://imgur.com/a/3FSUiZt

    I have access to the data now in my scriptable object, all that's left is to implement the system and make sure it meshes well with Adventure creator. Though I have some Naninovel/Adventure creator integration questions that I'll be posting about separately.

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.