• 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!

Destroy then create after a delay a new object

  • Thread starter pierrelouis.gaucher@gmail
  • Start date
P

pierrelouis.gaucher@gmail

Guest
Hello everyone,

I'm trying to learn how to make a plateformer, and now i have a problem, i'm trying to make an object (a green cube) on which my player can jump : when he jumps on, my player is "launched" exactly the same way when we jump on an ennemy in mario. This is currently working in my program.
Now i want this : when my player jumps on the green cube, the green cube disapears for something like 5 seconds, and reapears.
I tryied to use the same thing that with my ennemies : instance_destroy but i think it's deleting the object of the room so it should have another way to do this but i don't know how :/
Here's a screen of my room
1589966242301.png

Sorry for my english I'm trying my best :)

Thank you all !
 

Nidoking

Member
Something has to exist to maintain the timer so you can create the new instance. A nice, simple way would be to create an object with no sprite, and only an alarm event that creates the new instance.
 

TsukaYuriko

☄️
Forum Staff
Moderator
When you destroy an instance, it is indeed removed from the room, as you figured. You therefore won't be able to run any code from that instance anymore. So destroying it is not an option - or at least not only destroying it.

There are multiple potential approaches here. You could either use the same instance, but run different code while it is gone (not drawing a sprite, not handling collisions, just counting down and then reappearing), or use an entirely different object with the sole purpose of counting down and then re-creating the enemy (as well as destroying itself).
 
D

Deleted member 45063

Guest
Take a look at The Alarm Event in objects, paired with the visible variable. This way when the player jumps on the block you can turn it invisible which retains the object in the game, it just doesn't draw it. You also then need to set the alarm to make the object visible again after a set time. Do note that since the object is still in the room you need to be careful not to trigger collisions with it while it is invisible, so you can either check for that explicitly or (if your collision code depends on the solid variable) make the block not solid when you set it to invisible, and vice-versa.

On another note, you might not want your email address to be publicly available to everyone on the forum, so consider contacting a moderator for changing your username.
 

TheouAegis

Member
sprite_index=-1;

Hides the instance and prevents any collision checks. It is still visible, so it can still draw things if you want. Setting visible to 0 might be overkill, bit it would prevent that also.
 
P

pierrelouis.gaucher@gmail

Guest
Ok I think i know how i can do this :
When my player jumps on the green cube, in the step of the green cube I write a code which asks him to draw a sprite with nothing or a blink between the green cube and nothing, I also ask him to forget every collisions for something like 5 seconds, then he reappears ?
It should have a function which can return a value true or false after x seconds ?
 
P

pierrelouis.gaucher@gmail

Guest
Take a look at The Alarm Event in objects, paired with the visible variable. This way when the player jumps on the block you can turn it invisible which retains the object in the game, it just doesn't draw it. You also then need to set the alarm to make the object visible again after a set time. Do note that since the object is still in the room you need to be careful not to trigger collisions with it while it is invisible, so you can either check for that explicitly or (if your collision code depends on the solid variable) make the block not solid when you set it to invisible, and vice-versa.

On another note, you might not want your email address to be publicly available to everyone on the forum, so consider contacting a moderator for changing your username.
OK I'm going to search this way

sprite_index=-1;

Hides the instance and prevents any collision checks. It is still visible, so it can still draw things if you want. Setting visible to 0 might be overkill, bit it would prevent that also.
what's the utility of sprite_index ? I mean, how does this works ?
 

TheouAegis

Member
It's the sprite assigned to the instance. It doesn't work per se, it just allows the collision functions to work. It's basically the visible bounds, whereas mask_index is the invisible bounds. If spritw_index is -1, the instance is spatially non-existent. It's like God -- you can't see it nor interact with it, but it is still there doing whatever it feels like doing.
 
P

pierrelouis.gaucher@gmail

Guest
It's the sprite assigned to the instance. It doesn't work per se, it just allows the collision functions to work. It's basically the visible bounds, whereas mask_index is the invisible bounds. If spritw_index is -1, the instance is spatially non-existent. It's like God -- you can't see it nor interact with it, but it is still there doing whatever it feels like doing.
Ok so if I understand correctly I must use this with alarms ! I'll try, thank you all
 

Yal

🐧 *penguin noises*
GMC Elder
I personally prefer the other approach... have the instance create a special object "DeadPlatform" at its position when destroyed; this dead platform object waits 5 seconds (using an alarm), and then creates a new destructible platform at its position, and then destroys itself. So there's a cycle of life where the object destroys itself, creates a placeholder, which then recreates the new platform. The placeholder can delay itself by checking if the spot is free before respawning, and create particle effects a little while before actually spawning in (to warn the player)... the sky's the limit.

But... you know what's even more cool? The dead placeholder doesn't need to spawn in the green platform specifically, you could give it a my_object variable and let it spawn anything! This lets you reuse the placeholder for respawning enemies, respawning items, and so on, and you don't need to code the same countdown logic in 100 different objects. Recyling is good for the environment, and also lets you make games faster.
 
P

pierrelouis.gaucher@gmail

Guest
I personally prefer the other approach... have the instance create a special object "DeadPlatform" at its position when destroyed; this dead platform object waits 5 seconds (using an alarm), and then creates a new destructible platform at its position, and then destroys itself. So there's a cycle of life where the object destroys itself, creates a placeholder, which then recreates the new platform. The placeholder can delay itself by checking if the spot is free before respawning, and create particle effects a little while before actually spawning in (to warn the player)... the sky's the limit.

But... you know what's even more cool? The dead placeholder doesn't need to spawn in the green platform specifically, you could give it a my_object variable and let it spawn anything! This lets you reuse the placeholder for respawning enemies, respawning items, and so on, and you don't need to code the same countdown logic in 100 different objects. Recyling is good for the environment, and also lets you make games faster.
It looks good :) maybe more difficult than using sprite_index = -1 but probably more useful.
Currently I'm a beginner in GMS and i don't know yet how to use every functions but I'll try with your idea when I'll use correctly the alarms, thanks
 
Top