Infinite mana problem

01/02/2012 23:14 12k#16
Quote:
Originally Posted by pro4never View Post
Personally I'd write an accessor for mana which makes sure it never goes negative...

example..

public ushort Mana
{
get {return _mana;}
set{if(value < 0) value = 0; _mana = value;}
}
}

Because its a ushort and not a int, that won't actually work because it never goes below 0. So you would have to do something like this each time your subtracting from mana.

Code:
if (Amount > Mana)
{ Mana = 0; }
else { Mana -= Amount; }


#edit, little late on making that response haha, lateralus beat me


#edit 2


Quote:
Originally Posted by macht102 View Post
ok now the problem is mana doesnt decrease after casting a spell

even if i do this for example under public void UseSkill

if (SkillId == 1000)
{
if (SkillLvl == 0)
{
if (CurMP >= 1)
{
CurMP -= 1;
MyClient.SendPacket(General.MyPackets.Vital(UID, 2, CurMP));
}
else
{
MyClient.SendPacket(General.MyPackets.SendMsg(MyCl ient.MessageId, "SYSTEM", Name, "You do not have enough mana to cast this spell!", 2005));
}
}
That is JUST for thunder. and Elementary level for thunder.
01/03/2012 01:03 pro4never#17
Quote:
Originally Posted by Lateralus View Post
If he's going to do that, he's going to have to cast the (implicitly casted) int to a ushort before setting the value, which means that the value will never be < 0 and he'd have the same problem he has now. I'd also suggest using Math.Max... It looks cleaner, is probably inlined, and does the exact same thing.
Yes I realize both of those points. Sorry for not correcting it at the time, was a bit sleepy xD.

@OP: To simplify what they are saying.... Most math (unless specifically told otherwise) will return an integer value. Because of that you will often get issues such as what you're facing where it's saying take int X away from ushort Y. Because ushort can never be negative it loops back to max value. The solution I posted was silly of me because well.. it's wrong :P I'm checking for a negative value after it's already been looped back to max.

Also, Math library functions are lovely and should be used when applicable (no clue why I didn't use them at the time but w/e, gonna blame being super tired)
01/03/2012 01:51 macht102#18
problem solved.

#request close