How to introduce variables in an object before creating it

Carloskhard

Member
So I'm making some planets which obtein their scale and other variables from another object which spawn them and send them the variables
Code:
planeta = instance_create (xx,yy,obj_planeta);
if (yy = 7000){
            planeta.alpha = 1;
            planeta.image_xscale = 2;
            planeta.image_yscale = 2;
            }
, however, when planets execute their create code they don't have those varibales and they have to wait one frame before they come so my only option is to wait one frame with alarm[0] and do so for every inherent variable, which is ugly.
What can I do so objects obtain those variables before executing their create code? Thanks
 

Simon Gust

Member
So I'm making some planets which obtein their scale and other variables from another object which spawn them and send them the variables
Code:
planeta = instance_create (xx,yy,obj_planeta);
if (yy = 7000){
            planeta.alpha = 1;
            planeta.image_xscale = 2;
            planeta.image_yscale = 2;
            }
, however, when planets execute their create code they don't have those varibales and they have to wait one frame before they come so my only option is to wait one frame with alarm[0] and do so for every inherent variable, which is ugly.
What can I do so objects obtain those variables before executing their create code? Thanks
There is nothing you can do except putting the logic that requires these new variables also right there in your code you've shown.
 

samspade

Member
So I'm making some planets which obtein their scale and other variables from another object which spawn them and send them the variables
Code:
planeta = instance_create (xx,yy,obj_planeta);
if (yy = 7000){
            planeta.alpha = 1;
            planeta.image_xscale = 2;
            planeta.image_yscale = 2;
            }
, however, when planets execute their create code they don't have those varibales and they have to wait one frame before they come so my only option is to wait one frame with alarm[0] and do so for every inherent variable, which is ugly.
What can I do so objects obtain those variables before executing their create code? Thanks
This is a common problem. A similar, although not much less clunky way is to redo all the variables in the code which sets the variables. So after saying planeta.image_xscale = 2; you simply put the code that relied on that.

If you have GMS 2 there's a new instance variables section available in the IDE where you can define variables and then set those variables in the specific instance in the room editor and those variables run right before the create event. Won't work if you're creating objects dynamically, but if a bunch of them are set up first, then this is one way around it.
 

Paskaler

Member
Your specific problem can be easily fixed:
Code:
planeta = instance_create (xx,yy,obj_planeta);
if (yy = 7000){
           planeta.alpha = 1;
           planeta.image_xscale = 2;
           planeta.image_yscale = 2;
           }
You're passing yy into the created instance, which will have its y instance variable set in the creation code.
So, just move your if statement into the creation code and replace yy with y. You also need to remove the planeta. part, as well
 

CedSharp

Member
So I'm making some planets which obtein their scale and other variables from another object which spawn them and send them the variables
Code:
planeta = instance_create (xx,yy,obj_planeta);
if (yy = 7000){
            planeta.alpha = 1;
            planeta.image_xscale = 2;
            planeta.image_yscale = 2;
            }
, however, when planets execute their create code they don't have those varibales and they have to wait one frame before they come so my only option is to wait one frame with alarm[0] and do so for every inherent variable, which is ugly.
What can I do so objects obtain those variables before executing their create code? Thanks
If you want something to happen after YOU do something, that's the perfect use case for User Events!
Place the code that requires the changed variables in one of the Other/Event User, then in your piece of code, after setting the variables, call event_user()

This way you don't have to wait a frame, and the code will execute with the new variables set before it.
 

TheouAegis

Member
Set all the variables inside a global array. Then after the object's create event just fetch the values from that array. If you fail to set the array first, that's your own fault.

But then by that same token, you could use a user event, in which case if you forget to call the user event after creating the instance, that's your own fault.
 
Last edited:

CedSharp

Member
Set all the variables inside a global array. Then after the object's create event just fetch the values from that array. If you fail to set the array first, that's your own fault.

But then by that same token, you could use a user event, in which case if you forget to call the user event after creating the instance, that's your own fault.
But then every time he creates a new instance, he has to overwrite the values of that global array? Isn't that counter-intuitive ?
Remembering to call event_user() can be leveraged by creating a script, for example "scr_set_values(inst_id, value1, value2, value3)" and it would auto-call event_user(0).
 
K

Khanon

Guest
Sorry to say this to you ... but your way of doing things is good and bad (50-50).

You're right, it is possible to pass variables from the spawner to the spawned
but you shouldn't doing it if you can do it in a more direct way.
In my case, i really couldn't bypass it ...
so i have done like you (spawned's variables into the spawner)
I will not argue why i couldn't bypass it, cause it's a deep issue of my code.​

But in your case, here is the more direct way.
2 objects :
- obj_suna (as spawner)
- obj_planeta (as spawned)

in obj_suna [create_event]
Code:
xx=self.x
yy=self.y
instance_create (xx,yy,obj_planeta);
in obj_planeta [create_event]
Code:
if (obj_suna.yy >= 7000) {
          alpha = 1;
          image_xscale = 2;
          image_yscale = image_xscale;
} else {
          randddd = random_range(0.6,1.8);
          alpha = 0.4;
          image_xscale = randddd;
          image_yscale = image_xscale;
}
it is not exactly your main code, but it will works if your program still simple.

Oh, i forgot, ... 7000px for an y position seems to me really hight.
I didn't add a loop for obj_planeta spawning ... neither randomness in the spawning position.
Becarefull too, in my version code : it's not "= 7000" but ">= 7000".

Have fun :p
 
Last edited by a moderator:
Top