April 5, 2017

Tiny Script: Part II

First of all, I could not think of a good idea about which photo to include in this post. So, I decided to use one of mine.

Continuing with my previous post, I'll show you a few more TinyScript features.

Let's start by dissecting the different types of scripts which are divided into two logical groups: Declaration and Execution.

Declaration scripts are used to initialize and declare the whole world. They are executed during the initialization phase which occurs when the game app starts. Once the initialization phase ends, all declaration scripts are automatically disposed from memory as they are no longer needed. There are three types of declaration scripts:
    • Global - Used to initialize global stuff such as sounds, music, atlases and all sort of global things you can think of.
    • Room - Used to declare rooms.
    • Thing - Used to declare things.
On the other side, execution scripts remain always in memory as they must be progressively executed while the game runs to support the game logic. There four types of scripts:
    • EnterRoom. Occurs when entering a room.
    • ExitRoom. Occurs when exiting a room.
    • Outcome. Occurs as a result of interacting with a 'thing' (e.g., when the avatar opens a door.)
    • Routine. A special type of script that can be called by any other script.
For example, consider the following code:

Room Bedroom

EnterRoom Bedroom
    start-routine FrogSound

ExitRoom BedRoom
    stop-routine FrogSound

Thing Bedroom-Door
    => DisplayName = "Door"
    place this into Bedroom at 100,100

Outcome Bedroom-Door
    play-soundcue OpenDoor
    anim this Open

Routine FrogSound
    play-soundcue Frog
    sleep 3000

So, what the heck does all this crappy code do?
First of all, it declares a room named 'Bedroom'. The EnterRoom script will start the  'FrogSound' routine as soon as the 'Bedroom' room becomes active. Thie 'FrogSound' routine will play the sound of a frog, then wait 3 seconds (3000 ms) and finally restarts all over again.

Funny TinyScript moment: the sleep duration can also be a random number. For example random-sleep 4000 10000 will result in a random number between 4000 and 10000. It is a bit annoying to hear a frog with the precision of a Swiss watch.

One of the cool features of TinyScript is that each script runs in it's own virtual process. As a result, the 'FrogSound' script will continue its execution -beyond the current room- until someone stops it by calling the 'stop-routine' command.

Static vs. Dynamic
The Bedroom and Bedroom-Door are static entities. This means that there will be one Bedroom room and one Bedroom-Door in the game universe as soon as the game starts. Once created, static entities cannot be removed from the world by any means. For example, I can declare a MainMenu room.

BUT, what happens when I need to have several instances of the same type of entity???????????????????????????????????????????????????????????????????
?????????????????????????????????????????? (and more ????)

Well, this is what dynamic entities are for. Although the game has many static rooms and things, it also have some kind of silly procedural room generation that requires the ability to create and destroy  rooms and things on the fly; the solution comes from an asterisk! This magnificent symbol has the power to convert a static entity declaration into a dynamic one as follows:

Thing Door*
    => DisplayName = "Dynamic Door"

Outcome Door*
    play-soundcue OpenDoor
    anim this Open

The above code is more like the Class & Object concept. A dynamic declaration is never removed from memory and it can be executed as many times as needed to create dynamic entities on the fly. Dynamic entities can be created with the create-entity command or they can also be created directly from C# code too.

There is much more to talk about but I'm tired. Out for an expresso!

- Diego

No comments:

Post a Comment