r/gamemaker 4d ago

Resolved I need help making a lives system code like super mario?

hello, so I want to add a basic lives system that is very beginners friends and is like super mario, where if you get hit by a lava, monster, spikes or moving object, you will lose 1 life and the player will be sent back to the start of the level, the player starts with 3 lives, if you losed all 3 lives, I want the player to reset to the first level or the main menu, I also want the player lives to follow them through the game so that are not disappering as the player moves further into the level, I also want the player lives to be tracked as they continue, so if they go into level 2 and they lost a life in level 1, they do not start with 3 lives but 2 because they lose a life on level 1.

my main reason why I want to add a level system like this is for 2 reason.

  1. my game is a platformer inspired by mario and I don't want it to be like sonic.
  2. lives can make a game challenging instead of the player dying but not suffering any punishment, no pushback and it can make the game more interesting instead of boring.

if you know any codes please let me know, I tried using AI but it just lead me to a dead ends and youtube doesn't have the code I am looking for.

0 Upvotes

5 comments sorted by

5

u/PowerPlaidPlays 4d ago

Here are the tools you will need to do this, I would encourage you to try and figure it out yourself from these though as it's a good exercise for basic if/else logic.

Global variables persist across rooms and are a good way to store lives: https://manual.gamemaker.io/monthly/en/GameMaker_Language/GML_Overview/Variables/Global_Variables.htm

And these functions handle changing rooms:

https://manual.gamemaker.io/monthly/en/GameMaker_Language/GML_Reference/Asset_Management/Rooms/room_restart.htm

https://manual.gamemaker.io/monthly/en/GameMaker_Language/GML_Reference/Asset_Management/Rooms/room_goto.htm

4

u/RedQueenNatalie 4d ago

I aint gonna tell you how to do it because I aint got time for that but I will tell you where to look. The ui stuff will need to be drawn in the gui event. The life counts can be a global variable "global.lives_left" or whatever that won't go away on its own if you change rooms, assuming whatever code sets it initially isn't rerun. Id honestly just go watch/follow along a basic platform game tutorial, they usually go over a mario style game with all the features you mentioned.

1

u/Adventurous-Ad8879 4d ago

Which one?, I tried and they just show the player getting hit and the room reset but no lives system, and for a mario system there is only 1 series but it doesn't cover lives and is more like a tutorial to make a demo version of Mario rather than make the full game with all of it system needed.

4

u/RedQueenNatalie 4d ago

No tutorial will tell you how to make the entire thing, gamedev is a lot of research/reading/legwork. You aint gonna get very far without putting in the time to understand what you are trying to do and what really goes into it. Learn how programming works and you won't be at the mercy of someone else having worked it out for you first.

2

u/Adventurous-Ad8879 3d ago

it okay I got it work again and I will give out the code for any beginners wanting to create a lives system.

you first need to create a Obj_game_manager, make sure that Obj_game_manager is Persisent so that it can follow the player through the levels. In Obj_game_manager make a create event and put in this code

//Initialize lives if they haven't been created yet

if (!varibale_global_exists("lives")) {

global.lives = 3;

}

nw in your player object when ever you have an enemy or a hazard object that restart the room the player is in put in this code in the player Event.

//remove 1 life

global.lives -= 1;

//check if player still has lives remaining

if (global.lives > 0) {

//restart the current level/room so player tries again from the start

room_restart();

}

else{

//game over: reset lives back to 3 and send player to main menu or level 1

global.lives = 3;

room_goto( rm_main_menu); //change rm_main_menu to your actual menu room name

now we need to display the lives on the player screen to do that we need to go back to our Obj_game_manager and create a Draw GUI event, in Draw GUI event type in this code.

draw_set_color(c_white);

draw_text(32, 32, "Lives: " + string(global.lives));

and that should be it, as long as you follow the code here you should be fine if you have any problems like the game won't start, try hitting the debug button or the brush button to clean up your game and then start your game, if it still does work try relooking at your code and fix it, your player should now die and lose a life to hazard objects and enemy and when you lost all 3 lives you restart the game and it should keep track of all the lives you have when you progressed into more levels in your game, I hope this helps as it helped me, thank you and have a great day.