Adding to a variable?

Started by The Love Below, Sun 09/10/2005 05:04:23

Previous topic - Next topic

The Love Below

I'm having some trouble with my code. What I want is to add 1 to a variable each time a key is pressed until that variable equals 10. I don't really know what I'm doing so I guess I've probably got it wrong.

Code: ags

int drink = 1;  
  
	if (drink < 10) {
		if (IsKeyPressed(68) == 1) {
			drink += 1;
			character[DOOD].LockView(2);
			character[DOOD].Animate (1, 5,  0, eBlock,  eForwards);
			} 
		}
			if (drink > 10) {
			  if (IsKeyPressed(68) == 1) {  
				DisplayMessage(0);
				}
			}


Please forgive my ignorance, especially if this is covered in the manual somewhere...

GarageGothic

The first thing that I notice is the line:

  drink += 1;

What you want is either:

  drink = drink + 1;

or just:

  drink++;

which adds 1 to the value.

The Love Below

Thanks, I was just kinda guessing with the "drink += 1;" part. Unfortunately I changed it to "drink++;" and it still didn't do what I wanted. I've done the code again and now it looks like this...

Code: ags

int drink = 0;
if (IsKeyPressed(68) == 1) {
  drink++;
  }
  if ((drink < 10) && (IsKeyPressed(68) == 1)) {
	character[DOOD].LockView(2);
	character[DOOD].Animate (1, 5,  0, eBlock,  eForwards); 
		}
	if ((drink >= 10) && (IsKeyPressed(68) == 1)) {
	  DisplayMessage(0);
	  }
   }


This still doesn't work for me though. I can just keep pushing the button forever. However, when I accidentally had the < and the > around the wrong way, the second part worked. Meaning when I pushed the button the animation didn't play and the message came up; which is what I want, but only after the button has been pressed 10 times.

GarageGothic

#3
Where in the script are you running this? In the Repeatedly_Execute? If so, you must realize that it's running 40 times per second (depending on game speed), and IsKeyPressed is checked every time, thus going past 10 within 1/4 second if the key is held down.

You should rather use the on_key_press event function if you only want it to update every time a key is pressed.

Edit: No, wait, if the script looks like what you have here, the int drink is defined within the function itself. Thus resetting to zero every time and never getting beyond 1. You should define the variable either at the top of the room script or the global script depending on where you want the function to run.

The Love Below

#4
So, um, how would I script that? And where should I put it?
EDIT: On second thought I think I might know...sorry about this...
EDIT: Just read your edit, I'll try that.
EDIT: Thanks very much! That works!

strazer

Quote from: GarageGothic on Sun 09/10/2005 05:37:15
The first thing that I notice is the line:

  drink += 1;

What you want is either:

  drink = drink + 1;

or just:

  drink++;

which adds 1 to the value.

  drink += 1;
does the same thing. It doesn't matter which of these three you use.

SMF spam blocked by CleanTalk