dim3 Forum

Full Version: [Tutorial] Advanced Scripting - Teleporter object
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Advanced Dim3 Scripting Tutorial - Creating a teleporter using the data object and spot parameters

Requirements:
Basic understanding of JavaScript syntax and scripting in Dim3
Understanding of messages in Dim3

In this tutorial, I will teach how to create a teleporter object using the data object and spot parameters.
The teleporter should:
- Take the player to a different spot in the map
- Be easily customizable

OK, let's start by creating a simple object that has a watch and triggers a function
when the player comes near it.

Code:
var watchDistance = 3000;

function teleConstruct(obj) {

obj.model.on=true; //Object has a model
obj.model.name="teleporter"; //The model is called teleporter
obj.setting.suspend=true; //The object does not move

}

function teleSpawn(obj) {

obj.model.animation.start('Idle'); //Start an animation
obj.watch.start(watchDistance); //Start the watch

}

function teleWatch(obj,subEvent) {

//Stop if the watch was not triggered by the player coming near
if(subEvent!=DIM3_EVENT_WATCH_NEAR and !obj.watch.objectIsPlayer) return;

playerTeleport(obj);

}

function playerTeleport(obj) {

//This is still empty, we will get to it later.

}

function event(ojb,mainEvent,subEvent,id) {

switch(mainEvent) {

case DIM3_EVENT_CONSTRUCT:
teleConstruct(obj);
return;

case DIM3_EVENT_SPAWN:
teleSpawn(obj);
return;

case DIM3_EVENT_WATCH:
teleWatch(obj,subEvent);
return;

}

Notice that the playerTeleport function is still empty. We will fill that now.
First, we will have to define where the player should be teleported.
Now here comes the first tricky part: To get the position, we will place a node,
retrieve it's position, and then move the player there.
We will give the node a unique name, for example "teleDestination", and then retrieve
it's position using:
Code:
var telePosition = map.node.getPosition(map.node.find('teleDestination'));
Now we will store the position using the data object like this:
Code:
data.add('telePosX',telePosition.x);
This will add a data variable that can be accessed by any script, called telePosX that
contains the x coordinate of the node.
Do the same for Y and Z.
Now we have to let the player know that we want to move there. Use a message to
trigger a function in the player script that retrieves the data usin
Code:
data.get('telePosX');
and the same with Z and Y of course, and then moves the player object using
Code:
obj.position.place(x,z,y,ang);

Your script should now work and teleport the player to the correct node.
You might want to add another variable for the angle, if you need that.

Now what about a different teleporter, that teleports to a different node?
Maybe with a different watch distance?
To do that, use object parameters.
When you double-click a spot in the editor, there are 10 fields at the bottom, in which
you can enter something. For example? The name of the node to teleport to. Or the watch
distance. Or something else.
To retrieve the parameter, use
Code:
obj.setting.getParameter(n);
where n is the number of the parameter. Careful: The first one is 0, not 1!
This has to be used after the object has spawned, so use the spawn function to retrieve the
parameters. An example: At the top of the script define the variable watchDistance.
In the spawn function, set watchDistance to the first parameter like this:
Code:
watchDistance = obj.setting.getParameter(0);
and then start the watch using the watchDistance variable.
Depending on what number you enter in the spot settings, the watch distance will change.
But be careful: obj.setting.getParameter(n); returns a string!
You will have to convert it to an integer for some functions. (For example if you want to use it as the watch distance.)
To do that, use the parseInt(str); function. This will turn a string into an integer. Example:
Code:
watchDistance = parseInt(obj.setting.getParameter(0));

Now you should be able to write a simple teleporter script that can be used over and over again,
yet acts differently every time, depending on the spot parameters. You might want to add
effects or animations too, but that's up to you.

If you have any questions or found a mistake, let me know!
I'll try to fix it as fast as I can.

NOTE: This was written on an iBook G3 without Dim3, and I could not test the script!
Please tell me if there's anything wrong with it, so that I can fix it!
This is also my first tutorial. If you didn't understand something, tell me and I will try to explain it.

@Brian: It would be nice if you could move this to the tutorials section, when it's done. Smile
You probably wanna make a parameter for the node name so you can make multiple teleporters....

Also note that most games will have a teleporter entrance and exit, so instead of using a node most people will use models (another teleporter).

[EDIT]also, instead of using a watch use the touch event. Wink
ccccc: I think you misunderstood something. Smile
This is not a tutorial on how to make a teleporter for a specific game, but a tutorial about using the data object and spot parameters. The teleporter is just the example, because it shows both things nicely.
Of course there are a hundred ways to do teleporters. I'm using this version in Origami Bots, because it suites the game.
The teleporter script in Origami Bots is extremely customizable.
All the spot parameters are used up completely, to make the script work. This includes:
Model on/off, Teleporter starts on/off (if off, it has to be turned on by a trigger message), watch distance, teleport to node or to a new map, teleport node/map name, turns off after teleport/stay on, trigger a movement after teleport, movement name, flash color (for tinting the screen when teleporting) and flash time (0 for no flash). Smile
There are thousands of ways to do it, and this is only one of them.
Ok, I get it. Smile
A few bugs in the script

DIM3_EVENT_WATCH_NEAR should be DIM3_EVENT_WATCH_OBJECT_NEAR

typo:
Code:
function event(ojb,mainEvent,subEvent,id) {

ojb --> obj

also probably worth mentioning how to send messages to the player, and how to catch them.
Full playerTeleport():

Code:
function playerTeleport(obj) {
var telePosition = map.node.getPosition(map.node.find('teleDestination'));

data.add('telePosX',telePosition.x);

data.add('telePosY',telePosition.y);

data.add('telePosZ',telePosition.z);

obj.event.sendMessageToPlayer(5);
}

and in player.js

just below
Code:
function playerMessage(obj,subEvent,id)
{
and above
Code:
        // we are only checking for player_X key
        // message here, we can ignore all others
           if (subEvent!=DIM3_EVENT_MESSAGE_FROM_KEY_DOWN) return;

add:
Code:
if(id==5){
        // Teleport Player
        iface.console.write("Teleport!");
        teleportDo(obj);
    }

and anwhere in the Player.js add the teleportDo() function
Code:
function teleportDo(obj){
    var y,x,z;
    y = data.get('telePosY');
    z = data.get('telePosZ');
    x = data.get('telePosX');
    obj.position.place(x,z,y,0);

}

Worth noting that i haven't bothered here to copy the start angle across (its just set to 0), however the method how to do it should be obvious.
Reference URL's