[SOLVED/TUTORIAL] Dynamic arrays and structure notation

H̶i̶!

T̶h̶e̶ ̶a̶l̶g̶o̶r̶i̶t̶h̶m̶s̶ ̶n̶e̶e̶d̶e̶d̶ ̶f̶o̶r̶ ̶t̶h̶e̶ ̶g̶a̶m̶e̶ ̶I̶’̶m̶ ̶w̶o̶r̶k̶i̶n̶g̶ ̶r̶e̶q̶u̶i̶r̶e̶s̶ ̶t̶o̶ ̶d̶y̶n̶a̶m̶i̶c̶a̶l̶l̶y̶ ̶a̶d̶d̶ ̶s̶o̶m̶e̶ ̶c̶h̶i̶l̶d̶s̶ ̶v̶a̶r̶i̶a̶b̶l̶e̶s̶ ̶t̶o̶ ̶a̶ ̶s̶t̶r̶u̶c̶t̶u̶r̶e̶ t̶h̶r̶o̶u̶g̶h̶ ̶a̶n̶ ̶i̶t̶e̶r̶a̶t̶i̶o̶n̶,̶ ̶a̶t̶ ̶r̶u̶n̶t̶i̶m̶e̶,̶ ̶t̶o̶ ̶s̶i̶m̶u̶l̶a̶t̶e̶ ̶a̶n̶ ̶a̶r̶r̶a̶y̶.̶ ̶ W̶h̶a̶t̶’̶s̶ ̶t̶h̶e̶ ̶s̶y̶n̶t̶a̶x̶ ̶n̶e̶e̶d̶e̶d̶ ̶t̶o̶ ̶t̶h̶a̶t̶?̶ ̶H̶o̶w̶ ̶c̶a̶n̶ ̶I̶ ̶d̶y̶n̶a̶m̶i̶c̶a̶l̶l̶y̶ ̶d̶e̶c̶l̶a̶r̶e̶ ̶t̶h̶e̶ ̶n̶a̶m̶e̶ ̶o̶f̶ ̶t̶h̶e̶ ̶c̶h̶i̶l̶d̶ ̶o̶f̶ ̶a̶ ̶s̶t̶r̶u̶c̶t̶u̶r̶e̶?̶

T̶h̶a̶n̶k̶s̶ ̶i̶n̶ ̶a̶d̶v̶a̶n̶c̶e̶.

[size=200]SOLUTION:[/size]

You can use structure notation to declare the child of a structure:

Do = 1 To Variable myStruct["myChildVar"]

You can also declare it dynamically:

Do = 1 To Variable myStruct[VariableString(myVar)]

And use almost the same syntax to dynamically access its value:

Do = Variable(myStruct[VariableString(myVar)]) To Variable myVar

So, you can simulate dynamic arrays (declared during an iteration for example):

Do = 1 To Variable myStruct[VariableString(myCounterVar)].myChildVar

And dynamically access its value:

Do = Variable(myStruct[VariableString(myCounterVar)].myChildVar) To Variable myVar

Or access the value of a specific element of the array (in this example, 5):

Do = Variable(myStruct["5"].myChildVar) To Variable myVar

You can even use double square brackets to simulate a 2D array:

Do = 1 To Variable myStruct[VariableString(counter1)][VariableString(counter2)].childVar

And dynamically access to its value with the same syntax:

Do = Variable(myStruct[VariableString(counter1)][VariableString(counter2)].childVar) To Variable myVar

[i]This tutorial in the wiki: http://www.wiki.compilgames.net/doku.php/en/game_develop/tutorials/simulatingdynamicarrayswithstructurenotation

Thanks to Victor and Lizard-13 for the help.[/i]

2 Likes

[Edit]-----------Autoquoted by error. Sorry. Can’t delete the duplicate.

There are nothing to do, just use it.

Use what? I don’t even know what to use! It’s not documented!

Can I dynamically create a variable with the name of the value stored yet in another variable? How?

Do= Variable(tempVar) To Variable myStruct[VariableString(count)]

or

Do= Variable(tempVar) To Variable myStruct[count]

or

What?

Nothing seems to work… I need to know if there is a way to simulate a dynamic array declared during runtime.

The first syntax should work.
http://wiki.compilgames.net/doku.php/en/game_develop/tutorials/howtousevariables (last section)

Ok, I need to dynamically create a variable (a structure child) and named it as the value of another variable (another structure child, from another structure)

Do= “whateveeer…” To Variable myStruct[VariableString(myStruct2[VariableString(var)])]

This is right?

Thanks for edit your comment and add that useful link to the wiki but I already know it and the question I’m making is not documented there (I read it, completely, just before make this thread).

Yes, there say how to dynamically access childs of structures, but it don’t explains how to dynamically create childs of structures or if some functions as “Variable” or “VariableString” are needed inside the brackets of the structure notation or if the structure notaion with brackets is always strictly necessary… or if there can be arrays of structures inside structures (ok, that’s another question). But nope, it’s not documented, there isn’t a easy way to access that knowledge.

Even more funny, I have spent all my night (it’s night in this hemisphere) testing different syntax combinations and have identified two different syntax for the variable name field, that don’t makes the compiler crash, but it don’t seems to be working right in any of the two cases.

To be honest, all the core mechanics of my game (as any other more or less complex game) depends on the possibility of create, manage and modify dynamics arrays during runtime… and I hope I’m wrong or misunderstanding something but that seems to be a nightmare, in GD.

Check this :slight_smile: :
http://www.forum.compilgames.net/viewtopic.php?f=46&t=6222&p=48691#p48725

Do= "whateveeer..." To Variable myStruct[VariableString(myStruct2[VariableString(var)])]

It doesn’t work? Looks good :confused:
If you can provide a basic example where it doesn’t work, you could share it :slight_smile:

Wait! What?

You’re saying you can use double square brackets to create a two-dimensional array?

That change absolute everything! If that is possible and the syntax Victor and you are suggesting is right… then it will take a year organizing the actions and events of my game… but at least it’s possible in GD.

You can use

myStruct["10"]["5"]

to access an element like in a two dimension array (I said “like” because it’s not really a two dimension array).

And can I “declare” that element in the same way?

Can I declare a “two-dimension” array element like this:

Do =Variable(myVar) To Variable myStruct[VariableString(count_1)][VariableString(count_2)].childVar

And access in this way:

Do = Variable(myStruct[VariableString(count_1)][VariableString(count_2)].childVar) To Variable myVar

Bacause that’s great but it’s not working or I’m still doing something wrong.

Yes, and that should work, can you send a project file with these events to see what’s wrong ?

EDIT : When you access it, you have to put Variable(…) around myStruct[…][…].myChild :wink:

Yes.

Syntax is working.

Thanks.

Just a side note to say that it would be interesting to have this thread as a tutorial on the wiki :slight_smile:

.

Done!

http://www.wiki.compilgames.net/doku.php/en/game_develop/tutorials/simulatingdynamicarrayswithstructurenotation

1 Like

Awesome! This will be of a great use for people trying to do advanced things with variables :smiley: