Living Samples - Name Me

The following sample of code demonstrates one way to name the current living object. In this sample, we'll name the object by sending it a message from our playable character.

// Living Samples - Name Me
function onCreate() {
    this.owner.sendMessage('Hi there! My name is ' + this.name);
}

function onMessage(sender, message) {
    if (sender != this.owner) return;

    if (message.startsWith('set name')) {
        try {
            this.setName(message.substr(9));
            this.sendMessageTo(sender, 'Very well! My name is now ' + this.name);
        } catch (ex) {
            this.sendMessageTo(sender, 'I am unable to change my name. Reason: ' + ex.message);
        }
    }
}
The above code will result in the computer character, let's call him 'Johnny', sending a message to us containing
Hi there! My name is Johnny
We can then reply with
tell my johnny set name Jimmy
to which the computer character will reply
Very well! My name is now Jimmy
Let's have a look at the message that we sent to Johnny. The first part is tell which is how a message is sent to a specific player or computer character. The next part of the message is my, which simply means that we need to only consider computer characters that you own. Next comes johnny which is obviously the name of the computer character that you own. Finally set name Jimmy is the message that will be sent to the computer character.