[SOLVED] Object variables on instance_create()

G

Gaspode

Guest
Hi, new to GM:S but absolutely loving it! Great forum here, learnt a ton in the last week and my first game is progressing well.

My question... I have a single room for my game part and dynamically instantiating objects to go in the room. What is the "proper" way to set variables for that instance in my script. I know that I can do the following:

Code:
insID = instance_create(20, 20, obj);
insID.varA = 0;
instID.varB = 1;
etc.
However, for example, let's say my instantiated object creates a particle emitter in it's create event but I want to create that emitter using the variables varA and varB (which using the code above won't get set until after the create event has been run) - how do I do that?

Hope this makes sense - thanks for getting this far...
 

jo-thijs

Member
Hi and welcome to the GMC!

You're asking a good question for which there is no perfect answer as far as I know.
What I usually do is, create the emitters in the event user 0 event and create the instance like this:
Code:
with instance_create(20, 20, obj) {
    varA = 0;
    varB = 1;
    event_user(0);
}
Alternatively, you could also have some variable "justCreated" set to true in the create event and in the begin step event,
you could check if this variable is still true, create the emitters if so and set the variable to false afterwards.
 
G

Gaspode

Guest
Ah, haven't really investigated the user events yet, but that looks like a neat(ish) solution. Thanks for the reply ... and so quick too!
 
Last edited by a moderator:

Sabnock

Member
Hi, new to GM:S but absolutely loving it! Great forum here, learnt a ton in the last week and my first game is progressing well.

My question... I have a single room for my game part and dynamically instantiating objects to go in the room. What is the "proper" way to set variables for that instance in my script. I know that I can do the following:

Code:
insID = instance_create(20, 20, obj);
insID.varA = 0;
instID.varB = 1;
etc.
However, for example, let's say my instantiated object creates a particle emitter in it's create event but I want to create that emitter using the variables varA and varB (which using the code above won't get set until after the create event has been run) - how do I do that?

Hope this makes sense - thanks for getting this far...
@Gaspode
@Nocturne
@jo-thijs

i also have been giving this some thought.

I still think that it is a little counter intuitive for a new instance not to be able to have variables passed into it's create event from the instance that is creating it. (say that 5 times quickly lol)

anyway my work around i this.

i always have a room setup object that creates all my variables, arrays and kick starts things like my background tasks etc.

so in this i now create global.temp[10] = 0; // temp variables arrayarray.

this is probably way over complicated and another far elegant solution maybe available. i just haven't learnt it yet.

this allows me to do the following when creating new instances.

global.temp[0] = variable_1; global.temp[1] = variable_2; global.temp[2] = variable_3; ........ etc, etc...
instance_create(x,y, obj_whatever);

obj_whatever
Create event
shield_strength = gloabl.temp[0]; fuel = global.temp[1]; ammo_type = global.temp[2]; ..... etc, etc...
more code;

step_event
more code;

although this is a tad confusing at first it works and the passed variables are recognised and available to be used by the create event of the new instance.

i don't know about everybody else but i regularly need to pass variables from one instance to another and find it frustrating that create_instance() doesn't allow to pass said variables for immediate use by the new instances create event. rant over ;


ps, i love Terry Pratchetts books as well Gaspode
 
Last edited:
@Sabnock, that's not a bad idea, but if you're going to do it that way, you might as well write what I call a constructor script, which would look something like this:

Code:
/// textbox_create(x, y, text, name, avatar);

var x1 = argument[0],
    y1 = argument[1],
    txt = argument[2],
    nm = argument[3],
    av = argument[4];

var inst = instance_create(x1, y1, obj_textbox);

with (inst)
{
    text = txt;
    name = nm;
    avatar = av;
   
    textbox_init(); // This replaces the code that would be in the Create Event, and the Create Event is left blank
}

return (inst);
... then you just call it like so:

Code:
textbox_create(16, 16, "By golly!  I can TALK!", "Old Guy", spr_old_guy_avatar);
... saves a lot of typing in the end.
 
G

Gaspode

Guest
Thanks everyone, need to digest all that, but all good stuff.

Sabnock said:
ps, i love Terry Pratchetts books as well Gaspode
Ha, that darn dog cracks me up.
 

Sabnock

Member
Thanks everyone, need to digest all that, but all good stuff.


Ha, that darn dog cracks me up.
slightly off topic; i have been doing a vimes marathon and am currently 75% through snuff. which to be honest is hard work compared to the others. gaurds gaurds, feet of clay, fifth elephant, night watch and thud are fantastic
 
Last edited:

jo-thijs

Member
@stainedofmind, that's exactly what I was thinking when I read Sabnock's post.
The only thing I would do differently, is I would use argumentX instead of argumant[X], as GameMaker performs extra checks on the argument amount when using those.
I also wouldn't write them to temporary variables first, only to write them to the instance variables after.
Just directly assing argumentX to the instance variables.
 
Last edited:
G

Gaspode

Guest
@stainedofmind, now I've actually read and processed all this, I'm inclined to go with your solution plus @jo-thijs extras. I presume (I'm new to GM) that I could create a tabbed script for each of my objects containing the two scripts xxx_create and xxx_init named in a similar fashion, e.g.

obj_textbox (the textbox object)
scr_obj_textbox (containing textbox_create & textbox_init)

That, actually, seems fairly straight-forward and manageable.

I guess I could have all the _create and _init scripts in one tabbed script object, but the OCD part of me likes to organise...
 

Sabnock

Member
@stainedofmind, now I've actually read and processed all this, I'm inclined to go with your solution plus @jo-thijs extras. I presume (I'm new to GM) that I could create a tabbed script for each of my objects containing the two scripts xxx_create and xxx_init named in a similar fashion, e.g.

obj_textbox (the textbox object)
scr_obj_textbox (containing textbox_create & textbox_init)

That, actually, seems fairly straight-forward and manageable.

I guess I could have all the _create and _init scripts in one tabbed script object, but the OCD part of me likes to organise...

would be interest to see what you come up with.
 
Top