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

player being duplicated

RODO

Member
Hello everybody, How are you ? well, i have a problem with my procedural map generation code, and i would like to know about something.

Explaining previously about how the game is: you are created on a map, which is almost a totally random maze, and every time you find the exit, the room is restarted.
1609961138220.png
1609961215960.png
(images as an example)

The problem: when I put obj_player as persistent so that its code is not lost (like life, points, weapons etc.), when the room restarts, obj_creater ends up creating several players a lot at once, making everything turn into a full chaos of player. The game itself works well, a problem that every time "move to the next room" ends up losing all information and so everything is restarted.
is there any way to just keep a part of the code as persistent? or, how can I get the other instances of the player to be destroyed? thanks for your attention and thanks for all the help
 

kburkhart84

Firehammer Games
Your problem started when you put all that stuff,(life, points, weapons) on the player. Those things should be stored either globally if you prefer, or in a controller object that is persistent and only created once. The player object should really only handle the player itself, nothing else. It is good practice to isolate objects into small things that handle just one thing only. It lets you easily add and take away later. If I have a platformer player, and I decide I want a level where the player acts completely different, I can just make a different player and spawn that instead. I have a different object that handles input, so the player objects(whichever I spawn on a given level) checks the input object. This way, I don't have to duplicate the code that handles inputs since that won't change.

Another tip, put your controller object in a room that doesn't do anything except go to the first room you need(your main menu, splash screen, whatever). Then, you don't have to put that object anywhere else, and since its persistent, it will stay around. And since you don't put it anywhere else, and you never go back to that first room, you avoid the duplicate objects.
 

RODO

Member
Your problem started when you put all that stuff,(life, points, weapons) on the player. Those things should be stored either globally if you prefer, or in a controller object that is persistent and only created once. The player object should really only handle the player itself, nothing else. It is good practice to isolate objects into small things that handle just one thing only. It lets you easily add and take away later. If I have a platformer player, and I decide I want a level where the player acts completely different, I can just make a different player and spawn that instead. I have a different object that handles input, so the player objects(whichever I spawn on a given level) checks the input object. This way, I don't have to duplicate the code that handles inputs since that won't change.

Another tip, put your controller object in a room that doesn't do anything except go to the first room you need(your main menu, splash screen, whatever). Then, you don't have to put that object anywhere else, and since its persistent, it will stay around. And since you don't put it anywhere else, and you never go back to that first room, you avoid the duplicate objects.
wow, that helps me a lot. I didn't come up with this idea but I have another question: can't this kind of create a very big mess? because I’m worried if this could be a problem because there’s too much obj
1609965181100.png
for example: this is all that I am using in my game and I am scared to see so much that I did
 

kburkhart84

Firehammer Games
There are tricks to lessening how many objects you have. Then if you use groups to good effect you will also be fine. Don't be surprised when you have lots of objects though. If you are adding objects due for example to the advice I've given you, making each object do its own thing, then its a good thing to have more objects. There isn't any kind of hard limit to how many objects you have, so don't worry about anything like that. Some games end up with 100s of objects an they work just fine.

One trick to saving objects, is when possible, make objects have variables that dictates behavior. For example, if you have two bullets, two sprites, two speeds, but otherwise identical, make it one object, and just set the sprite and the speed, if that feels good to you to do in order to save an object. Another thing you can do, which helps especially for things you actually place into the room(less so for things you spawn during the gameplay), is using parent/child objects where possible, and using instance variables where possible, as overwritten in the room editor. If I make a button object, and I place 3 instances of that button, I can set a text variable and set a variable for the function to call when the button is hit. And I can have 3 buttons show different text, do different things when you click on them, but have the same code for responding to clicks, animating when you mouse over, etc... This can be done just changing variables in the room editor.
 
Top