dim3 Forum

Full Version: Messages
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
In this tutorial, I will explain one of the most important aspects of scripting in Dim3.
Messages.

Note: Read ccccc's scripting tutorial first. You need it.

What is a message?
A message is basically a way to send information from script to script.
Script A sends a number to script B and script B then executes the appropriate action.

How do I use them?
There are a few built in functions for sending messages in Dim3.
Look at the Event Object page in the Docs and you will see:

Code:
game/course/obj/weap/proj
    event
        sendMessage(to,name,messageId);               to is DIM3_MESSAGE_TO_???
        sendMessageToPlayer(messageId);
        sendMessageToHeldWeapon(messageId);
        sendMessageToSpawnWeapon(messageId);
        sendMessageToObjectById(objectId,messageId);
        sendMessageToObjectByName(objectName,messageId);
        sendMessageToCourse(messageId);
        sendMessageToGame(messageId);

Sending a message from a weapon script to the player script would look like this:

Code:
weap.event.sendMessageToPlayer(<someMessageID>);

The message id can be any number you want.
Let's see how the player script deals with this message:
Whenever a script receives a message, there is a DIM3_EVENT_MESSAGE event.
To act upon receiving the event, you have to add this even to your event function:

Code:
function event(obj,mainEvent,subEvent,id,tick)
{
    switch (mainEvent) {

        case DIM3_EVENT_MESSAGE:
            objectMessage(obj,subEvent,id);
            return;
            
    }
}

This will call the objectMessage function whenever the object receives a message and pass it the obj, subEvent and id variables.
subEvent can be any of the following, depending on where the message came from:

Code:
DIM3_EVENT_MESSAGE_FROM_SCRIPT //message came from a script
DIM3_EVENT_MESSAGE_FROM_COURSE //message came from a map (by entering a portal for example)
DIM3_EVENT_MESSAGE_FROM_KEY_DOWN //message came from pressing a key
DIM3_EVENT_MESSAGE_FROM_KEY_UP //message came from letting go of a key

id is the number you sent. (<someMessageID in the sending messages example)
The objectMessage function would look like this:

Code:
function objectMessage(obj,subEvent,id) {
    switch(id) {
        case 1:
            doSomething();
            return;
        case 2:
            doSomethingElse();
            return;
    }
}

Now if you send a message to the object using one of the functions mentioned earlier, it will check if the id is 1 or 2 and do something or something else, depending on the id.
You can use this to call animations or change certain values or whatever you can think off.

If you have more questions, PM me on the forum or ask on IRC or AIM. Smile

Reference URL's