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

Camera makes game crash if player instance is destroyed

EpicMcDude

Member
So this camera makes the game crash whenever the player dies or moves to another room, this is because the player object no longer exists so the camera crashes.
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of Step Event2
for object obj_Camera:

Unable to find any instance for object index '0' name 'obj_player'
at gml_Object_obj_Camera_Step_2 (line 16) - var targetX = obj_player.x - RES_W/2;
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_Camera_Step_2 (line 16)

Basically, if the player instance is destroyed, how can I make the camera stay on the player? I have no idea how to code this, especially with the room transitions.

Thank you in advance.
 
Last edited:

FrostyCat

Redemption Seeker
Check instance_exists(obj_player) before running that line of code or any others that reference values off obj_player.
GML:
if (instance_exists(obj_player)) {
    var targetX = obj_player.x - RES_W/2;
    ...
}
 

ophelius

Member
You can either use:
Code:
if(instance_exists(obj_player)){
   ...
}
or maybe when the player dies, turn him invisible instead of destroying the object. GM will still think the player object exists. But in the player object, you would have to limit the actions available if he's invisible/dead:

Code:
if(obj_player.visible){
   //process all your normal actions/code here
}else{
   //other code that executes only when dead
}
 
Last edited:
M

ManiacCoder

Guest
maybe when the player is killed, get rid of the camera too. and if the player moves to another room, get rid of the previous camera and make a new one.
 

EpicMcDude

Member
Check instance_exists(obj_player) before running that line of code or any others that reference values off obj_player.
GML:
if (instance_exists(obj_player)) {
    var targetX = obj_player.x - RES_W/2;
    ...
}

You can either use:
Code:
if(instance_exists(obj_player)){
   ...
}
or maybe when the player dies, turn him invisible instead of destroying the object. GM will still think the player object exists. But in the player object, you would have to limit the actions available if he's invisible/dead:

Code:
if(obj_player.visible){
   //process all your normal actions/code here
}else{
   //other code that executes only when dead
}
Thank you for your help amazing people!
The instance_exists works wonders, the camera stays on the body and the game no longer crashes, and I can transition into a new room.

But now there is a new problem, when I respawn the player, (press R to restart) the camera seems to squish the whole room into the game window, like it's ignoring the entire code. When I transition into a new room, the camera resets and functions as normal, but not when I restart.
I don't know if this is a problem with the way the restart code works or the camera code...?

EDIT: Actually room transitions are botched too, the room gets squished into the game window.
 
Last edited:

woods

Member
off chance is there 2 instances of the obj_player and the camera is fighting to see both of them?
 

EpicMcDude

Member
off chance is there 2 instances of the obj_player and the camera is fighting to see both of them?
Seems to be the problem, I fixed the transition problem, when I transition into the room that doesn't have the obj_player (the next part of the level) on it everything works, but when I go back to the first level where the obj_player is placed in the room the camera freaks out.

Thank you for you help.
 
Top