Under review

How to read VariablesManager.GetListItem

francisco chong 5 months ago in Game Creator updated 4 months ago 5

Hi I'm trying to iterate a ListVariable and Assign the contest to a c# list just for testing.

the ListVariable im iterating has gameobject references in it.

List list = new List { };

for(int i=0; i<6; i++)
list.Add(VariablesManager.GetListItem(characterGOs, ListVariables.Position.Index, i));

this obviously doesn't work, I would like to know how do i extract the gameObject from the GetListItem method



Unity version:
Game Creator version:
Under review

Hi Francisco. You're doing it right. If you're not getting the game object, probably means that the object "characterGOs" is not the one with the List Variable component.


Marti - for list vars it appears Variable.Get() is not getting called. Instead of the value it appears the entire Variable object is getting returned instead.

Steps to reproduce:

1) Create a game object with a local var named "Locals Only" and a list var, both with type string (other types have same issue but are harder to debug). Set initial value for local to "Initial Local var Value" and index 0 to "Initial first in list value"

2) Create another game object and put this code on it: 

 public GameObject objSpawnId;
 private void Start() {
Debug.Log("Player Manager Start");

public void TestLocalStrings() {
object result = VariablesManager.GetLocal(objSpawnId, "LocalsOnly");
Debug.Log("PlayerManager TestLocals value = " + result);

public void TestListVarString() {
object result = VariablesManager.GetListItem(objSpawnId, ListVariables.Position.First);
Debug.Log("PlayerManager TestListVars value = " + result);}

3) Drag the first game object into objSpawnId and run.

4) On Variable.cs line 86 add:  Debug.Log("In Variable Get for name = "+ this.name);

5) Results: Notice Get is called for local, but not for list vars (which don't have name, but the debug log should still display).

Additional note: 

Change the list var type from string to float and casting the result (as shown example in https://docs.gamecreator.io/game-creator/systems/game-creator-api/variables-access), works for local vars but throws a run time error for list vars: InvalidCastException: Specific cast is not valid. 

Strings and GameObjects are not strictly typed like floats, so no run time errors are thrown. 

    Hi, GameUX

    maybe, I can give some code to help you

    This is whats happening to me also.

    Now im trying to get a Sprite list which I am not able to.

    InvalidCastException: Specified cast is not valid.

    var q = VariablesManager.GetListItem(questionImagesList, ListVariables.Position.Current); VariablesManager.SetLocal(questionImage, "spriteData", q);

    the local variable script is attached to "questionImage" with the name "spriteData" setup as Sprite type.
    questionImagesList is the gameObject with the ListVariables component with a list of Sprites.

    Edit: adding .Get<Sprite>() at the end of line 1 fixed the issue
    var q = VariablesManager.GetListItem(questionImagesList, ListVariables.Position.Current).Get<Sprite>();

    Thanks @JOON