• Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!

Variables at create

66Gramms

Member
Hello!
I'm making a game and on a few levels I want survival mode. For this i made a spawner object that has 2 variables
1. Enemy type //Which enemy to spawn
2. Delay //Delay beetwen spawns
When the player enter a level the level is being called from a script (the script spawns all the objects) and there when i create the spawner i use "with"
(see the exact script)
Code:
obj_2587=instance_create(960,555,obj_spawner)  with obj_2587 {enemType = obj_mainEnemy; delay = 10*room_speed; alarm[0] = delay; instance_create(x,y,obj_mainEnemy);}
When i run the game it creates the first enemy instantly (just as i want) but after 10 seconds I get this error
Code:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Alarm Event for alarm 0
for object obj_spawner:

Variable obj_spawner.enemyType(100534, -2147483648) not set before reading it.
 at gml_Object_obj_spawner_ObjAlarm0_1 (line 2) -     instance_create(x,y,enemyType);
############################################################################################
In the alarm 0 event i have this code
Code:
///Spawning enemies
    instance_create(x,y,enemyType);
    alarm[0] = delay;
Also originally i had these vars set up in the create event but it overwrote what i gave in the script so now the create event of the spawning object is empty. (all it has is "visible = false") I want to use this object on different levels and with different values. How could i solve this? Thank you in advance
 

cidwel

Member
I'm using some thingy I learn here looking for the same problem. I found my answer here: https://yal.cc/gamemaker-create-event-arguments/
This is from a cool guy here in the GMC. I just used his instance_create_v script and changed some cosmetics for fitting my needs.

Create this script: instance_create_pre
Code:
/// instance_create_pre(x, y, layer, object, ...args)
var n = argument_count - 4;
game.create_arg = undefined;
ct_count = n;
for (var i = 0; i < n; i++) game.create_arg[i] = argument[4 + i];
var r = instance_create_layer(argument[0], argument[1], argument[2], argument[3]);
game.create_arg = undefined;
ct_count = undefined;
return r;
Then you create instances that need to pass variables like this:
Code:
instance_create_pre(x,y,"layer_name",object, arg0, arg1...argN);
In my script you can pass 4 variables. You can extend easily to be able to pass more variables, or just pass a map with all the variables you need

and inside the create event you retrieve all the added arguments
Code:
thingie0 = game.create_arg[0];
thingie1 = game.create_arg[1];
This way you can pass variables to newly created objects and have access to them in the create script

pd: "game" is a singleton object I'm using as first instance in all my rooms that keeps constants, options and these kind of things. Probably you can do the same with constants (if there still exists in gm, dunno)

It is not elegant, but works like a charm. I do not need nothing more.
 
Last edited:

66Gramms

Member
I'm using some thingy I learn here looking for the same problem. I found my answer here: https://yal.cc/gamemaker-create-event-arguments/
This is from a cool guy here in the GMC. I just used his instance_create_v script and changed some cosmetics for fitting my needs.

Create this script: instance_create_pre
Code:
/// instance_create_pre(x, y, layer, object, ...args)
var n = argument_count - 4;
game.create_arg = undefined;
ct_count = n;
for (var i = 0; i < n; i++) game.create_arg[i] = argument[4 + i];
var r = instance_create_layer(argument[0], argument[1], argument[2], argument[3]);
game.create_arg = undefined;
ct_count = undefined;
return r;
Then you create instances that need to pass variables like this:
Code:
instance_create_pre(x,y,"layer_name",object, arg0, arg1...argN);
In my script you can pass 4 variables. You can extend easily to be able to pass more variables, or just pass a map with all the variables you need

and inside the create event you retrieve all the added arguments
Code:
thingie0 = game.create_arg[0];
thingie1 = game.create_arg[1];
This way you can pass variables to newly created objects and have access to them in the create script

pd: "game" is a singleton object I'm using as first instance in all my rooms that keeps constants, options and these kind of things. Probably you can do the same with constants (if there still exists in gm, dunno)

It is not elegant, but works like a charm. I do not need nothing more.
I'm using GMS and as i saw instance_create_layer is for gm2 and i'M not sure what is it trying to be.
 
Top