C
Casp
Guest
If so, what is the function/piece of code I would need to do so? I can't find any quick way to do it other than typing out every single part of the array. I may be missing something obvious here.
Ah right okay, makes sense.just forget the [] at the end so: scr_display_message(messages);
// scr_set_second_value( array, value );
var array, value;
array = argument[0];
value = argument[1];
array[1] = value; // set the second position of the array to the value
// inside a random script somewhere
var arr=0;
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
scr_set_second_value( arr, 5 );
var value = arr[1]; // value = 2
// scr_set_second_value( array, value );
var array, value;
array = argument[0];
value = argument[1];
array[@ 1] = value; // set the second position of the array to the value
// inside a random script somewhere
var arr=0;
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
scr_set_second_value( arr, 5 );
var value = arr[1]; // value = 5
I wasn't planning on modifying the array, but I will be modifying others in the future. So I'll keep this in mind when I'm that up. Thanks!Also worth mentionning, if you intend to modifie the content of the array without returning it, you need to use the '@' accessor.
This is what you would do if you return the value:
The above code will not run as expected because it wouldn't modify the original array.Code:// scr_set_second_value( array, value ); var array, value; array = argument[0]; value = argument[1]; array[1] = value; // set the second position of the array to the value // inside a random script somewhere var arr=0; arr[0] = 1; arr[1] = 2; arr[2] = 3; scr_set_second_value( arr, 5 ); var value = arr[1]; // value = 2
Gamemaker will automatically duplicate the array if you modify it inside a script,
so the second value modification actually happens in a different array, hence the value
in the "real" array never changes.
To actually modify the array ( the original one ) simply add '@' inside the brackets
This way, the orignal array will be modified and you never created a duplicate one.Code:// scr_set_second_value( array, value ); var array, value; array = argument[0]; value = argument[1]; array[@ 1] = value; // set the second position of the array to the value // inside a random script somewhere var arr=0; arr[0] = 1; arr[1] = 2; arr[2] = 3; scr_set_second_value( arr, 5 ); var value = arr[1]; // value = 5
Hope this helps ~
CedSharp
I've just come up with a completely unrelated but still annoying error. This may be due to my lack of knowledge on how arrays work within scripts, but I have created code that goes as such;I know you weren't modifying the array, but since you were asking on how to actually use an array in a script,
I thought it was vital to share this important piece of information in order to avoir you what happenned to me:
hours of searching without understanding.
I actually found the solution randomly while reading on how to use array syntax with all the ds_* structures.
They talk about the '|', '#', '?' accessors, and also the '@' accessor
for(i=0; i=7; i+=1)
{
Position[i] = obj_controller.x + (26*i)
}
tell = instance_create(0,0,obj_tell); //create obj_tell, the drawing one
with (tell)
{
Pos = Position[5] //set pos in tell to position
}
The for loop in your code can't possibly run because i == 7 is false from the start, leaving the entire Position array undeclared. Hence the error.This works as follows - First statement1 is executed, then the expression is evaluated and, if it is true, statement 3 is executed. Then statement 2 and then the expression is evaluated again. This loop will continue until the expression is found to be false.
for( i=0; i<7; i++ ) {
Position[i] = obj_controller.x + ( 26*i );
}