How do I get a child of an array from it's index

I have been looking all over the internet for a solution to this problem, but all of them were unclear.
I need a simple expression, not anything situation specific.

Use ArrayName[index], where index is a number of a number variable.

Simply using ArrayName[index] returns “No object, variable, or property with this name found”
News.ArrayName[index] returns a 0

Oh, I misunderstod. ArrayName is to be replaced with the Array’s Name. Sorry!

1 Like

You need to replace ArrayName with the name of your array. You need to set the value of index too. You need to have elements in the array.

So, for example, if your array is named ArrayOfFruit, and has elements (in order) “Apple”, “Fejoa” and “Banana”, then ArrayOfFruit[0] will return “Apple” (because the array is zero indexed, meaning the first element of the array is at position 0)

Haha, too fast. I posted my response after I saw your rely :smiley:

That still returns the error “You can’t use brackets to access an object variable.” It suggests I use a dot, but that wouldn’t let me use variables.

What are the elements of the array? Can you screen shot how you are using it?

To use a global variable as you are, you need to declare it in the global variable list (as you have the array).

Otherwise, reference it with GlobalVariable(global_variable_name)

So if global variable newsNum is not defined, then that red underlined part should read news[GlobalVariable(newsNum)]

Also, I’d suggest you use global variables when they’re for the whole game and are used across different scenes. Otherwise, just use a scene variable. Same rules apply about declaring it, otherwise you’ll need to use Variable(scene_variable_name) to reference them.

I think it actually means you can’t use brackets, as that has the same error.

No, you need to use brackets to reference an array element. In this case it means it doesn’t know about the global variable newsNum.

To reference an array element, you use a dot, which isn’t what I’m looking for. It returns the same error when I use a plain number.

No, you do not reference array elements with a dot. That is for structures.

Have you defined news as a global variable, or as a scene variable? Which version of GDevelop are you using?

Because this works for me (I defined news as a global array), and it spits out randomly selected elements:

And a structure is what you have :man_facepalming: . How did I miss that in your earlier post? Because you started the thread with “Array”.

You’ll need to change the reference to a string news[GlobalVariableString(newsNum)], as you are referencing the value of the element using the key.

In future, please use the correct terminology. It makes a difference.

As a general question, why are you using a structure? You don’t reference a structure using an index. It’s incorrect. Structures are key-value pairings, and the key part is a string. It could be a number (but in string form), but more likely It will be a word (like “name”, “age”, “health” etc).

If you just want to randomly select an element, then an array is a much better option.

1 Like

I changed it to an Array after noticing that so no

Ok, if you’re using an array then my previous post stands.

Let us know if you still have issues with it and have set up news as a global variable,

I updated to the newest version, and the issue still stands. “You can’t use brqckets to access an object variable.”