Windows initializing variables

I was just looking at a GMS2 best practice guide to make sure I'm on track. I did not know there was an array_create() function. But I guess this makes sense in situations when you know the maximum size. In discovering this I found array_set(variable, index);

My question is what is the difference between array_set(variable, index); and variable[?] = x;

I'm finding out all sorts of stuff I didn't know such as array = 0; I didn't know you could do this. I thought you had to loop.

If anyone could explain this I would be most grateful. Any other optimization tips would also be most welcome.

Edit: sorry doing a columbo here. Does anyone know of any plans to tighten up the syntax in GM?
 
Last edited:

chamaeleon

Member
Bah, I can't justify throwing in another layer of unknowns right now, while http_get works perfectly fine.

If http_get works, and im not typing $_POST incorrectly in script, then everything outside of GM is done.
I was just looking at a GMS2 best practice guide to make sure I'm on track. I did not know there was an array_create() function. But I guess this makes sense in situations when you know the maximum size. In discovering this I found array_set(variable, index);

My question is what is the difference between array_set(variable, index); and variable[?] = x;

I'm finding out all sorts of stuff I didn't know such as array = 0; I didn't know you could do this. I thought you had to loop.

If anyone could explain this I would be most grateful. Any other optimization tips would also be most welcome.

Edit: sorry doing a columbo here. Does anyone know of any plans to tighten up the syntax in GM?
Not much of a difference using
GML:
array_set(arr, idx, x);
arr[idx] = x;
On the other hand
GML:
arr2 = arr;
arr = 0;
means you're assigning the value 0 to the variable arr. At that point arr is no holding an array value, it is holding the value 0. The line of code does not set all array elements to 0, if that is what you imagined when you mention a loop. The previous array and its content stored in arr is no longer accessible through arr, but would be through arr2 (while keeping in mind GMS copy-array-on-write unless @ notation is used to modify the array content).
 
I'm with you just up till the part in the brackets (while keeping in mind GMS copy-array-on-write unless '@' notation is used to modify the array content). Unless I misunderstanding is you saying that using the '@' notation I will negate copy-on-write. If so Why would you want it ever to copy-on-write that sounds atrocious? I must be missing something. Is it only arrays with non-defined sizes that do the copy-on-write?

Thanks for you help by the way. Everyone on these forums are very helpful and polite.
 

Yal

🐧 *penguin noises*
GMC Elder
(while keeping in mind GMS copy-array-on-write unless @ notation is used to modify the array content).
I'd be careful not to push that too far. I had a bug not too long ago where I tried to copy an array (which was inside another array - my goal was to duplicate the first data point to make the data form a closed loop) and then modify some of the copy's values (since the position data needed to be monotonously increasing for each data point)... but it ended up reflecting on both the original and the copy. So I'd ended up copy-by-value'ing an array reference without realizing, which means copy-by-reference instead of copy-by-value as I expected. Very confusing times were had by all. If you explicitly need an unrelated copy of the data, remember that there's array_copy to do this kind of thing.
 

chamaeleon

Member
I'm with you just up till the part in the brackets (while keeping in mind GMS copy-array-on-write unless '@' notation is used to modify the array content). Unless I misunderstanding is you saying that using the '@' notation I will negate copy-on-write. If so Why would you want it ever to copy-on-write that sounds atrocious? I must be missing something. Is it only arrays with non-defined sizes that do the copy-on-write?

Thanks for you help by the way. Everyone on these forums are very helpful and polite.
GML:
var arr = [1];
var arr2 = arr;
show_debug_message(string(arr[0]) + " -- " + string(arr2[0]));
arr2[@ 0] = 2;
show_debug_message(string(arr[0]) + " -- " + string(arr2[0]));
arr2[0] = 3;
show_debug_message(string(arr[0]) + " -- " + string(arr2[0]));
Code:
1 -- 1
2 -- 2
2 -- 3
Using @ in the index expression makes no copy of arr to its own array in arr2, so both variables refer to the same array. When @ is not used, the array is copied, and the value assigned becomes unique to arr2's version of the array. A common use-case is to use the @ operator for all updates to an array argument so the array passed to the script is updated, rather than a temporary copy being updated inside the script (which scenario you want and whether to use @ or not depends on the hypothetical script in question).
 
Last edited:

chamaeleon

Member
I'd be careful not to push that too far. I had a bug not too long ago where I tried to copy an array (which was inside another array - my goal was to duplicate the first data point to make the data form a closed loop) and then modify some of the copy's values (since the position data needed to be monotonously increasing for each data point)... but it ended up reflecting on both the original and the copy. So I'd ended up copy-by-value'ing an array reference without realizing, which means copy-by-reference instead of copy-by-value as I expected. Very confusing times were had by all. If you explicitly need an unrelated copy of the data, remember that there's array_copy to do this kind of thing.
It is a bit unclear to me what you're saying that contradicts what @ is supposed to do.. Or maybe I have not pushed arrays to the limit..
 

Felbar

Member
maybe this will clarify

basically if an array or other data structure is passed into a script
and modified in that script, it will be copied unless you use the
accessor style;
//in script
var the_array = argument0;
the_array[0] = 5; //this will cause a copy of whole array

the_array[@ 0]= 5; //this will modify original reference

and its just a shorthand for
array_set(the_array, 0, 5);

reading from the array

var value = the_array[0]; //does not cause copy

but ppl just generally use. the_array[@ 0] for both
because it doesn't cause an error and ensures you don't cause a copy
 
I'm glad you said that. I was about to search and replace 1000's of lines of code. I really wanted to understand this correctly before doing so as I expected problems as you do when you have to learn a new branch of logic you are not familiar with. I have had similar problems to you in the past many times, thinking you have understood something new then later finding you really didn't.
 

Yal

🐧 *penguin noises*
GMC Elder
It is a bit unclear to me what you're saying that contradicts what @ is supposed to do.. Or maybe I have not pushed arrays to the limit..
The manual implies @ only has an effect in scripts. In either case, I was modifying var scope arrays (to create side effect output data once the data had been processed) instead of having the script operate on an array in the caller object scope, so it doesn't apply.

When setting a variable henry, I expected to set it to a copy of richard (knowing GM's pass-by-value behavior), but it was set to a reference instead. So modifying henry also modified richard which I didn't want to modify. (Names chosen for illustrative purposes only)
 
That does clear things up somewhat. I don't think ill modify my code or stress about this too much until I have performance issues. This goes against my normal principals but I don't believe my code has many cases where this would be necessary. I will bear that in mind but I don't think I'm going to bust a ball over this particular issue. Thanks again for your help. This was most useful.

Edit: When you say script you mean Gamemaker 'IDE scripts' or are you talking in more general terms?
 

chamaeleon

Member
The manual implies @ only has an effect in scripts.
In this particular case, I'll take my chances, I think (famous last words). The manual is good and covers just about everything, but its strong suit is not exact definitions.
 

Felbar

Member
The code you write in events is literally saved as a script, check your project folder

and yeah i mean in scripts or anywhere the array is passed too outside of the original reference,
user events , i'm not sure if other or with in other events causes a copy but no harm in using
other.my_array[@ 0] = 5;

I should add unless your arrays are huge or you are doing many tiny arrays this probably will not cause a performance problem.
the average object is probably bigger and we create and destroy many of those per frame all the time
 
Last edited:
Top