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

Lose Life When Player Clicks on Object

T

Tenten2016

Guest
So I'm having trouble with one of my codes. I'm trying to make the player lose an life each time the player
clicks on an object which is called object_wrong answer. When the player clicks on an wrong answer three times, he lost an total of three lives, and the game shall restart. Object_wrong_answer, step event:

  1. {

  2. //Check if the mouse is touching the object
  3. var mouse_in_bounds = point_in_rectangle(mouse_x, mouse_y, bbox_left, bbox_top, bbox_right, bbox_bottom);
  4. //Check if the left mouse button is clicked and released
  5. var mouse_clicked = mouse_check_button_released(obj_wrong_answer);

  6. //If the left mouse button is clicked and released while touching the object
  7. if (mouse_in_bounds) && (mouse_clicked) {
  8. //Remove 1 life or restart the room if there isn't a life to remove
  9. if (lives > 1) lives -= 1; else room_restart();
  10. }

  11. }
I feel like this is a bit much for what I'm trying to do, but this is the first time I'm working on an multiple choice type game. Any suggestions, or advice would be appreciated.
 
T

Tenten2016

Guest
As I always do, I'm going to continue to work on an solution, and update this thread as I go. I like to help others as well when I have an problem just in case they have an similar problem.
 
Why not just use a mouse click event?
https://gyazo.com/8432a0681e6c61b972eba11fc4fdfdcf

