GMS 2 NOT creating instances

S

s0ngbirdd

Guest
I have a problem with creating intances of an object.
I need to move instance to the wall (after key released) then this instance should destroy. After destroing when i realesed a key (again) it shoul be placed into its first position and then repeat this action again. The problem is in not creating this instance.
 

Attachments

PlayerOne

Member
Hello and welcome to the forums.

1) Do not post screenshots here. Copy and paste the code using code brackets so that people can help you.

2) when using the with function you are telling the game to create an object with obj_spear when move==2. Since you keep destroying obj_spear and move==2, nothing is being created.

In this instance you would use:

Code:
//STEP:
if !instance_exists(obj_spear) // <<< If instance is not in room
{
instance_create_layer(xpr,ypr,"Instances",obj_spear) // <<< Create instance at layer...
}
 

TheouAegis

Member
keyboard_check_released() takes a keycode or virtual key constant. keyboard_check_released(1) is not proper use.

instance_destroy(other) is only used in collision events or within the scope of with(). Your spear's step event has neither.
 
S

s0ngbirdd

Guest
Hello and welcome to the forums.

1) Do not post screenshots here. Copy and paste the code using code brackets so that people can help you.

2) when using the with function you are telling the game to create an object with obj_spear when move==2. Since you keep destroying obj_spear and move==2, nothing is being created.

In this instance you would use:

Code:
//STEP:
if !instance_exists(obj_spear) // <<< If instance is not in room
{
instance_create_layer(xpr,ypr,"Instances",obj_spear) // <<< Create instance at layer...
}



i tried your code but the errot appeared (so, it cant find any instance when i destroyed it):
############################################################################################
FATAL ERROR in
action number 1
of Step Event0
for object obj_game:

Unable to find any instance for object index '1' name 'obj_spear'
at gml_Object_obj_game_Step_0 (line 34) - instance_create_layer(obj_spear.xpr,obj_spear.ypr,"Instances",obj_spear) // <<< Create instance at layer...
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_game_Step_0 (line 34)
 
S

s0ngbirdd

Guest
so, i have an object obj_spear :

create event :

move = 1
xpr = xstart; //position x
ypr = ystart; //position y

step event:

if keyboard_check_released(1) && move == 1 {
while !place_meeting(x, y + 64, obj_wall){
y += 64;
move = 0;
}
}
if move == 0 {
instance_destroy() ;
}

and i have an object obj_game (without sprite(it is transparent)) :

step event:

if !instance_exists(obj_spear) // <<< If instance is not in room
{
instance_create_layer(obj_spear.xpr,obj_spear.ypr,"Instances",obj_spear) // <<< Create instance at layer...
obj_spear.move = 1;
}
 
S

s0ngbirdd

Guest
I believe there is more going on. please post the entire code in code brackets so we may help you better.
so, i have an object obj_spear :

create event :

move = 1
xpr = xstart; //position x
ypr = ystart; //position y

step event:

if keyboard_check_released(1) && move == 1 {
while !place_meeting(x, y + 64, obj_wall){
y += 64;
move = 0;
}
}
if move == 0 {
instance_destroy() ;
}

and i have an object obj_game (without sprite(it is transparent)) :

step event:

if !instance_exists(obj_spear) // <<< If instance is not in room
{
instance_create_layer(obj_spear.xpr,obj_spear.ypr,"Instances",obj_spear) // <<< Create instance at layer...
obj_spear.move = 1;
}
 

PlayerOne

Member
I see. The reason you keep getting that error is that your calling from xpr and ypr from obj_spear when attempting to create it. Because the object is not in the room - making xpr and ypr non-existent aswell - the game will crash. For this you will need to modify the following:


Code:
obj_game

CREATE:
_xpr = obj_spear.xstart // <<< Gets spear x postion when spwaned
_ypr = obj_spear.ystart // <<< Gets spear y postion when spwaned



if !instance_exists(obj_spear) // <<< If instance is not in room
{
instance_create_layer(_xpr,_ypr,"Instances",obj_spear) // <<< Create instance at layer...
obj_spear.move = 1;
}
also change the keyboard_check to vk_ or ord(" ").
 
S

s0ngbirdd

Guest
I see. The reason you keep getting that error is that your calling from xpr and ypr from obj_spear when attempting to create it. Because the object is not in the room - making xpr and ypr non-existent aswell - the game will crash. For this you will need to modify the following:


Code:
obj_game

CREATE:
_xpr = obj_spear.xstart // <<< Gets spear x postion when spwaned
_ypr = obj_spear.ystart // <<< Gets spear y postion when spwaned



if !instance_exists(obj_spear) // <<< If instance is not in room
{
instance_create_layer(_xpr,_ypr,"Instances",obj_spear) // <<< Create instance at layer...
obj_spear.move = 1;
}
also change the keyboard_check to vk_ or ord(" ").

Sooo, now i have another issue : the instance of obj_spear is not disappearing (but it should), it should move toward wall(after key released), disappear,and then after (again key released) appeared and do the same thing again.
Here is the code of obj_spear :

create event :

move = 1;

step event :

if keyboard_check_released(vk_anykey) && move == 1 {
while !place_meeting(x, y + 64, obj_wall){
y += 64;
move = 0;
}
}


