Skip to content

Client Script Introduction

TheUnknownOne edited this page Jul 5, 2013 · 12 revisions

Scripting/Client/Introduction

Client scripting is a way to implement additional features in your Pokémon Online client, within the limits of available events and functions.

Getting started

The default client has no way of accepting client scripts right away. The necessary plugin, however, is downloaded with the latest client.

  • On the Pokémon Online client and after connecting to a server, click Plugins, then Plugin Manager
  • Click Add Plugin and open ClientScripting.dll/ClientScripting.so (depending on your Operating System)
  • Back on the Pokémon Online client, click Plugins again, and this time click Script Window

This window is where scripts are going to be given to the client. Once the window is closed, the client will run every command inside it, and eventually return errors. Client scripting uses JavaScript, so any function and method native to JavaScript will work for Pokémon Online scripts, as well as any of these functions.
In order to accomplish something more complex than simply running each command once, events can be used: with these, it's possible to make the client run certain parts of code every time an event is triggered, for example every time it receives a PM. Consult this list for every event available to client scripts.

Actually using events looks like this:

    events = ({
        eventName1: function(parameters) {
            actual code
        },
        eventName2: function(parameters) {
            more code
        }
    })

Note that you cannot put anything after this block. If you do need functions or variables, put them above the ({ }) part.

Some examples

A very simple script that lets you reconnect to the server you're on, in case of lag or disconnects.

    events = ({
        beforeSendMessage: function(message, channel) {
            if (message === "reconnect") {
                sys.stopEvent();
                client.reconnect();
            }
        }
    })

Every time you try and send "reconnect" from your client, it will stop that message from being sent, then force a connection retry to the server you're on.

If you want a better way to recognize commands, you can try using a handler like this

    events = ({
        beforeSendMessage: function(message, channel) {
            var command = "",
                commandData = "",
                index = -1;

            if (message.charAt(0) === "/") {
                command = message.substr(1);
                index   = command.indexOf(" ");

                if (index != -1) {
                    command     = command.substr(0, index);
                    commandData = command.substr(index + 1);
                }

                command = command.toLowerCase();
                
                // Commands go here
            }
            // Insert more stuff at this point
        }
    })

If you put that at the top of the beforeSendMessage block, every message you send that has "/" as its first character will be treated as a command, using the same format as server commands.
You can then use lines such as if (command === "reconnect") to easily create client commands. Remember to use sys.stopEvent(); in every command and to end it with a return; or you might accidentally trigger more than you intended to.