Gw Temp


Tutorial - 'Enteties In Sphere' by rjt

An item about Sphere posted on


This tutorial covers information on enteties and can be a useful reference.


In Sphere, people and events are classed under the title entities. There are two types of entities: Persons and Triggers. Persons are usually character's and NPCs, while Triggers are the Sphere equivalent of the RM2k's Events (they’re pieces of code run when the character walks over them). Both can be added by right clicking on the place you want to add the trigger/person (in the map editor) and by selecting Insert Entity ->. Persons consist of a spriteset, a name and five events. These events are:

On Create: This script is run when the entity is created. This is either when the map is first run (in this case the entity must have been added in the map editor) or when CreatePerson(name, spriteset, destroy). Destroy is either true or false, and used to select if you want to destry the character during map switches. The entity is created at the Entry Point of the map.

On Destroy: Run when the entity is destroyed. This can be if the map closes and the entity has a Destroy value of true, or when the DestroyPerson(name) is used.

On Activate (Touch): This is run when the main character's feet touches of the feet of the owner of this script's feet. The main character is the character that you used AttachInput() with (in other words, the character that takes input from the keyboard).

On Activate (Talk): This is run when the main character is within talking distance of the entity and the talk key is pressed. Talking Distance is set to 8 by default, though you can change it with SetTalkDistance(Pixels); (it can be check your talk distance with GetTalkDistance();). The talk key is space bar by default, but can be changed using SetTalkActivationKey(Key). Key is in the format: KEY_SPACE, or KEY_A, or KEY_ENTER. A full list of keys can be viewed in the keys.txt file in the docs folder (or in the editor Help -> Local Documentation).

On Generate Commands: Basically, when an entity isn’t moving, and there’s no input coming from the keyboard to the entity or any commands coming from scripts to the entity, it’s On Generate Commands script is run. Stuff for controlling random movement or similar scripts are put here.

You can also set entity’s script through a script using SetPersonScript(EntityName, ScriptName, NewScript); ScriptName is one of the following: SCRIPT_ON_CREATE, SCRIPT_ON_DESTROY, SCRIPT_ON_ACTIVATE_TOUCH, SCRIPT_ON_ACTIVATE_TALK, SCRIPT_COMMAND_GENERATOR. For Example:

SetPersonScript(“Bob”, SCRIPT_ON_ACTIVATE_TALK, “GetSystemFont().drawText(1, 1, ‘Hi’); FlipScreen(); GetKey();”);

This would look much better if we could condense those three lines of script (the ones that show ‘Hi’) into one line. To do this, put this in your main script:

function MessageBox(x, y, text)
Font = GetSystemFont()
Width = GetScreenWidth()
Font.drawTextBox(x, y, Width, Font.getStringHeight(text, Width), 1, text)

Then use: SetPersonScript(“Bob”, SCRIPT_ON_ACTIVATE_TALK, “MessageBox(1, 1, ‘Hi’);”);

This is just defining a simple function. You can use any function, from an entity or trigger, that you defined in your main script. If you wanted to use a function from another script, you would use EvaluateScript(ScriptName) in your main script.
What if you wanted one entity to follow another like the main characters follow Squall in FF8? There is a function to do this: FollowPerson(Folowee, Follower, Distance);
The Followee follows the Follower at Distance (which is measured in pixels). To demonstrate this, I’ll show you a program:

If you have not already done so, make a new basic project (as described in ‘Sphere Basics 1’). All we’ll need is a map, a spriteset and a script to start a new game using that spriteset on the map you made. Make a new trigger on your map (somewhere you’ll remember) and put this code in it:

CreatePerson(-1, "Character.rss", false);
for(var i = 0; i < 20; i++)
h = i - 1;
CreatePerson(i, "Character.rss", false);
FollowPerson(i, h, 10);
FollowPerson(0, "bob", 10);

On lines 1 and 5, change Character.rss to the name of your spriteset, and on the last line change bob to the name of your character. Test the game, and walk onto the trigger. Start walking away from it, and you’ll see many entities following you. If you want more entities to appear, then change the 20 on line 2 of the code to something greater.

That’s it for this tutorial. I didn’t say much about triggers, but they’re very easy to understand so there was little need to get into much detail. If you want to send any feedback, my email and MSN address is