Any code you put in that event will only happen once the first time the left mouse button is pressed ON that object (using it's sprite mask or collision mask for checking against the mouse cursor)
Getting the player reference might be an issue, unless the player object is unique on each map. If it is unique, then in the click event we can just do:

Code:
var _player = instance_find(obj_player, 0);
if(_player.lives > 0){
  _player.lives -= 1;
}
else{
  room_restart();
}
 
Easier to just say

Code:
with(obj_player)
{
   if(_player.lives > 0){
     _player.lives -= 1;
   }
   else{
     room_restart();
   }  
}
Assuming there is only ever one player.
 
Easier to just say

Code:
with(obj_player)
{
   if(_player.lives > 0){
     _player.lives -= 1;
   }
   else{
     room_restart();
   }
}
Assuming there is only ever one player.
Yes, this is definitely another viable solution.
Also, you forgot to remove the declared variable names, so:

Code:
with(obj_player)
{
   if(lives > 0){
     lives -= 1;
   }
   else{
     room_restart();
   }
}
 
T

Tenten2016

Guest
{

{
//Remove 1 life or restart the room if there isn't a life to remove
if (lives > 1) lives -= 1; else room_restart();
}

}

This was the code I implement, and now the game starts over, but only on one life. In other words, when the player clicks the wrong answer once, the game restarts. Let's see what would happen if I add in lives = 3.
 
{

{
//Remove 1 life or restart the room if there isn't a life to remove
if (lives > 1) lives -= 1; else room_restart();
}

}

This was the code I implement, and now the game starts over, but only on one life. In other words, when the player clicks the wrong answer once, the game restarts. Let's see what would happen if I add in lives = 3.
Yes, you would want to declare the correct amount of lives the player would have again.
 
T

Tenten2016

Guest
The code works, but my lives aren't working. So when the player clicks on the wrong answer once....it still starts over. Let me experiment.
Also the other code I was using also worked but same thing when it came to lives. That's why I like GML, more than one way to do one thing. =D

Yes, this is definitely another viable solution.
Also, you forgot to remove the declared variable names, so:

Code:
with(obj_player)
{
   if(lives > 0){
     lives -= 1;
   }
   else{
     room_restart();
   }
}
Yes, you would want to declare the correct amount of lives the player would have again.
 
T

Tenten2016

Guest
So the code still isn't upsetting:

lives = 3

Regardless if I put in the create event or the mouse click event.
 
T

Tenten2016

Guest
This code isn't working the way it's suppose to be working even when manipulating the numbers, it remains the same. It's failing to reconize that there are three lives in the create event.
  1. with(object_wrong_answer)
  2. {
  3. if(lives > 0){
  4. lives -= 1;
  5. }
  6. else{
  7. room_restart();
  8. }
  9. }
I will continue to experiment and update as I can.
 
This code isn't working the way it's suppose to be working even when manipulating the numbers, it remains the same. It's failing to reconize that there are three lives in the create event.
  1. with(object_wrong_answer)
  2. {
  3. if(lives > 0){
  4. lives -= 1;
  5. }
  6. else{
  7. room_restart();
  8. }
  9. }
I will continue to experiment and update as I can.
The "with" operator means that all code executed inside the brackets will be under the instance of the argument passed into it.
So, in this case, you are saying the lives belong to object_wrong_answer, which we know is wrong.
If the lives belong to the player, you will need to execute it as the player object, not as the wrong answer object.
Refer to the code that we discussed, the one you are using is different.

If your player object is named object_player, then it would be:
Code:
with(object_player){
  if(lives > 0){
    lives -= 1;
  }
  else{
    lives = 3;
    room_restart();
  }
}
I would suggest researching documentation and watching youtube tutorials if you want to understand how the function works.
https://docs.yoyogames.com/source/d...ce/001_gml language overview/401_18_with.html
 
T

Tenten2016

Guest
My player object is not named player object, its object_wrong_answer. duh...

However, I don't think you know what I'm trying to do, or what exactly I'm asking advice on.
The "with" operator means that all code executed inside the brackets will be under the instance of the argument passed into it.
So, in this case, you are saying the lives belong to object_wrong_answer, which we know is wrong.
If the lives belong to the player, you will need to execute it as the player object, not as the wrong answer object.
Refer to the code that we discussed, the one you are using is different.

If your player object is named object_player, then it would be:
Code:
with(object_player){
  if(lives > 0){
    lives -= 1;
  }
  else{
    lives = 3;
    room_restart();
  }
}
I would suggest researching documentation and watching youtube tutorials if you want to understand how the function works.
https://docs.yoyogames.com/source/dadiospice/002_reference/001_gml language overview/401_18_with.html
 
My player object is not named player object, its object_wrong_answer. duh...

However, I don't think you know what I'm trying to do, or what exactly I'm asking advice on.
Duh? You never clarified on that exact point. And if that's true, then all you need to do is add lives = 3; right before the room_restart() call.
 
T

Tenten2016

Guest
Nope that didn't work either. Like I said...something is wrong with that code. Because it doesn't implement the lives and I am trying to experiment to why it isn't implementing the lives.

Duh? You never clarified on that exact point. And if that's true, then all you need to do is add lives = 3; right before the room_restart() call.

The "with" operator means that all code executed inside the brackets will be under the instance of the argument passed into it.
So, in this case, you are saying the lives belong to object_wrong_answer, which we know is wrong.
If the lives belong to the player, you will need to execute it as the player object, not as the wrong answer object.
Refer to the code that we discussed, the one you are using is different.

If your player object is named object_player, then it would be:
Code:
with(object_player){
  if(lives > 0){
    lives -= 1;
  }
  else{
    lives = 3;
    room_restart();
  }
}
I would suggest researching documentation and watching youtube tutorials if you want to understand how the function works.
https://docs.yoyogames.com/source/dadiospice/002_reference/001_gml language overview/401_18_with.html
 
T

Tenten2016

Guest
Something within this line of code:

  1. with(object_player){
  2. if(lives > 0){
  3. lives -= 1;
  4. }
  5. else{
  6. lives = 3;
  7. room_restart();
  8. }
  9. }
Is preventing the lives from working; try experimenting with the code yourself and you'll see it isn't working. The code is causing the room to restart after player clicks on wrong object 1 time. When player is suppose to be able to click on object three times and then game restarts. Wrong answer, wrong answer, wrong answer room restart. NOT - wrong answer, room restart.
 
Something within this line of code:

  1. with(object_player){
  2. if(lives > 0){
  3. lives -= 1;
  4. }
  5. else{
  6. lives = 3;
  7. room_restart();
  8. }
  9. }
Is preventing the lives from working; try experimenting with the code yourself and you'll see it isn't working. The code is causing the room to restart after player clicks on wrong object 1 time. When player is suppose to be able to click on object three times and then game restarts. Wrong answer, wrong answer, wrong answer room restart. NOT - wrong answer, room restart.
If it does it immediately, it means that lives is not being initialized to an integer greater than zero. Have you tried debugging the program and single stepping through the code, checking the variable states?
Somewhere in the creation of your player object, you need to initialize the lives variable and set its value to 3.
 
T

Tenten2016

Guest
In the Create Event: Lives =3
unless I make an Object_controller, Create Event: global.life =3 something like that.

I still think it's the code. I'm going to try posting on Reddit, and see what they say if I'm not able to solve it by 5pm. If that fails then I'll debug it. But rarely if ever have bugs.....in the games I work on. But we'll see.

If it does it immediately, it means that lives is not being initialized to an integer greater than zero. Have you tried debugging the program and single stepping through the code, checking the variable states?
Somewhere in the creation of your player object, you need to initialize the lives variable and set its value to 3.
 
In the Create Event: Lives =3
unless I make an Object_controller, Create Event: global.life =3 something like that.

I still think it's the code. I'm going to try posting on Reddit, and see what they say if I'm not able to solve it by 5pm. If that fails then I'll debug it. But rarely if ever have bugs.....in the games I work on. But we'll see.
It's not the code, it's probably the scope of your variables that you're referencing.
As I asked earlier, have you tried debugging your code and single stepping through the event? You need to check the variable values to determine with certainty that you are referencing the correct objects.
 
T

Tenten2016

Guest
I haven't debugged it yet, but the variables are correct. Have you tried the code in GM yourself?

It's not the code, it's probably the scope of your variables that you're referencing.
As I asked earlier, have you tried debugging your code and single stepping through the event? You need to check the variable values to determine with certainty that you are referencing the correct objects.
 
I haven't debugged it yet, but the variables are correct. Have you tried the code in GM yourself?
If you have not debugged, how do you know the variables that are being used and referenced are within the correct scope and have the correct values? You cannot know unless you debug the program or add in debugging features/printouts.

Try the code how? I do not have the rest of the context.
I do have many similar uses of code throughout my project, and that's why I am certain it would work fine.
 
T

Tenten2016

Guest
Well then make an sample project with an room and two objects so as to test the code on its own. See what happens. Because I'm arguing the code is a miss a bit.

If you have not debugged, how do you know the variables that are being used and referenced are within the correct scope and have the correct values? You cannot know unless you debug the program or add in debugging features/printouts.

Try the code how? I do not have the rest of the context.
I do have many similar uses of code throughout my project, and that's why I am certain it would work fine.
 
Well then make an sample project with an room and two objects so as to test the code on its own. See what happens. Because I'm arguing the code is a miss a bit.
Yup, works fine. I didn't use lives as a variable name though because that's a reserved variable name.

Green object is player, red object is wrong answer. Clicking wrong answer three times will make the room background change to black for one second, before calling the room_restart() method.

http://www.mediafire.com/file/i8daxzrbmraq8hm/Test1.gmx.zip
 
T

Tenten2016

Guest
Thanks, I'm going to take a look at this then compare it to what I got. Then come back and list my mistakes if things turn out good. =D That's usually how I do it.

Yup, works fine. I didn't use lives as a variable name though because that's a reserved variable name.

Green object is player, red object is wrong answer. Clicking wrong answer three times will make the room background change to black for one second, before calling the room_restart() method.

http://www.mediafire.com/file/i8daxzrbmraq8hm/Test1.gmx.zip
 
T

Tenten2016

Guest
Why is there an obj_player in your game? I still think you're confused at what I'm trying to do.
My game is an point in click game. You're asked an question, you have four answers to choose from. Three are wrong and one is right. If you click three wrong answers, the game is over.

  1. with(object_player){
  2. if(lives > 0){
  3. lives -= 1;
  4. }
  5. else{
  6. lives = 3;
  7. room_restart();
  8. }
  9. }
The reason why this code doesn't work is because it isn't responding to anything because there is no obj_player.This code is broken. It doesn't check to see if anything is being clicked, but rather loops through until your lives are all gone. It does not stop after removing only 1 life. Unless you're telling me I have to create an object player make it invisible...which seems ugghhg =0

Yup, works fine. I didn't use lives as a variable name though because that's a reserved variable name.

Green object is player, red object is wrong answer. Clicking wrong answer three times will make the room background change to black for one second, before calling the room_restart() method.

http://www.mediafire.com/file/i8daxzrbmraq8hm/Test1.gmx.zip
 
T

Tenten2016

Guest
Yeah that code doesn't work; it's broken. The mouse needs to check for an object to click on it.

Well back to square one.
 
T

Tenten2016

Guest
  1. with(object_player){ //there's no object_player as a result GM doesn't know what I or anyone else is referring to when creating an point & click game
  2. if(lives > 0){//these next two really don't make sense, if lives are less than 0 then why is it losing an life. It should be click object, lose life, if lives less than 0 restart
  3. lives -= 1;
  4. }
  5. else{
  6. lives = 3; //as a result, the lives keep repeating itself in a cycle because the three lives are automatically being used up
  7. room_restart();
  8. }
  9. }
The code isn't checking for anything. The mouse doesn't know what to click on. The lives don't know when to act and react.
 
  1. with(object_player){ //there's no object_player as a result GM doesn't know what I or anyone else is referring to when creating an point & click game
  2. if(lives > 0){//these next two really don't make sense, if lives are less than 0 then why is it losing an life. It should be click object, lose life, if lives less than 0 restart
  3. lives -= 1;
  4. }
  5. else{
  6. lives = 3; //as a result, the lives keep repeating itself in a cycle because the three lives are automatically being used up
  7. room_restart();
  8. }
  9. }
The code isn't checking for anything. The mouse doesn't know what to click on. The lives don't know when to act and react.
You should not be copy and pasting literally everything. You should be able to adapt and integrate the code into your project accordingly based on your design.
For line 1: It's an example. You are supposed to reference whatever the name of YOUR player object is. If your player object is named "obj_player" then that's clearly different than "object_player". GM will know exactly what you or anyone else is referring to if you add it as a click event (which was already mentioned before) to the object you want to be clicked.
For line 2: What this line says is, if lives is GREATER than zero, subtract a life. If it is NOT greater than zero, restart. The minor correction to this (that I made in the example I uploaded which I'm guessing you haven't even looked at) is that zero should be replaced with the number one. By originally comparing it to zero, it effectively allowed four lives total instead of three. In my correction, I kept this structure of the if statement because that's how it was originally being explained. But, if were up to me it would be:
Code:
with(obj_player){
  lives -= 1;
  if(lives <= 0){
    lives = 3;
    room_restart();
  }
}
For line 6. Please refer back to the point I made for line 2.
 
T

Tenten2016

Guest
RESOLVED!!!

I used an completely different code. I used the global.currentlives code and I placed it in 'Left Press'. Come to find out the 'Left Button' code isn't the same
as the 'Left Press' code. Something I didn't realize until now.

This code:

with(obj_player){
lives -= 1;
if(lives <= 0){
lives = 3;
room_restart();
}
}

Doesn't work because as I said before, there is NO object player. There also isn't any code that tells the lives to subtract as a result of it being an broken code. I would not recommend this code; instead, I recommend for people to use the global.currentlives code.


You should not be copy and pasting literally everything. You should be able to adapt and integrate the code into your project accordingly based on your design.
For line 1: It's an example. You are supposed to reference whatever the name of YOUR player object is. If your player object is named "obj_player" then that's clearly different than "object_player". GM will know exactly what you or anyone else is referring to if you add it as a click event (which was already mentioned before) to the object you want to be clicked.
For line 2: What this line says is, if lives is GREATER than zero, subtract a life. If it is NOT greater than zero, restart. The minor correction to this (that I made in the example I uploaded which I'm guessing you haven't even looked at) is that zero should be replaced with the number one. By originally comparing it to zero, it effectively allowed four lives total instead of three. In my correction, I kept this structure of the if statement because that's how it was originally being explained. But, if were up to me it would be:
Code:
with(obj_player){
  lives -= 1;
  if(lives <= 0){
    lives = 3;
    room_restart();
  }
}
For line 6. Please refer back to the point I made for line 2.
 
Top