7. ACTION OBJECTS
objects, action callback, status, access




The action callback

Action objects are dynamic brushes that can trigger a response when the player presses the action key, while standing in front of them. They are used for a wide range of puzzles, representing game characters the player must interact with or various devices that need to be fixed.

Start from the default template set of data and open the map in the editor. Choose the tile with the id 171, representing the Daisy character. Open the properties dialog and set the brush type to dynamic, the brush id to 1000 and the brush class to action. Place it on the map at the 560,232 position. Save the map and run the game. Try to press action key while standing in front of Daisy. Nothing special will happen! The inventory dialog will open as usual. That's because you didn't wrote a script callback to respond to this event.

Open the game.gs file and add the folowing function at the end of it:



Save the file and run the game. Try again the action key in front of Daisy. This time everything works fine and the function above is called. The small conversation is displayed.

Action_ID()

This latent callback is requested by the action handler when the player presses the action key in front of an action object. Action objects are dynamic brushes with the action class and a valid brush id. This id value is used in the name of the function (ID) to associate it with it's coresponding object. This way, such a callback will be called only for it's intended object. And since the object's id is unique, the function will have an unique name.

These functions are to be added by the game developer in the game.gs file.


The Status property

Sometimes in a game, a character will say something the first time when you interact with him and another thing, when you interact again. It's like he remembers you already talked to him. This is done by remembering the status of the conversation in a property of the object, usually the status property. Before trying this, let's see how to access the objects from the map and their properties.

ObjFind( id )

This function, exported by the engine, is used to search a dynamic brush from the map, by it's unique id property. If a brush with the specified id exists, it returns the integer index in the internal list of dynamic brushes. This index is used in other object access functions exported from the engine. If there's no brush with the specified id in the map, this function returns -1.

ObjGet( idx, property )

This function, exported by the engine, is used to get the integer value of a property of a dynamic brush. It uses the brush's internal index obtained with the ObjFind function. The property parameter is the integer value representing the brush's property and it's usually specified using the exported defines, like O_X, O_Y, O_ID, O_STATUS, etc.

ObjSet( idx, property, value )

This function, exported by the engine, is used to set the integer value of a property of a dynamic brush. It uses the brush's internal index obtained with the ObjFind function.


Open the game.gs file and edit the action callback you added before. Write the folowing content instead of the old one:



As you can see, after the first conversation, the status property is changed from 0 (default) to 1. When you interact with Daisy again, the status property is found different than zero and the second message is displayed.


Homework

Try to implement a puzzle with two characters, Daisy and Denzil.

When you first talk with Daisy, make her send you to find Denzil and ask him how he feels.
Then, after you talk to him, return to Daisy with the answer.
Use the status property to remember the status of the puzzle, as showed in this chapter.
You'll have to use a different id for the Denzil's dynamic brush (for example 1001) and write an action callback for him too.

You'll use only the simple things you have learned, but the trick is to cover all the situations and to avoid things like characters saying "hello" twice. Sometime this kind of puzzles can get pretty complex, with many branches, so you have to pay attention and test it well. But getting it right, will give more realism to your game.

After you try your best with this
homework, check this solution.

For more details about objects and action flow, read the folowing reference links:
default template action functions
the action handler
exported objects properties and functions.