inst = instance_create(x,y,obj_anything)

Sabnock

Member
another noob question,

whenever I use

inst = instance_create(x,y,obj_anything)
inst.shield= shield (or any variable you want to pass to the instance you are creating)


the variable is not available in the create event of the newly created instance and the only way I seem to be able to get it to work as I want is

inst = instance_create(x,y,obj_anything)
inst.a_variable = shield (or any variable you want to pass to the instance you are creating)


obj_anything
create event
alarm[0] = 1;

alarm[0] event
if shield == code here;

step event
more code;


what am I doing worng?
 

Roderick

Member
the variable is not available in the create event of the newly created instance
Correct. The create event runs immediately on creation, before you get a chance to pass variables.

What exactly are you trying to do? There might be a better way of doing it.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin

Sabnock

Member
When you create an instance, the create event will be called before the rst of the code will continue... so it's:
Code:
inst = instance_create(x,y,obj_anything);
<perform create event of the instance just created>
inst.shield= shield; <--- this will be added after the create event of the new instance is run
The manual does say this ;) ... http://docs.yoyogames.com/source/dadiospice/002_reference/objects and instances/instances/instance functions/instance_create.html
Hi Nocturne, funnily enough I had read that but forgot it :D

what would be nice is

create_instance (x, y, obj_anything, argument[1], argument[2],........);

so the way I am doing it currently is the only reliable way to pass and initialise variables from an instance into a new instance?

tbh it's working fine I only asked to make just to make sure i'm doing things correctly rather than bullying GMS to do it the way I want!
 
Last edited:

Yal

šŸ§ *penguin noises*
GMC Elder
so the way I am doing it currently is the only reliable way to pass and initialise variables from an instance into a new instance?
You could always do it like this:
Code:
with(instance_create(x,y,obj_something)){
  variable1 = value;
  variable2 = someothervalue + 15;
  event_user(0);
}
...and then you put code in the objects Other--->User Defined 0 event that does things depending on the value of the shield variable. Since you decide when to execute the user defined event, you just run it AFTER setting all variables you need, and you'll have access to them. It means you have to split up the code a bit, but at least you've got full control this way.
 

Sabnock

Member
You could always do it like this:
Code:
with(instance_create(x,y,obj_something)){
  variable1 = value;
  variable2 = someothervalue + 15;
  event_user(0);
}
...and then you put code in the objects Other--->User Defined 0 event that does things depending on the value of the shield variable. Since you decide when to execute the user defined event, you just run it AFTER setting all variables you need, and you'll have access to them. It means you have to split up the code a bit, but at least you've got full control this way.
Thanks Yal,

I will give that a go. I've not touched on user defined events yet so will be something to play with later.
 

ZeDuval

Member
I'm not 100% sure what you want and what way exactly, but here you can see several possibilities, pick the things you like. ;)


  • Define all variables in the create event initially:
Code:
/// create_event
var1 = 0;
var2 = 0;
var3 = "";
var4 = undefined;
Code:
// draw_event
draw_text(var1,var2,var3);
if !is_undefined(var4){var1++;var2++;} // or if var4!=undefined{...}
This way, GM won't throw an error if you use var1(real),var2(real),var3(string) and var4(undefined) in the draw_event from the start and create the instance like this:
Code:
var inst = instance_create(x,y,objABC);
inst.var1 = 32;
inst.var2 = 64;
inst.var3 = "my name is objABC, deal with it";
inst.var4 = "initialized";
...or...
Code:
with instance_create(x,y,objABC){
  var1 = 32;
  var2 = 64;
  var3 = "my name is objABC, deal with it";
  var4 = "initialized";
  other.inst = id;
}


  • Omit the create event (almost) completely:
Code:
/// create_event
init = false;
Code:
/// draw_event
if init{
  draw_text(var1,var2,var3);
}
Code:
with instance_create(x,y,objABC){
  var1 = 32;
  var2 = 64;
  var3 = "my name is objABC, deal with it";
  init = true;
  other.inst = id;
}


  • Use a way utilizing the user_events like suggested above by others, alone with this there are multiple ways...


  • Directly refering to...
    create_instance (x, y, obj_anything, argument[1], argument[2],........);
Create a script:
Code:
/// create_objABC(x,y,var1,var2,var3)
with instance_create(argument0,argument1,objABC){
  var1 = argument2;
  var2 = argument3;
  var3 = argument4;
  return id;
}
Use a script:
Code:
inst = create_objABC(128,256,32,64,"my name is objABC, deal with it");
Note: This here doesn't solve the errors with undefined variables, look above for that. This here should only show you how you could write a constructor-script to get exactly the syntax you wanted.

Really, the possibilities are endless. I think it's quite fun to find out what works and what doesn't and to always come up with just another idea. ;) Have fun!


edit: I forgot one thing: If you feel like ever using the YCC-compiler, best start defining all variables(0/""/undefined/...) in the create_event now.
 

chamaeleon

Member
I am sure most of the replies cover what you need and/or what can be considered best practices. Unless my eyesight is failing me I didn't see anyone recommending writing create/initialization scripts that take arguments, wrapping up the construction of your objects for you.

Alternatively, you could initialize a global variable with the desired value before calling instance_create and read it inside the create event.. I do not recommend this approach though. It feels icky. :)
 

Sabnock

Member
thanks all there is some really good stuff for me to try out there.

might have to go back through my code and rewrite some :D
 

Sabnock

Member
@Gaspode
@Nocturne
@jo-thijs

i 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 ;
 

TheouAegis

Member
The create event is intended to allocate memory for any variables the object will need and clear any shared variables/arrays. The instance is the one using the object, so it makes sense that you'd need to do stuff in the User Event if you think of it that way. The Create Event isn't for instances, it's for objects. If you want an instance value to be available upon the object's creation, then store the values in a global array and use a global counter to tell the object which index in the arrays to use.

No, I didn't just pull that out of my ass.
 
T

Toxicosis

Guest
I dealt with something similar myself.

My solution was creating a different object, obj_holdval, first. Then it would pass the values to obj_holdval. On the create event, it'd use a with loop, look for obj_holdval, and take the values from there. Finally, it'd destroy obj_holdval.

Code:
instance_create(0,0,obj_holdval);
with obj_holdval
{
xval = x;
yval = y;
}
instance_create (x+a,y+b,obj_anything);
with obj_holdval instance_destroy();

//CREATE EVENT:
var _xval, _yval;
with obj_holdval
{
_xval = xval;
_yval = yval;
}
x = xval;
y= yval;
In hindsight, a ds_something would do the job a bit better, though...
 
Top