Get the Index of an Array Var

Let’s say that I have an Array “A” with a child variable String of “B” at Index 0… there is a way to get the Index 0 by the String “B”?

More generally… there is a way to get the Index of a String (or number) knowing only the String (and the Array, obviously)?

It’s possible?

1 Like

Yes, and I think the only way will be to iterate through each array element, checking the value.

1 Like

But I dont know which Action or Condition use… some advice?

Here’s an example of how to do it:

The array is defined as:

And the events are as follows:

Hopefully you can follow what’s going on, but reply if you’d like it explained a bit :slight_smile:

1 Like

Wonderful! I get it!
Thank you lot, savior!

Dont suppose theres a way to do this with a structure rather than an array, is there?

How is your structure setup? Is it a single structure or a structure of structure? If the structure is relatively consistent in structure then you could use a for each child to go through the structure. There’s probably a very simple way to do it through JavaScript.

What does the structure look like and what do you want to search for?

It is EXTREMELY consistent

so basically lol
(because its Huge), it goes:

GlobalVariable
{Child1(structure) :
Child1.1(string) : Value,
Child1.2(string : Value,
Child1.3(string) : Value,
Child1.4(number) : Value,
[Child1.5(array) :,
0(string : Value
1(string) : Value]
Child1.6(string) : Value,
Child1.7(number) : Value,
Child1.8(number) : Value,
Child1.9(number) : Value,
,

Child2(structure) :
(Identical to the above. wash. rinse. repeat. hundreds of times…)

My hope is to be able to dynamically extract and copy a child structure and all its chitlins (1.1, 1.2, 1.3, ect) knowing only its name, and then copying each value to an object variable…

I’m thinking. You can’t dynamically pick a variable. You can pick the children but not the parent meaning the variable name. You can use for each child to go through the structures. Are you going through all of them or just specific ones? (for each child only works with scene variables but you can copy global to scene)

Is the data already defined or loaded through something like a JSON file?

How are you with creating extensions and JavaScript? I think an extension with JavaScript might be able to pick specific structures and interact with a sprite fairly easily.

If it was something like
game.a1.name

It would be as easy as
game[stringVariable].name

But I’m not sure how to get it without the game part because in GD that part can’t be dynamic.

Some more info would help. What is the purpose of this? Variable names and children names should start with a letter or a couple of symbols. I’m not sure if a1.1 would be an issue. Technically, 1 can be part of a string as long as it a1[“1”] not a1[1] that would be an array.

A little more info would help.

I only used “1.1” as an example. I have a database already built in another app. I’ve set it up so I can continue to add more items to that database and it will automatically turn the entire database (including updated and new items) into a .JSON, which I then load into the game at start-up.

to provide more detail and context:

[
{
“Name”: “Recon Drone”,
“Faction”: “Terrans”,
“Art”: “”,
“Type”: “Tech & EQ”,
“Sub-Type”: [ “Drone” ],
“Rarity”: “Uncommon”,
“Rules/Text”: “When Recon Drone enters the battlefield, choos 3 cards from target opponents hand and then the reveal those cards. ‘Flying’”,
“Att”: 0,
“Def”: 1,
“SDC”: 0,
“HP”: 1,
“Attributes”: [ “Flying” ],
“Buffs”: “”,
“I”: 2,
“G”: 2,
“Hunger”: 0,
“Influence”: 0,
“Gold/rnd”: 0,
“Food”: 0,
“Art.http”: “”
},
{
“Name”: “Tango-Six, 3rd Inf.”,
“Faction”: “Terrans”,
“Art”: “”,
“Type”: “Combat Unit”,
“Sub-Type”: [ “Human” ],
“Rarity”: “Common”,
“Rules/Text”: “When Tango-Six, 3rd Inf. is deployed, group Tango-Six, 3rd Inf. with target Champion you control. Otherwise sacrifice Tango-Six, 3rd Inf. then deal its Att to target enemy combat unit without Flying.”,
“Att”: 1,
“Def”: 1,
“SDC”: 0,
“HP”: 1,
“Attributes”: ,
“Buffs”: “”,
“I”: 0,
“G”: 2,
“Hunger”: 1,
“Influence”: 0,
“Gold/rnd”: 0,
“Food”: 0,
“Art.http”: “”
},
{
“Name”: “Heavy Tank”,
“Faction”: “Terrans”,
“Art”: “”,
“Type”: “Combat Unit”,
“Sub-Type”: [ “Ground-Vehicle” ],
“Rarity”: “Rare”,
“Rules/Text”: “‘Armored’ When Heavy Tank is Deployed, put 3 counters on it. (tap), remove 1 counter → This unit deals X SDP to one target enemy structure or Armored CU where X = this cards att.”,
“Att”: 3,
“Def”: 3,
“SDC”: 3,
“HP”: 3,
“Attributes”: [ “Armored” ],
“Buffs”: “”,
“I”: 6,
“G”: 5,
“Hunger”: 1,
“Influence”: 0,
“Gold/rnd”: 0,
“Food”: 0,
“Art.http”: “”
}
]

Those are a few “items” in the database, and that is the exact structure atm. before your most recent response I switched from the parant being a structure to an array instead. My thinking was to use the extension “array tools>array search>index of a string” function. Basically trying to use the “object stack” ext. to take a list of saved items (the cards the player has collected so far and arranged into a Deck), and build said deck on the fly, before every match. only problem now is whenever I try to get the index of a string from the main list, no matter what I’ve tried, it always returnes “-1”
I’ve tried this

…and this

it always returns -1 and I’m stumped lol.

btw, Thank You, for taking the time to help me out.

I’m not sure how array tool handles arrays of structures if that’s what your variable currently is. It might just search the first level or child which in your case sounds like it would be a structure and not a value.

Maybe if you used a matching structure with the names and the index as a lookup.

To search the array of structures you could loop through it with a variable as a counter inside a while. But a structure cross reference might be better. Or JavaScript or array tools. IDK. This is a little outside of my knowledge.

How many entries are you expecting. Would it be beneficial to offload the data to say firebase?

https://wiki.gdevelop.io/gdevelop5/all-features/firebase/firestore/

A manual search of an array of structures would be something like

The benefit of a manual search is you could modify it search any child variable and it could create an array of matches.

I’m probably over thinking this. IDK how large your array is. I prefer my own logic.

the only reason Firebase isnt an option is because I’m a broke ho lol. In the future I’d be more able. As to the number of entries, there are plans for a few hundred unique cards in the first release. And consistent updates and additions. I greatly appreciate all of your help :slight_smile: I’ll keep trying out some other avenues and keep my eye on here as well. :smile:

1 Like