Page 1 of 1

textNumber

PostPosted: Sun Nov 25, 2007 7:49 pm
by Super Pieman
I am making a shooter game and your maximum ammo for one gun is 154. I have made it so that you can only pick up the ammo when you are not full. For some reason when you pick up the ammo it shows waht you would get for half a second and then goes down to 154. Here is the script.
Code: Select all
if (textNumber>154)
{
     textNumber=154;
}


Is there a way to make it so that it immediatly shows 154.

Re: textNumber

PostPosted: Sun Nov 25, 2007 9:10 pm
by pixelpoop
Make sure that that code is with the code that allows you to pick up or not pick up ammo. If the code is separate then it could be that GE runs the pickup code, then advances a frame, then runs the ammo limit code. If this is the case then you would see the number shift for a brief frame.

Re: textNumber

PostPosted: Mon Nov 26, 2007 5:01 am
by Fuzzy
Dont do math operations on textNumber. Its prone to errors. Store your data in appropriately named variables; its just good coding practice.

Code: Select all
ammo = min(ammo, 154);


You dont need an if here. The opposite would be max(ammo, 154), which would prevent it from going under 154. you can use this to assure that ammo never falls below zero.

Code: Select all
ammo = min(max(ammo, 0), 154);


now it is not possible for ammo to be anything but a range from 0 to 154

When you are coding, ask yourself "am i SURE i always want the computer to do this?" if the answer is yes, you probably dont need an if. Basically, if the decision is already known before the program runs, then you dont want to use if. You want to tell the compiler "do this, and dont think about it".

After that, assign it to the textNumber

Code: Select all
textNumber = ammo;


and you will never see a value outside the range you want.