• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

using with() statement: what am i doing wrong?!

S

shui

Guest
Okay so this is driving me insane. It feels like sometimes with() works exactly how I think it should and other times I endlessly get errors while testing. So, here is the create event of the object explosionController I am trying to use with..with:

Code:
///Boost SFX Init
target = noone

alarm[0] = room_speed
x = target.x
y = target.y

size = (target.sprite_width/6 + target.sprite_height/6)
system=part_system_create();
part_system_depth(system,10);

part=part_type_create();
part_type_shape(part,pt_shape_ring);
part_type_speed(part,0,0,0,0);
part_type_colour3(part,c_white,c_ltgray,c_gray);
part_type_life(part,room_speed*3,room_speed*3);
part_type_size(part,1,1.1,.1,0);
part_type_blend(part,true)
part_type_alpha3(part, 1,.5,0);

part5=part_type_create();

part_type_shape(part5,pt_shape_smoke);
part_type_speed(part5,.1,5,.1,.1);
part_type_colour3(part5,c_purple,c_white,c_white);
part_type_life(part5,room_speed*3,room_speed*3);
part_type_size(part5,2,4,-.05,0);
part_type_blend(part5,true)
part_type_alpha3(part5, .7,.4,0);
part_type_orientation( part5, irandom(360), irandom(360), 1, .1, true );

for(i=0; i<5; i++)
{
with(instance_create(target.x,target.y,debris))
target = other.id
}

Len = 1

     var _dir=0;
    var _dist=0;
   
    var _x=target.x
   
    var _y=target.y
    var _part_dir=point_direction(_x,_y,target.x,target.y);
   
    //
    var _dir2=random(120);
    var _dist2=30;
   
    var _x3=target.x+10+lengthdir_x(_dist,_dir);
   
    var _y3=target.y+lengthdir_y(_dist,_dir);
   
 
   
    var _part_dir3=choose(irandom(360))//point_direction(_x,_y,target.x,target.y);
    var _part_dir4=choose(irandom(360))
   
    part_type_direction(part5,_part_dir3,_part_dir3,0,0);
    part_particles_create(system,_x3,_y3,part5,3);
   
     part_type_direction(part5,_part_dir4,_part_dir4,0,0);
     part_particles_create(system,_x3,_y3,part5,3);

   
   
    //
   
   
    //part 1
    part_type_direction(part,target.image_angle,target.image_angle,0,0);
    part_particles_create(system,_x,_y,part,1);
Now I have this object set to spawn in a collision event with my enemies parent object. In the parents collision code I have:

Code:
i = instance_create(x,y,explosionController)

with(i)
{
target = obj_enemy1 //this was set to other.id but didn't work either
}
Alright.. so I run the game and shoot my enemy, and get:

___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Create Event
for object explosionController:

Unable to find any instance for object index '-4' name '<undefined>'
at gml_Object_explosionController_CreateEvent_1 (line 5) - x = target.x
############################################################################################

What am I missing here?
 
S

sndfnts

Guest
Doesn't instance_create immediately run the created object's Create code? It isn't possible to execute code before the create event.

you could try moving the Create code in a separate script (like explosionController_initialize) and execute the script in the collision event
Code:
///explosionController_initialize(id, tgt)
//this script is used for initializing the explosionController after it is created
var argument0 = id
var argument1 = tgt
with (id)
{
    target = argument1
   //...the rest of your create event
}
Code:
//in your collision event
i = instance_create(x,y,explosionController)
explosionController_initialize(i, obj_Enemy1)
 
I

icuurd12b42

Guest
move

x = target.x
y = target.y

to the place you create the instance


with(i)
{
target =other.id;

x = target.x
y = target.y
}
 
S

shui

Guest
Doesn't instance_create immediately run the created object's Create code? It isn't possible to execute code before the create event.

