How to make "Game over" screen and restart room by pressing space?

pixeltroid

Member
As of now for my platform project, I have a working overhead health bar that correctly displays damage taken by player. I want to make it such that when the player dies, the following things should happen:

1. First we see "You died. Press space to restart" displayed on the screen (where the player died)
2. Player needs to hit "space" to restart game.
3. When game restarts, players is put back at the start of the room with his health bar reset to 100

I tried different tuts but each tut followed a different method and they didn't help me achieve the effect I wanted. I understand this "room restart" and "game over" thing involves health systems, rooms and text but I don't how how to go about it.

I am not a coder. Please explain slowly. Can someone please tell me what codes to put in what object?

Any help would be greatly appreciated!

P.S - For reference, I am working with obj_player and obj_healthbar
 
Last edited:
L

Lars Karlsson

Guest
EDIT: More in depth examples given there -> LINK

I've never written a 'reset' before, but I think I've got something that could work.
I'm going to assume that the player's health is stored in the obj_player. If not, tell me where it is and we'll work around that.
It deppends a bit on how things are being made and if several rooms are being used.


So first we want to declare a variable to keep track of wether or not the game is ready for a restart.
Code:
[Player Create Event]

restart = false;

Here we check inside the step event if the player has died. If true; we put the 'restart' variable to true and we are now checking for a spacebar input to happen. If that triggers, then we restart.
Code:
[Player Step Event]

if (health <= 0)
{
    restart = true;
    if (keyboard_check_pressed(vk_space))
    {
        room_restart();
    }
}

This part is for drawing the text. If the restart variable is true; we draw the text.
Code:
[Player Draw Event]

draw_self();    // This is to make sure the sprite image is being drawn.

if (restart)
{
    draw_text(x, y, "You died. Press space to restart.");
}
Probably not the cleanest way of doing it. But it should work.
 
Last edited by a moderator:

pixeltroid

Member
I'm going to assume that the player's health is stored in the obj_player. If not, tell me where it is and we'll work around that.
.
Hi, Thanks for your input!

Players health is stored in obj_player create event.

I tried your method, what happens is that the text displays as it should, but player still moves about. Pressing space gives me a black screen :/
 

Zerb Games

Member
if (restart)
{
draw_text(x, y, "You died. Press space to restart.");
}
Should be changed to:

draw_set_halign(fa_center)
draw_text(view_wview/2, view_hview/2, "You died.# Press space to restart.");

I think it would look better and if you want it in the center I would advise doing that, aside from that the system mentioned previously works just fine!
 
L

Lars Karlsson

Guest
if (restart)
{
draw_text(x, y, "You died. Press space to restart.");
}
Should be changed to:

draw_set_halign(fa_center)
draw_text(view_wview/2, view_hview/2, "You died.# Press space to restart.");

I think it would look better and if you want it in the center I would advise doing that, aside from that the system mentioned previously works just fine!
That does indeed look better.
 
L

Lars Karlsson

Guest
Hi, Thanks for your input!

Players health is stored in obj_player create event.

I tried your method, what happens is that the text displays as it should, but player still moves about. Pressing space gives me a black screen :/
When the player dies, are you destroying any objects? Not just the obj_player?

Edit: Actually, I don't understand why it'd go black. Are you placing objects in the room via the room editor?

Edit2: I'm at work atm. Won't be home for another 6 hours, so little tricky to help. But I would't mind taking you through it once I get home. Unless someone else gives you a working solution before.
 
A

Aura

Guest
Why are you using health when you're using hp for determining health? Quit copy-pasting the code as-is. Instead understand how it works and tweak it according your needs.

Code:
if (hp <= 0) {
   //...
}
Also, you'd perhaps want to use game_restart() instead, if you want to restart the game entirely; not just the room...
 

pixeltroid

Member
Why are you using health when you're using hp for determining health? Quit copy-pasting the code as-is. Instead understand how it works and tweak it according your needs.
ok. hehe. I forgot about the "hp" in players create event. Now I've changed "hp" to "health". But it still isn't working.
The "you died" text displays but the player can still move around and shoot. Pressing space to restart room gives me a black screen.




Also, you'd perhaps want to use game_restart() instead, if you want to restart the game entirely; not just the room...
When the hero dies, I want to restart from the room I died in, not all the way at the beginning.
 
L

Lars Karlsson

Guest
The problem with the player being able to move while dead is an easy fix which we can go to later. And it's honestly something you should try and figure out yourself.
But what's odd is the screen going black after the reset. Are you using views?
 

pixeltroid

Member
The problem with the player being able to move while dead is an easy fix which we can go to later. And it's honestly something you should try and figure out yourself.
I tried fixing it by destroying the player object and adding a death animation. But then the "you died press space to restart" doesn't appear. :/
 
L

Lars Karlsson

Guest
Yes. I am using views...to focus on the player around as he walks around.
Okay. Make sure the 'Visible when room starts' is enabled in the room. That might solve the black screen problem.
 

pixeltroid

Member
But what's odd is the screen going black after the reset. Are you using views?
Update: You are correct. I think it has something to do with views. The screen goes black because my background colour is black. I changed it to green and now the screen turns green when I restart. So whats been happening is that the restarted room starts of at the top left corner (where there are no graphics or objects). That's why I've been seeing the background color when the room restarts!

In room editor, I placed the player on the top edge of the screen, and when I die and restart the room, the player appears and drops down to the floor...and the game resumes normally.

So your method is working! I just need to make the view focus on my player when the room restarts.
 

pixeltroid

Member
Guys I need some serious help!

