How can I use (or substitute) pointers?

O

Olivebates

Guest
Hey! :)

I'd like to store a series of variables in an array list. Not the values of these variables, but the actual adresses in memory. How can I do this?

Thanks in advance!
xoxo, Olive
 

Perseus

Not Medusa
Forum Staff
Moderator
You can't. GameMaker passes data by value, not by reference. Even in GMS 2, a pointer is used only for some very specific functions, like getting a texture or buffer address from memory for another function. You cannot even do operations on a pointer.
 
T

Tempus64

Guest
There might be some other solution for you if you outline what you're trying to accomplish.
 
O

Olivebates

Guest
I just want to have a series of variables that each have a unique name (like playerSpeed, playerGravity, etc), and also be able to cycle through them with a loop, like you would an array.
 

dphsw

Member
I suggest keeping them in an array to begin with, and maybe having some macros defined that make it clear what each element of the array refers to. So you can say:
vars[SPEED] = 2;
vars[GRAVITY] = 0.3;
etc. (where SPEED is a macro defined as '0' and so on), and you would be able to loop through them all and copy one bunch to another, or save them all easily. Arrays aren't exactly passed by reference, though if you use the accessors (i.e. vars[@ SPEED]= ... ) then it just refers to them by reference. And if you used a ds_list instead of an array, then it definitely passes by reference.

Alternatively, you could do the same thing but put the values in a buffer - then you can use the buffer_get_address command to get a genuine pointer, which you could pass to a DLL if you wanted.
 

GMWolf

aka fel666
Arrays can pe passed by reference rather than by value. Look up accessors.

You could pass single values wrapped in arrays, and use then a little Like you would in c.

Using a map may be more suited to your needs, depending on what you are making.
 
R

rui.rosario

Guest
They're pretty slow to iterate through.
In order to speed up iteration of a DS Map you can store a DS List in the map with all the keys in the map. Then you iterate the list (which is faster than iterating the map) and just directly access the map with the key from the list.
 

PNelly

Member
In order to speed up iteration of a DS Map you can store a DS List in the map with all the keys in the map. Then you iterate the list (which is faster than iterating the map) and just directly access the map with the key from the list.
You got me there.
 
Top