An object is anything in the game or a scene that is animated, moves, talks, or gets clicked. Objects are controlled and modified using special object syntax and predefined functions and properties.

Objects can be declared either with or without an initialization script, but it usually makes sense to include one (it helps to separate the code for clarity). The initialization script is executed for game objects when the player begins the game (like the main-event), and for scene objects whenver the scene is entered (like an enter-event).


object Ego
{
     setPosition(20, 350);
     setStopAnim2H(EgoSR_sprite, EgoSL_sprite);
     setWalkAnim4(EgoWU_sprite, EgoWR_sprite,
                    EgoWD_sprite, EgoWL_sprite);
     setTalkAnim2H(EgoTR_sprite, EgoTL_sprite);
     clickable = false;
}

object Bob;  // declaration without an init script

Initialization scripts are a handy way to setup objects for different conditions when the player enters a scene. Here it checks a flag variable to see if a door is open or closed.


object Door
{
     setPosition(740, 320);
     if DoorIsOpen
     {
          setAnim1(DoorOpen_sprite);
     }
     else
     {
          setAnim1(DoorClosed_sprite);
     }
}
See Also...

Object Classes

Classes are new to AGAST 1.08. You can set an object's class by setting its class property to any other object.

object MyClass { }

object MyObject
{
     class = MyClass;
}

Classes can be changed at any time simply by assigning another object to the class property. This is known as dynamic classing.

Another dynamic aspect is that you can modify the MyClass object's member variables, and it will affect all the objects whose class is set to MyClass. For more information, please read about the class property in the following links. The Introduction to Classes Tutorial explores some more possibilities by example.

See Also...

Object Member Variables

Beginning with AGAST 1.08f, objects may have member variables.

Member variables are dynamic variables that belong to an object. The dynamic aspect is what makes them unusual, and also powerful, because dynamic variables don't exist until the line of code that declares them is actually executed.

object Ego
{
     member Name = "Alex";
}

It's most appropriate to declare member variables inside the object initialization script, only because it's easier to remember where you declared them. If you are working on a large project, other programmers who read your code would expect that. But any script that is run can declare a member variable, and it will be added to the active object.

Once a member variable has been declared, it can be used just like any variable. This script would print the name of the active object.

script PrintName()
{
     print("Hi, my name is %s.", name);
}

If, in the above code, the member variable name is not present in the active object, a run-time error will occur, and a debug message will be displayed. (Although the message is not very informative at this time, it will at least bring to your attention the fact that something is wrong with your program, and you will not have to worry about trying to backtrack if something is not working as you expect.)

Using Member Variables and Classes

Member variables are especially useful when combined with classes. The following example should give you a clear idea of using member variables together with classes. (Note that, in an actual game, there would be some extra code to make the object clickable and set its clickable area, but they have been omitted from this next example for clarity.)

object RockClass
{
     member Description = "a rock";     
}

object BigRock
{
     class = RockClass;

     // Override the RockClass's
     // Description member:

     Description = "a big rock";
}

object OrdinaryRock
{
     class = RockClass;

     // Don't override; will fall back
     // on RockClass's Description.
}

event Lookat -> Rock {
     print("This is %s.", Description);
}

When the player clicks on the ordinary rock object, he will see a message box that inherits the description member from the parent class: "This is a rock." When the player clicks on the big rock, he will see the big rock's own message "This is a big rock."

The advantage is that now one single event definition can be used for every rock in the entire game, which is especially handy if your game takes place somewhere in Arizona. But of course this may also be useful for people, chairs, turtles, and stars, if you would like to use them for such things. This is just the tip of the iceburg, since members can hold things like colors, coordinates, resource indices, or anything else you'd like.

[Note: Before the addition of member variables, object fields could have been used to similar effect, by calling the SetField and GetField functions. Now, member variables should be used instead in most, if not all, cases.]

Objects can be declared as arrays, which may make indexing several objects a little more conventient. The following declaration would declare an array of ten birds.

object Birds[10];

Unfortunatley, object array syntax is not so intuitive sometimes. You may need to manually set the ActiveObject, like this:

ActiveObject = Birds[10];