Jump to content


Photo

A Very Basic Question...


  • Please log in to reply
6 replies to this topic

#1 RaptureMachine

RaptureMachine

    GMC Member

  • New Member
  • 10 posts
  • Version:GM8

Posted 11 March 2012 - 07:54 PM

Alright, so I have my player coded to gain health when he picks up a health potion, of course. Now, my problem is that I don't want the play to be able to pick one up if his health is full. I've tried various code, and nothing seems to be working. Here's the code I have now for a collision event with the potion.

if hitpoints < 100 {
    hitpoints += 25;
    if hitpoints > 100 {
        hitpoints = 100
        }

I know it's a very simple question, but any help would be appreciated.
And obviously, hitpoints is my variable.

Thanks a ton!
  • 0

#2 Mine Lad

Mine Lad

    GMC Member

  • New Member
  • 36 posts
  • Version:GM8

Posted 11 March 2012 - 07:58 PM

Well I have never worked with health before, so is "hitpoints" the built in point system or a global var you made?
hitpoints SHOULD be appearing orange... Try this..
if global.hitpoints < 100 {
global.hitpoints+=25
}
else {
exit
}

But like I said, I am not sure..
  • 0

#3 RaptureMachine

RaptureMachine

    GMC Member

  • New Member
  • 10 posts
  • Version:GM8

Posted 11 March 2012 - 08:06 PM

Well I have never worked with health before, so is "hitpoints" the built in point system or a global var you made?
hitpoints SHOULD be appearing orange... Try this..

if global.hitpoints < 100 {
global.hitpoints+=25
}
else {
exit
}

But like I said, I am not sure..


Hmm.. That seemed to just disable the "100" health limit I had. And no, it isn't a global variable. It's in the create event of the player as "hitpoints = 100;" and that's it. But alas, the code didn't seem to work! I appreciate the help though.
  • 0

#4 Figuras

Figuras

    GMC Member

  • New Member
  • 51 posts
  • Version:GM8

Posted 11 March 2012 - 08:26 PM

What exactly doesn't work? Obviously, the potion should destroy itself in the end of the script. How does your script go on?
  • 0

#5 Mine Lad

Mine Lad

    GMC Member

  • New Member
  • 36 posts
  • Version:GM8

Posted 11 March 2012 - 08:46 PM

Try putting this in the collision event of the potion
if hitpoints<100 {
hitpoints+=25
instance_destroy()
}
else
if hitpoints=100 {
exit
}



  • 0

#6 GameGeisha

GameGeisha

    GameGeisha

  • GMC Member
  • 2747 posts
  • Version:GM:Studio

Posted 11 March 2012 - 09:11 PM

I would suggest this:
//Player collision event with potion

if (hitpoints < 100) {
  //Cap the health
  hitpoints = min(hitpoints+25, 100);
  //Destroy the potion
  with (other) {
    instance_destroy();
  }
}


@Mine Lad

Well I have never worked with health before, so is "hitpoints" the built in point system or a global var you made?

It is definitely a variable that the original poster created. The built-in ones are score, health and lives. You can easily look this up in the manual and it won't be there, that should have told you that it is user-made.

And it doesn't have to be global, either. If there are multiple instances of this object, then having one global hitpoint variable for it would pose serious problems because the instances would then all share identical HP. Unless it is explicitly declared as global using the globalvar keyword, you should have assumed that it was instance-local.

hitpoints SHOULD be appearing orange...

If it is a custom-made variable, it should definitely not highlight orange by default.

Did you even bother opening up GM to see what colour it would have been?

if global.hitpoints < 100 {
global.hitpoints+=25
}
else {
exit
}
But like I said, I am not sure..

Again, if you're not sure, don't try to help.

In fact, your code has the potential to cause more problems than it will solve, because the exit statement. If the hit points variable is greater than or equal to 100, the exit statement will stop the code right away. As a result, any code that comes after this if-else block can be disrupted by a localized source.

You should definitely read this topic as a case study. Notice how I misdiagnosed the cause several times before I correctly diagnosed the exit statement from the posted source. This should tell you why you should not use the exit statement so carelessly.

if hitpoints<100 {
hitpoints+=25
instance_destroy()
}
else
if hitpoints=100 {
exit
}

This still does not correctly solve the hitpoint cap problem. What if hitpoints is 85? You would increase it by 25 to 110 and stop there. You clearly haven't traced this piece of code very well.

GameGeisha
  • 0

#7 PetzI

PetzI

    GMC Member

  • GMC Member
  • 1026 posts
  • Version:GM8.1

Posted 11 March 2012 - 10:57 PM

In the collision with the potion event:

if hitpoints < 100
{
hitpoints = min(hitpoints + 25,100)
with (other) {instance_destroy()}
}

EDIT: I hadn't read GameGeisha's post. I basically wrote the same solution he did but with a lot less detail.

Edited by PetzI, 11 March 2012 - 11:01 PM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users