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

GameMaker No gun on character when entering next room

OnLashoc

Member
Ok so Im working on a platformer to which my Player object (oPlayer) runs into the floating Gun object (oGunPickup) to pick it up with a collision event on the oGunPickup object and here is the code:

Code:
/// @description Pickup gun

global.hasgun = true;
instance_create_layer(oPlayer.x,oPlayer.y,"Gun",oGun);
audio_play_sound(snGunEquip,10,false);
instance_destroy();
So now my player has a gun equipped and is able to shoot and run around within the room shooting and killing enemies... However as soon as I exit the room via a trigger / transition object placed at the exit of the level, and my character is placed in the next room, the gun disappears unless I drag a oGun object and place it in the room.

Since I want to have different gun pickups in the first room, I mean who doesn't like choice? :p I don't want to place the oGun object in the room because no matter which gun you choose, it is always going to default to the oGun object I placed in the room.

What would be the best way to implement remembering which gun object that was chosen, when transitioning into the next room and so on?
 

OnLashoc

Member
For instance if the player chooses to go with the laser gun I had an object called oLorbPickup with a similar set of code (like in oGunPickup) as seen below:

Code:
/// @description Pickup Lorb

global.hasgun = true;
instance_create_layer(oPlayer.x,oPlayer.y,"Gun",oLorb);
audio_play_sound(snGunEquip,10,false);
instance_destroy();
 
Create an enum with a list of the various weapons and when you pick up a gun, store the enum that corresponds to that gun in a separate variable, then load whatever gun it has in room_start.
Create Event
Code:
enum e_guns {
   NONE,
   PISTOL,
   BAZOOKA
}
current_gun = e_guns.NONE;
On weapon pickup
Code:
current_gun = e_guns.PISTOL // Change the enum for each gun, obviously
Room Start
Code:
switch (current_gun) {
   case e_guns.PISTOL:
      instance_create_layer(oPlayer.x,oPlayer.y,"Gun",oPistol);
   break;
   // Etc, etc, create a new case for each weapon.
}
 

OnLashoc

Member
God I swear I've forgot more than I remember lol.

Check mark Persistent on the the oLorb and oGun object and whichever one was chosen will carry on into the next room :p

Oh and thanks for the response, I'll try that too :)
 
Persistence is a bad way to do this, because then you have to worry about any and all cases where the player can exit a room and you DON'T want the gun to come along. With my way, if the player exists, the gun is created, if the player doesn't exist (for instance going back to the title screen or whatever) no need to worry about having to find and delete whatever gun is hanging around.

Really, you only ever want controller objects to be persistent, and then only controller objects that you want to literally exist for the entire game. Persistence is a quick fix to problems like this, but it will lead to headaches later on.
 

OnLashoc

Member
So what object am I putting the enum on? oPlayer, oGunPickup, or oGun?

Until you respond as you said I've quick fixed by adding this at the beginning of oLorb and oGun

Code:
if (room == rMenu)
    {
        instance_destroy();
    }
    else
    {
        x = oPlayer.x;
        y = oPlayer.y-35;
        
    }
In which destroys whichever gun was equipped to be destroyed when exiting the final level and returns you to the main menu. However upon death you respawn without a gun, and depending on which level, you may not have a weapon which is needed to finish the level... I patiently await while I research enum's more.
 

OnLashoc

Member
Yea I tried to follow your directions, either I was getting an error on death, or it outright wasn't doing anything. I searched youtube and found 2 videos that explained what enum's did, but were either irrelevant to my setup, or were in GMS 1.4 and difficult to follow.
 
Top