I wasted the whole day trying to get this to work but I failed! I did everything that people posted here, but something or the other goes wrong. Either the game restart message doesn't appear... or the room restarts with the view all messed up... or the healthbar doesn't reset. It is maddening to try doing same thing and failing over and over!

I deleted all the codes I was trying and placed a new test game file here:
https://drive.google.com/open?id=0Bz3M7gWNpxJCeTFBa2J0LWpLTE0

The file has got the player with healthbar, a few enemies, health kits and 2 rooms to check if the game restarts in the same room.

I just want "you died. Press space to restart" to appear on the center of the screen when player dies. And pressing space should restart the room with a full health bar. Once this is done, I can move on to other things like enemy AI and level design.

Can somebody please just take a look at my file, make necessary changes and reupload it to googledrive or dropbox or whatever else ?

Thanks in advance. Any help will be greatly appreciated!
 
Last edited:
L

Lars Karlsson

Guest
Project

[Player Object]
  • Added 3 variables in the create event under // Restart.
    • 'spawnx / spawny': You'll have to update these everytime you want to move the spawnpoint, e.g. when you change room.
  • Added a script in the Step Event for checking if player is dead. It also looks for the 'vk_space' input and resets some variables and restarts the room.
  • Added a scipt in the Draw Event to draw text when the player is dead
  • Added a check for player's health before allowing player actions.

Your code is a bit of mess. You really should follow some GML tutorials. You'll get much further if you understand what the lines you add actually do.
 

pixeltroid

Member
Project

[Player Object]
  • Added 3 variables in the create event under // Restart.
    • 'spawnx / spawny': You'll have to update these everytime you want to move the spawnpoint, e.g. when you change room.
  • Added a script in the Step Event for checking if player is dead. It also looks for the 'vk_space' input and
.
Oh man! Thanks a TON!! What you did was EXACTLY what I needed. I was pulling my hair out over this last night!

One question. Where exactly do I go to update spawnx/spawny to create a respawn point unique to each room?

As of now, whenever I die, I respawn at a specific place in room1.


Your code is a bit of mess. You really should follow some GML tutorials. You'll get much further if you understand what the lines you add actually do
I did start off looking at GML tuts, but after a point, a lot of the concepts started to go over my head. Now I'm sometimes too scared to mess with code because if I do something wrong, the whole game breaks . I'm still trying to learn a bit and taking notes, so I am still dependant on the coding expertise of others.
 
L

Lars Karlsson

Guest
You have this in the player colliding with the next room object.
Code:
//go thru the door

if (room_exists(other.new_room)) {
    room_goto(other.new_room);
    x = other.new_x;
    y= other.new_y;

    instance_create(x,y,obj_fade)
}
add the following before instance_create(x,y,obj_fade)
Code:
spawnx = other.new_x;
spawny = other.new_y;
 

pixeltroid

Member
You have this in the player colliding with the next room object.
Code:
//go thru the door

if (room_exists(other.new_room)) {
    room_goto(other.new_room);
    x = other.new_x;
    y= other.new_y;

    instance_create(x,y,obj_fade)
}
add the following before instance_create(x,y,obj_fade)
Code:
spawnx = other.new_x;
spawny = other.new_y;
Ok, I did it. And it worked.
Thanks a TON man! This feels like a huge burden off my chest! thank you so much for all your patience and help!

I can finally tick this off my to-do list!
 
R

randomizedboss2016

Guest
So when I try to run my game, i dont even get a message saying hit space to respawn, and even when i try to respawn, hitting space, it does nothing. Here are all my player codes, please help. Also all that happens when my health hits 0 or less than 0, my player just dissapears, hope you have enough info to help.

CREATE CODE
/// Set fixed rotation
event_inherited();
hp = 5;
spd = 2;
hspd = 0;
vspd = 0;
len = 0;
dir = 0;
xaxis = 0;
yaxis = 0;
attacked = false;
image_speed = 0
state = scr_move_state;
face = RIGHT;
gamepad_set_axis_deadzone(0, 0.7);
restart = false;
view_object[0] = id;

// Create the sprite array
sprite[RIGHT, MOVE] = spr_player_right;
sprite[DOWN, MOVE] = spr_player_down;
sprite[LEFT, MOVE] = spr_player_left;
sprite[UP, MOVE] = spr_player_up;

sprite[RIGHT, ATTACK] = spr_player_attack_right;
sprite[LEFT, ATTACK] = spr_player_attack_left;
sprite[DOWN, ATTACK] = spr_player_attack_down;
sprite[UP, ATTACK] = spr_player_attack_up;


STEP CODE
/// Move the player in the step event
depth = -y;
script_execute(state);

sprite_index= sprite[face, movement];

// Respawn
if (hp <= 0)
{
restart = true;
if (keyboard_check_pressed(vk_space))
{
room_restart();
}
}

// Check for death
if (obj_player_stats.hp <= 0) {
instance_destroy();
}


DRAW CODE
/// Draw the shadow
draw_sprite(spr_player_shadow, image_index, x, y);
draw_self();

// Death message
draw_self(); // This is to make sure the sprite image is being drawn.

if (restart)
{
draw_set_halign(fa_center)
draw_text(view_wview/2, view_hview/2, "You died.# Press space to restart.");
}

Please reply if you see any problems!
 
L

Lars Karlsson

Guest
If you were to remove the following code, it should all work.

Code:
// Check for death
if (obj_player_stats.hp <= 0)
{
    instance_destroy();
}
What is happening is that the object gets destroyed as soon as the player hits 0 health. So it is only looking for your keyboard input and drawing the text, during a single frame.

Edit: I don't think you'll need the instance_destroy(), since you are restarting the room.
 
Last edited by a moderator:
Top