if move == 0 {
instance_destroy();
}

And here is the code of obj_game :

create event :

xpr = obj_spear.xstart;
ypr = obj_spear.ystart;

step_event :

if keyboard_check_released(vk_anykey) && !instance_exists(obj_spear)
{
instance_create_layer(xpr,ypr,"Instances",obj_spear);
obj_spear.move = 1;
}



 

TheouAegis

Member
if keyboard_check_released(vk_anykey) && move == 1 {
while !place_meeting(x, y + 64, obj_wall){
y += 64;
move = 0;
}
}


if move == 0 {
instance_destroy();
}
This code here is odd. It says when any key is released, snap the spear to the nearest wall below it then destroy it. So there are four problems here: 1) releasing any key will destroy the spear immediately, so there is no point snapping it to the nearest wall below it; 2) the spear can only be destroyed when any key is released, so if no key is released, the spear will never be destroyed; 3) there is no code to actually move the spear each step, so the spear never actually moves; 4) if there is no obj_wall beneath the spear, that pointless while loop will freeze the program.

So describe very clearly what you want to actually happen with this spear. As it is, your code in the spear doesn't appear to do anything useful at all.
 
S

s0ngbirdd

Guest
This code here is odd. It says when any key is released, snap the spear to the nearest wall below it then destroy it. So there are four problems here: 1) releasing any key will destroy the spear immediately, so there is no point snapping it to the nearest wall below it; 2) the spear can only be destroyed when any key is released, so if no key is released, the spear will never be destroyed; 3) there is no code to actually move the spear each step, so the spear never actually moves; 4) if there is no obj_wall beneath the spear, that pointless while loop will freeze the program.

So describe very clearly what you want to actually happen with this spear. As it is, your code in the spear doesn't appear to do anything useful at all.
so, how can i make a pause between destroying and creating of spear instance?
 

TheouAegis

Member
Creating the instance probably isn't a problem. What's probably going on is the spirit never moving so it just deletes itself and then a new one is recreated right where is that. For starters if you want the spear to actually move, you need to tell it to move. You do not use while loops to do that. If you want the spear to move 64 pixels at a time every few steps, then you can use an alarm, when it ticks down move the spear 64 pixels and then check for a collision with a wall; if there is no wall, reset the alarm. The spear will stay where it is at if it hits a wall.

Code:
if keyboard_check_released(vk_anykey) && move == 1 {
while !place_meeting(x, y + 64, obj_wall){
y += 64;
move = 0;
}
}
This part is now logical, kinda. Snap the spear to the wall below it and tell obj_game you can destroy that spear. Let obj_game destroy the spear when it creates the new one.
 
S

s0ngbirdd

Guest
Creating the instance probably isn't a problem. What's probably going on is the spirit never moving so it just deletes itself and then a new one is recreated right where is that. For starters if you want the spear to actually move, you need to tell it to move. You do not use while loops to do that. If you want the spear to move 64 pixels at a time every few steps, then you can use an alarm, when it ticks down move the spear 64 pixels and then check for a collision with a wall; if there is no wall, reset the alarm. The spear will stay where it is at if it hits a wall.

Code:
if keyboard_check_released(vk_anykey) && move == 1 {
while !place_meeting(x, y + 64, obj_wall){
y += 64;
move = 0;
}
}
This part is now logical, kinda. Snap the spear to the wall below it and tell obj_game you can destroy that spear. Let obj_game destroy the spear when it creates the new one.
THANK YOU VERY MUCH!!! Alarm really helped with my problem))
 
S

s0ngbirdd

Guest
Creating the instance probably isn't a problem. What's probably going on is the spirit never moving so it just deletes itself and then a new one is recreated right where is that. For starters if you want the spear to actually move, you need to tell it to move. You do not use while loops to do that. If you want the spear to move 64 pixels at a time every few steps, then you can use an alarm, when it ticks down move the spear 64 pixels and then check for a collision with a wall; if there is no wall, reset the alarm. The spear will stay where it is at if it hits a wall.

Code:
if keyboard_check_released(vk_anykey) && move == 1 {
while !place_meeting(x, y + 64, obj_wall){
y += 64;
move = 0;
}
}
This part is now logical, kinda. Snap the spear to the wall below it and tell obj_game you can destroy that spear. Let obj_game destroy the spear when it creates the new one.
but there is one mini-issue:

when i create more then 1 instances in room works only 1rst (others have just destroyed after key released)

code:

obj_spear:

create event:

move = 0;

step event:

if keyboard_check_released(1) && instance_exists(obj_spear) && move == 0{
while !place_meeting(x,y + 64, obj_wall){
if place_meeting(x, y + 64, obj_player){
game_restart()
}
y += 64;
}
move = 1;
}

if move = 1 {
alarm[0] = 1;
}

alarm[0] event:

instance_destroy(obj_spear);
move = 0;

obj_game:

create event:

xpr = obj_spear.xstart;
ypr = obj_spear.ystart;

step event:

if keyboard_check_released(1) && !instance_exists(obj_spear)
{
instance_create_layer(xpr,ypr,"Instances",obj_spear);
}
 
Top