SOLVED Game.Score or Global Int for count-down time system?

Started by Allawayjh, Sun 02/10/2016 18:27:35

Previous topic - Next topic

Allawayjh

Hello! Jen here.

I'm working on a game where I want time to be a major mechanic, but not ACTUAL time. The longer someone plays as one character, I want the world to have distorted versions of object images, objects to disappear, dialogue to change, etc.

When I thought of how to do this in the code, I immediately wanted to use a countdown system rather than actual time because that would allow for the player to take things in at their own pace. However, I'm not sure how to approach this in code.

I'm not sure I understand the game.score function and how to manipulate it. It's my first instinct for how to accomplish this. Whenever the player does something or has a conversation, reduce score by 1, when score reaches x number, then put if/then functions on all the ojbects/backgrounds in game to change out the asset images, etc.

The other potential way to do it is make a global variable "Time" that's an int, with a starting value that shrinks down and resets whenever you return to character select, but again, I'm not sure how to code that properly.

I'm sorry if this question is too vague. Let me know if I can clarify anything.

Jen

Snarky

I would use a global variable rather than game.score, just because game.score has side effects.

So create a global int, as you say. "Time" is OK, but it's usual to start variables with a lowercase letter, and personally I like to use a name that is a little more descriptive about what the variable stores, so I would maybe recommend something like "timeCounter" or "timeCountdown".

In order to reduce time by 1, we use the decrement operation:

Code: ags
  timeCounter--;


The double minus is a shortcut for:

Code: ags
  timeCounter = timeCounter - 1;


You could just put a line like this in all the functions for actions that should count down, but in order to handle the switching of the graphics, I would instead make a couple of functions. Put this in your global script header:

Code: ags
void TimeCounterDecrement(); // Reduces the time counter by 1
void TimeCounterReset();     // Resets the time counter to the starting value


Now to actually write the functions, put this in your global script:

Code: ags
// We define the time threshold values as constants so that if we want to change them later, we can do it in one place.
// It also makes the code more readable. Avoid having lots of random numbers sprinkled throughout the code.
#define TIME_START 100
#define TIME_DISTORT_A 80
#define TIME_DISTORT_B 50
#define TIME_DISTORT_C 20

void TimeCounterDecrement()
{
  timeCounter--;
  if(timeCounter == TIME_DISTORT_A)    // We've reached the first threshold
  {
    // Code to switch out the graphics, etc. goes here
  }
  else if(timeCounter == TIME_DISTORT_B)
  {
    // Switch out the graphics, etc.
  }
  else if(timeCounter == TIME_DISTORT_C)
  {
    // Switch out the graphics, etc.
  }
}

void TimeCounterReset()
{
  if(timeCounter <= TIME_DISTORT_A)
  {
    // Switch graphics back
  }
  timeCounter = TIME_START;
}


Now in any function that should count down (e.g. starting a conversation, or whatever), put this line:

Code: ags
  TimeCounterDecrement();


And in the function to switch characters, put this:

Code: ags
  TimeCounterReset();


Hope that makes sense!

An "even better" way is to use structs, but it's not necessary to learn about that just to do this.

Allawayjh


SMF spam blocked by CleanTalk