Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: gamemaka on Wed 13/04/2005 05:20:19

Title: Globalint restrictions [SOLVED]
Post by: gamemaka on Wed 13/04/2005 05:20:19
On my RPG, when a person uses a potion or mega potion, their health increases. 15 potion and 50 for mega. Is there a way I can do the source so that when they use a potion and their health is 90, their health doesn't increase to 105 but stops at 100. It would suck having to do TONS of conditionals for "If health is 90, add 10." and so on and so on. Please I NEED HELP!
Title: Re: Globalint restrictions
Post by: Gilbert on Wed 13/04/2005 05:23:44
[pseudo code]
if (health>100) health=100;
[/pseudo code]
Title: Re: Globalint restrictions
Post by: gamemaka on Wed 13/04/2005 05:31:06
Thank you so much!
Title: Re: Globalint restrictions
Post by: stuh505 on Wed 13/04/2005 06:07:23
To have good style, you should not simply manipulate the health variable when they drink a potion.

Instead, you should call a function that you write like Heal(x) where X is the amount to heal.  Then inside the heal function you add to their life, and perform the check to make sure they don't go over maximum health.  This way you only have to put the check in one place in your whole game, and it is in an abstract little box that you don't have to think about anymore once you have it working.

This applies in general to all things you will want to do.
Title: Re: Globalint restrictions
Post by: Gilbert on Wed 13/04/2005 06:13:39
Well whether one should use functions for everything is personal preference.
In my opinion, I'll judge whether I shall make a new function for something according to the complexness, if the task is simple enough I won't handle it as a function.
Title: Re: Globalint restrictions
Post by: stuh505 on Wed 13/04/2005 14:29:48
yes, obviously if everything is a function then you will have an infinite loop with no actual code...

however, any time you want other procedures to be ALWAYS done along with another procedure, it is a good indication that they should be wrapped up and associated together under one function.  it's a good way to keep your code shorter, decrease likelihood of writing an error by reducing code, and keep everything consistent

putting a maximum on the health isn't the only thing you might want to always do when adding health.  what if you decided to change your health system so that drinking potions did not immediately raise health but set a target health that was a certain amount higher than current health...and over time you would heal to that amount.  if you had implemented this with a function in the first place you could make this change to your whole game with a few lines of code.

anyway, im ranting  :P