Can you send entire arrays to a script?

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.
 
M

Mordecai142

Guest
just forget the [] at the end so: scr_display_message(messages);
 
C

CedSharp

Guest
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 happen if you don't use the accessor:
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
The above code will not run as expected because it wouldn't modify the original array.
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
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
This way, the orignal array will be modified and you never created a duplicate one.

Hope this helps ~
CedSharp
 
Last edited:
C

Casp

Guest
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:
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
The above code will not run as expected because it wouldn't modify the original array.
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
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
This way, the orignal array will be modified and you never created a duplicate one.

Hope this helps ~
CedSharp
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!
 
C

CedSharp

Guest
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 :p
 
C

Casp

Guest
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 :p
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;
Code:
for(i=0; i=7; i+=1)
{
Position[i] = obj_controller.x + (26*i)
}
This is within a script that is applied during a room with the obj_controller within it. I later call this variable within the same script to prove its existence to test if every variable in the code works as it should. By creating an object that will just draw this variable, as such;

Code:
tell = instance_create(0,0,obj_tell); //create obj_tell, the drawing one

with (tell)
{
Pos = Position[5] //set pos in tell to position
}
Everytime I try this I get told that I am "trying to index a variable which is not an array" which is referring to the Position array. Am I doing something evidently wrong here?
Sorry if this is off topic but since you know your stuff with arrays and scripts I was assuming that you could help.
Thanks!
 

FrostyCat

Redemption Seeker
This has absolutely nothing to do with arrays, it has to do with your improper usage of for loops.
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.
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.

Remember: To loop n times using for:
  • If it starts at i=0 and increments by 1, check i<n.
  • If it starts at i=1 and increments by 1, check i<=n.
 
C

CedSharp

Guest
Like @FrostyCat said,

1. you set i to 0.
2. you check if i=7
3. its false, so the loop doesnt execute..

perhaps you meant this?
Code:
for( i=0; i<7; i++ ) {
    Position[i] = obj_controller.x + ( 26*i );
}
regards
 
Top