Main
Projects
Art
Music
Scripting
LiveJournal
Forum
Links
Lesson 1
3, 2, 1...

So you want to make a countdown script, but you don't know where to begin.
The first thing you need to know is how to make variables.

To make a local variable in HamsterSpeak, all you need to do is write the text variable(name), replacing name with what you want to name your variable.

Let's call our script "countdown." So far, the script looks like this:



include, plotscr.hsd
include, game.hsi #replace "game" with your filename

define script(1,countdown,none)

script,countdown,begin
 variable(counter)
end

Hold on! Where did all this "define script" junk come from? Well, in order to create a script, you have to define it. For now, let's just say that your scripts will be defined like this:
define script(number,name,none)

Each script needs a different "number" and "name." In the example above, I gave the script the name "countdown," and the number 1. It really doesn't matter.

Back to the script. We want our variable "counter" to count down from 10 to 0. We could use set variable(counter,10) to set it to 10, then 9, and so on, but that would take a long time, not to mention cluttering up our script.

Let's not do that. Fortunately, HamsterSpeak has a wonderful thing called a for loop. We can make it do the hard work for us. A for loop looks like this:


script,countdown,begin
 variable(counter)
 for(counter,10,0,-1) do,begin
 end
end

Why does it have those numbers? Let's look at it part by part.

  • "counter" is the name of our variable.
  • 10 is where we want "counter" to start.
  • 0 is where "counter" should end up.
  • -1 is how much "counter" changes every time.
  • "do" starts the loop. Anything between "begin" and "end" will be repeated until "counter" reaches 0.


  • script,countdown,begin
     variable(counter)
     for(counter,10,0,-1) do,begin
      show value(counter)
      wait(10)
     end
     show no value
    end
    

    What did I do? Every time "counter" changes, it shows "counter" on the screen. The player will know how much time is left.
    The wait tag slows the script down. Without it, the player has no time to respond to the countdown. I gave the player about 10 seconds. You can increase this by increasing either the wait time or the initial value of "counter."

    Now, the important part. What happens when the time runs out?
    Let's look at a few examples.


    script,countdown,begin
     variable(counter)
     for(counter,10,0,-1) do,begin
      show value(counter)
      wait(10)
     end
     show no value
     if (check tag(2)) then (game over)
    end
    

    This script will cause the player to lose the game if tag 2 is on when time's up.


    script,countdown,begin
     variable(counter)
     for(counter,10,0,-1) do,begin
      show value(counter)
      wait(10)
     end
     show no value
     show text box(1)
    end
    

    This script will always show text box 1. You can do other things with the text box, like fighting enemies, healing your party, et cetera.

    Remember I said something about local variables? The other type of variable is a global variable. What's the difference? Global variables can be used in any script, and when a player saves his game, the save file stores global variables.
    Let's change "counter" into a global variable. HamsterSpeak doesn't like it -- you should use local variables in your for loops -- but it will still compile.


    global variable(1,counter)
    
    script,countdown,begin
     for(counter,10,0,-1) do,begin
      show value(counter)
      wait(10)
     end
     show no value
     show text box(1)
    end
    

    Now what? There's a number next to "counter." That's its ID number. Like define script, each global variable needs a unique ID number. It doesn't matter what you pick, but it's best to declare them in numerical order, starting at 1.

    And that, as they say, is that. Lesson 1 is over, and if you can make a countdown plotscript, you've graduated.

    Return to Main Menu

    Site layout (c)2000 Jennifer Raby. Used by permission.