you could try moving the Create code in a separate script (like explosionController_initialize) and execute the script in the collision event
Code:
///explosionController_initialize(id, tgt)
//this script is used for initializing the explosionController after it is created
var argument0 = id
var argument1 = tgt
with (id)
{
    target = argument1
   //...the rest of your create event
}
Code:
//in your collision event
i = instance_create(x,y,explosionController)
explosionController_initialize(i, obj_Enemy1)
That's what I wasn't sure about. I thought using with( would supersede the original creation event but apparently that isn't the case.

move

x = target.x
y = target.y

to the place you create the instance


with(i)
{
target =other.id;

x = target.x
y = target.y
}
When I did this the error went away, but what it does instead is spit out an error to any other reference to 'target' in the create event. I haven't tested it yet but I probably will have to do like sndfnts said

and jam it all into a script for my own sake and modularity

Isn't the error because you set target=noone right before you use x=target.x? Looks like that is the cause to me.
It might? I tried about a dozen different ways of arranging things and it still didn't take, but its possible. I was thinking I could leave target empty and just pass it in with with() but that doesn't look to be the case if the variable is referenced in the create event as well.

Anyway I will try this and report back!
 
I

icuurd12b42

Guest
I did not realize you have another reference...
the create event RAN. you cant change a variable (target) in the create event AFTER it ran. so you cant use variable you modify externally there... the code ran with noone and broke because noone.x is not valid

add an alarm1 event
change the create code to
Code:
///Boost SFX Init
target = noone
alarm[0] = room_speed
alarm[1] = 1;
and copy the rest to the alarm1
Code:
x = target.x
y = target.y

size = (target.sprite_width/6 + target.sprite_height/6)
system=part_system_create();
part_system_depth(system,10);

part=part_type_create();
part_type_shape(part,pt_shape_ring);
part_type_speed(part,0,0,0,0);
part_type_colour3(part,c_white,c_ltgray,c_gray);
part_type_life(part,room_speed*3,room_speed*3);
part_type_size(part,1,1.1,.1,0);
part_type_blend(part,true)
part_type_alpha3(part, 1,.5,0);

part5=part_type_create();

part_type_shape(part5,pt_shape_smoke);
part_type_speed(part5,.1,5,.1,.1);
part_type_colour3(part5,c_purple,c_white,c_white);
part_type_life(part5,room_speed*3,room_speed*3);
part_type_size(part5,2,4,-.05,0);
part_type_blend(part5,true)
part_type_alpha3(part5, .7,.4,0);
part_type_orientation( part5, irandom(360), irandom(360), 1, .1, true );

for(i=0; i<5; i++)
{
with(instance_create(target.x,target.y,debris))
target = other.id
}

Len = 1

     var _dir=0;
    var _dist=0;

    var _x=target.x

    var _y=target.y
    var _part_dir=point_direction(_x,_y,target.x,target.y);

    //
    var _dir2=random(120);
    var _dist2=30;

    var _x3=target.x+10+lengthdir_x(_dist,_dir);

    var _y3=target.y+lengthdir_y(_dist,_dir);



    var _part_dir3=choose(irandom(360))//point_direction(_x,_y,target.x,target.y);
    var _part_dir4=choose(irandom(360))

    part_type_direction(part5,_part_dir3,_part_dir3,0,0);
    part_particles_create(system,_x3,_y3,part5,3);

     part_type_direction(part5,_part_dir4,_part_dir4,0,0);
     part_particles_create(system,_x3,_y3,part5,3);



    //


    //part 1
    part_type_direction(part,target.image_angle,target.image_angle,0,0);
    part_particles_create(system,_x,_y,part,1);
that way your
instance_create will create the instance and do nothing yet but set the alarm 0 and 1
then your
Code:
i = instance_create(x,y,explosionController)
with(i)
{
target =other.id;
}
will set the target

then the alarm1 will trigger, finalizing the setup

another simpler method is to use a global



in your create, keep the original code you had but change
Code:
///Boost SFX Init
target = noone
...,
to
Code:
///Boost SFX Init
target = global.TARGET;
...
and change this
Code:
i = instance_create(x,y,explosionController)
with(i)
{
target = obj_enemy1 //this was set to other.id but didn't work either
}
to
Code:
global.TARGET = id;
instance_create(x,y,explosionController)
 
S

shui

Guest
Thanks a ton! That method works as well as making it a script. this will help me out a ton in the future
 
Top