GameMaker: Studio

GameMaker: Studio

View Stats:
FoxWasp Dec 4, 2014 @ 5:43am
Detecting if a variable has changed?
For a game I'm working on, I'm looking for a way to tell the game to do something if the player has lost health. Is there a way that I could somehow compare Player.Health in this frame to Player.Health in the previous frame?
I'd like to be able to measure how much the change was e.g. so I could spawn more blood if you take alot of damage at once.
Anyone have any ideas on how I could do this?
< >
Showing 1-8 of 8 comments
Blind Dec 4, 2014 @ 8:31am 
While I believe this would be an actually useful function, I don't think it natively exists (except for xprevious and yprevious). So basically, at the start of your Begin Step (or main step if you aren't doing those) just have a variable similar to "oldhealth" and go "oldhealth=health" and then later check if oldhealth and health are equal.
Sera Dec 4, 2014 @ 8:46am 
For what it's worth, xprevious and yprevious aren't a different concept than the oldhealth idea. They're just automated variables built into any given object; difference with the health is you need to handle that automated bit yourself every frame. I would advise to have oldhealth = health be one of the later calls you make in your step event, though; you don't want to reset it before finding any differences, so make sure your comparison logic comes first.
Daynar Dec 4, 2014 @ 9:31am 
Another way to do this would be to only allow player hp to be changed through a script that accepts the desired amount of damage as well as the type of damage (if nessissary) that way you can ensure anything you want to acount for in the damage amount (defensive stats buffs etc) are taken account for in one place and spawn the nessissary amount of blood or whatever for the amount of damage delt.

That said an oldHealth varible would work just fine for the blood and be relativly easy to impliment. If your simply going to have one check for it I'd set it right after the check in order to illiminate the chance of the health changing between the last check and the oldhealth = health. (for example if you checked oldhealth then the enemy step event or whatever happened and the player was damaged and then in the end step event you set oldhealth=health you'll completely miss the damage in the oldhealth check)
Blind Dec 4, 2014 @ 10:01am 
Ya, it should be either the first operation or the last operation in a step. That's why I said to put it in the Begin Step - since I like to make sure any precalculations (such as attack/defense mods) are synchronized across all instances before dealing damage.
FoxWasp Dec 4, 2014 @ 6:41pm 
I tried putting a code that says
oldlife = life;
in the End Step event on my Player object, and code that says
if life <= oldlife-5 { invuln = 1};
in the Player step event so the player becomes invulnrable if they take 5 or more damage at once.

I cannot get it to work, though. I've tried placing the invuln code at the start of the step event, at the end of the step event, and in the Begin Step event, but I can't get it to work. I'll touch an object that does 10 damage to the player, but the invulnrability won't activate. Anyone maybe know why?
Last edited by FoxWasp; Dec 4, 2014 @ 6:42pm
Blind Dec 4, 2014 @ 11:27pm 
Well, I say this a lot - but run a variable trap... in this case, "life-oldlife" is what you want to track. (and to break on a non-zero)

And since this will be a very fast detection, add a particle effect to it such as the default explosion.
Last edited by Blind; Dec 4, 2014 @ 11:30pm
Sera Dec 5, 2014 @ 12:00am 
The life update may also just be working fine; the problem might lie in your invincibility tracking.
FoxWasp Dec 5, 2014 @ 3:10pm 
Thanks guys, I got it working!
Turned out that the best thing was to put the on-hit effects and the oldhp varaible storage into the same script. Just put the on-hit effects at the top, and make storing oldhp the last thing the script does and it works out well. Thanks again!
< >
Showing 1-8 of 8 comments
Per page: 1530 50

Date Posted: Dec 4, 2014 @ 5:43am
Posts: 8