• Hey! Guest! The 37th GMC Jam will take place between May 28th, 12:00 UTC and June 1st, 12:00 UTC. Why not join in! Click here to find out more!

GML Different objects in a 'for' loop (SOLVED)

josyanf1

Member
Hi all! Programming objects that have different directions I thought I could make a for loop to optimize code.


I usually do it this way to assign attributes to an object:
Code:
var particle_1 =instance_create(x,y,obj_firework_particle); with particle_1 {direction = 30};

var particle_2 =instance_create(x,y,obj_firework_particle);with particle_2 {direction = 60};

But I'm still a novice programmer, do you know how it can be done?
Code:
for (c = 0; c < 12; c += 1)
{
var ?? = instance_create(x,y,obj_firework_particle); with ?? {direction = 30 * c};
}
A greeting and thanks for the help! :)
 
for (var c = 0; c < 12; c += 1)
{
with (instance_create(x,y,obj_firework_particle))
{direction = 30 * c};
}

Can be done I think....?? By having it in the with statement you don't need to define it as a variable, and making the variable in the for loop local it is accessible to the instance in the with statement.
 

chamaeleon

Member
Hi all! Programming objects that have different directions I thought I could make a for loop to optimize code.


I usually do it this way to assign attributes to an object:
Code:
var particle_1 =instance_create(x,y,obj_firework_particle); with particle_1 {direction = 30};

var particle_2 =instance_create(x,y,obj_firework_particle);with particle_2 {direction = 60};

But I'm still a novice programmer, do you know how it can be done?
Code:
for (c = 0; c < 12; c += 1)
{
var ?? = instance_create(x,y,obj_firework_particle); with ?? {direction = 30 * c};
}
A greeting and thanks for the help! :)
Using ?? is syntax error. Using inst, foo, or some other non-reserved word would do just fine. Instance ids are just numbers, and as such can be assigned to variables (instance, local, global), and passed around, stored in data structures. In other words, there's nothing special about the return value from instance creation. Just assign it to a name that makes sense to you (and that doesn't conflict with a name in use by GMS already, like id) and use it.
 

GMWolf

aka fel666
Well you got very close!
The ??? Can be whatever you like.
Also, 'c' needs to be declared local (using var) so that it is visible inside the with block.
That is because otherwise it is an 'instamce variable', and is only accessible through that instance.
For example :
Code:
for (var c = 0; c < 12; c += 1)
{
var particle = instance_create(x,y,obj_firework_particle);
 with particle {direction = 30 * c};
}
Every iteration of the loop, 'particle' will have a different value.

Or you can make use of the dot notation to make your code a little simpler:

Code:
for (var c = 0; c < 12; c += 1)
{
var particle = instance_create(x,y,obj_firework_particle);
particle.direction = 30 * c;
}
 
I think you're confusing yourself a bit with the temporary variables and assigning values to them. In this code you posted:
Code:
var particle_1 =instance_create(x,y,obj_firework_particle); with particle_1 {direction = 30};

var particle_2 =instance_create(x,y,obj_firework_particle);with particle_2 {direction = 60};
You could just as easily do this:
Code:
var particle =instance_create(x,y,obj_firework_particle); with particle {direction = 30};

particle =instance_create(x,y,obj_firework_particle);with particle {direction = 60};
And it would function identically. You only need to keep the ID in the variable as long as you need to manipulate that specific instance, once you have done the manipulation, you are free to use the variable again. That's why it's fine to do it like that in a loop. GMWolf's above code, when broken down into the individual loops, functions exactly like the code above, e.g.:

Loop 1:
Code:
var particle = instance_create(x,y,obj_firework_particle);
particle.direction = 30 * 0;
Loop 2:
Code:
var particle = instance_create(x,y,obj_firework_particle);
particle.direction = 30 * 1;
Loop 3:
Code:
var particle = instance_create(x,y,obj_firework_particle);
particle.direction = 30 * 2;
There's no need to worry about making separate variables for each instance.
 

josyanf1

Member
for (var c = 0; c < 12; c += 1)
{
with (instance_create(x,y,obj_firework_particle))
{direction = 30 * c};
}

Can be done I think....?? By having it in the with statement you don't need to define it as a variable, and making the variable in the for loop local it is accessible to the instance in the with statement.
Using ?? is syntax error. Using inst, foo, or some other non-reserved word would do just fine. Instance ids are just numbers, and as such can be assigned to variables (instance, local, global), and passed around, stored in data structures. In other words, there's nothing special about the return value from instance creation. Just assign it to a name that makes sense to you (and that doesn't conflict with a name in use by GMS already, like id) and use it.
Well you got very close!
The ??? Can be whatever you like.
Also, 'c' needs to be declared local (using var) so that it is visible inside the with block.
That is because otherwise it is an 'instamce variable', and is only accessible through that instance.
For example :
Code:
for (var c = 0; c < 12; c += 1)
{
var particle = instance_create(x,y,obj_firework_particle);
 with particle {direction = 30 * c};
}
Every iteration of the loop, 'particle' will have a different value.

Or you can make use of the dot notation to make your code a little simpler:

Code:
for (var c = 0; c < 12; c += 1)
{
var particle = instance_create(x,y,obj_firework_particle);
particle.direction = 30 * c;
}
Thank you all for your kind answers and for your patience.
As many say, it is only about declaring the variable 'c' of the 'for' loop and inside the loop creating the object in a variable with the 'with' in a normal way :)

Code:
for (var c = 0; c < 12; c += 1)
{
var particle = instance_create(x,y,obj_firework_particle);
with particle {direction = 30 * c};
}
 
Top