Player Not Respawning Correctly - Space Rocks Tutorial

C

CodeD

Guest
SOLVED!

Hello,

I've been progressing through the Space Rocks tutorial in order to familiarize myself with inherent GameMaker functions.

For those who may be unfamiliar with this tutorial game: The base game is about just creating a spaceship which tries to shoot and dodge asteroids while trying to rack up 1000 points to win. There is another set of videos after the primary tutorial which enhances the base game with factions, enemies, parallax, powerups, etc...

My Issue:
Moving on, the game worked as intended when I completed the basic tutorials. However, when I moved onto the follow-up tutorials and added in factions the respawn code that I had set up in a persistent game object no longer works. If the player is hit by an asteroid, they are destroyed but the room does not restart after a small delay as intended (it doesn't restart period to clarify).

I am unsure about what happened because I haven't changed anything that the tutorial did not as well [aside from minor variations to scripts to match 2.3 format changes].

What I have tried:

- Combing through the forums to find this issue or similar issues like a good noodle : )
No exact results, closest was an issue with the base-game respawn that I already solved
- Checking to make sure there is only one player in the room
There is only 1
- Checking to make sure that the game object (with the respawn code is in the room)
I have the game object set to persistent, so yes it is
- Making sure, to the best of my knowledge, that everything is executed in the correct order
I'm pretty sure it is, but maybe there's something I don't see
- Moving the room_restart(); code to the ship object's destroy event instead of the game object's switch statement
Almost solved it, but the delay that I need (to make sure the debris has time to display) gets bypassed, restarting the room instantaneously.
I tried using an alarm here after instance_destroy(), set it to the room speed, then put the room_restart() code into the alarm event
However, I'd rather keep this code in the game object if I can just because it isn't getting destroyed like the ship

I feel like I'm running around in circles here so any help would be greatly appreciated!

The Code:

Here are what I believe to be the relevant blocks of code [Gamemaker Version 2.3.1.542 - Written in GML]

**This is the step event for an object titled "obj_game", which is present in all rooms. Note the last few lines of code which deal with the respawn \/
GML:
if(keyboard_check_pressed(vk_enter)){
    switch(room){
        case rm_start:
            room_goto(rm_game);
            break;
           
        case rm_win:
        case rm_gameover:
            game_restart();
            break;
    }
}

if(room == rm_game){
    if(score>=1000){
        audio_stop_sound(msc_space_rocks_theme)
        room_goto(rm_win);
        audio_stop_sound(msc_space_rocks_theme)
        audio_play_sound(msc_win,1,false);
    }

    if(lives <= 0){
        audio_stop_sound(msc_space_rocks_theme)
        room_goto(rm_gameover);
        audio_play_sound(msc_loss,1,false);
    }

    if((!object_exists(obj_ship))and(lives>0)){
            alarm[2] = 3;
    }
}
**This is the what is in Alarm[2] event of obj_game \/
GML:
room_restart();
**This is a collision event regarding obj_faction for obj_ship \/
GML:
//Check if obj is a member of our faction

if(other.faction == faction) exit;
event_perform(ev_other,ev_user1);
**Finally, this is the user event called "Take Damage" of the ship which destroys it. \/
GML:
/// @description Take Damage

instance_destroy();
audio_play_sound(msc_explosion,1,false);

repeat(10){
instance_create_layer(x,y,"Instances",obj_debris);
}

lives = lives - 1;

Once again, any help would be much appreciated! Also, if anyone has suggestions on post formatting feel free to let me know because this is my first post on the site.
 
Last edited by a moderator:

Nidoking

Member
if((!object_exists(obj_ship))and(lives>0)){ alarm[2] = 3; }
3 means that the alarm will go off in three steps, correct? Walk through this with me. One step later, does an instance of obj_ship exist? Is lives greater than zero? Well, nothing has presumably happened to change either condition, so we set the alarm value to 3. It will go off in three steps.
One step later, does an instance of obj_ship exist? Is lives greater than zero? Well, nothing has presumably happened to change either condition, so we set the alarm value to 3. It will go off in three steps.
One step later, does an instance of obj_ship exist? Is lives greater than zero? Well, nothing has presumably happened to change either condition, so we set the alarm value to 3. It will go off in three steps.
One step later, does an instance of obj_ship exist? Is lives greater than zero? Well, nothing has presumably happened to change either condition, so we set the alarm value to 3. It will go off in three steps.
One step later, does an instance of obj_ship exist? Is lives greater than zero? Well, nothing has presumably happened to change either condition, so we set the alarm value to 3. It will go off in three steps.
One step later, does an instance of obj_ship exist? Is lives greater than zero? Well, nothing has presumably happened to change either condition, so we set the alarm value to 3. It will go off in three steps.
One step later, does an instance of obj_ship exist? Is lives greater than zero? Well, nothing has presumably happened to change either condition, so we set the alarm value to 3. It will go off in three steps.

When do you think the alarm will actually go off?
 

Yal

šŸ§ *penguin noises*
GMC Elder
if((!object_exists(obj_ship))and(lives>0)){
alarm[2] = 3;
}
Here's the issue: this will be run every step when the ship is destroyed, so the alarm keeps getting reset to 3 steps every step... never reaching 0, and thus never respawning the player.

Also, object_exists checks if the resource type exists, which is always true.

Add in a case to make sure the alarm only is set up when it's not running, and correct the function:
GML:
if (!instance_exists(obj_ship) && lives > 0 && !alarm[2]) {
  alarm[2] = 3
}
 
C

CodeD

Guest
\/ Solution \/
Add in a case to make sure the alarm only is set up when it's not running, and correct the function:
GML:
if (!instance_exists(obj_ship) && lives > 0 && !alarm[2]) {
  alarm[2] = 3
}
/\ Yes this worked, thank you Yal! I also changed it back to room speed because 3 frames was a bit too short after all.

The problem was a conceptual misunderstanding of the way the alarm time-set worked on my part. I came from a background of C-Basic, so in my mind I guess I was under the impression that this worked like the delay function in that it would actually prevent the code from progressing past that point until the timer finished counting down.

Now here's hoping I can finish the rest of this tutorial with no issues and move on to my bigger project!
 
Top