So, I'm going to create a few sticky topics with some scripting basics for those who are interested. This is easier than creating wiki pages at the moment but they will eventually migrate there. Once that happens then I will unsticky these and add a new "see the wiki" sticky topic.
For the first example script, I show the basics of hooking a script into one of the many events provided by the game. In this case "playerJoined" which is the most common script hook.
In single player, you can create a mods/scripts directory underneath where you run Mythruna. Copy this script in and it will run whenever you start a single player game.
On a server, this script would go in the scripts sub-directory under where you run the server.
Comments hopefully explain what is happening.
/**
* There are many hooks that an init script can add code to.
* In single player, there are:
* playerJoined: called when a player has actually joined the world
* playerLeft: called when a player leaves the world
* cellChanged: called whenever a block is changed in the world.
*
* On the server, there are also:
* serverStarted: called when the server first starts up
* serverStopping: called when the server is shutting down gracefully.
* newConnection: called when a new connection is first received.
* This can be used to hook IP-ban lists, etc.
* playerConnected: called once a player has fully logged in. playerJoined
* is triggered after this event.
* playerDisconnected: called when a player has disconnected. playerLeft
* is triggered before this event.
*
* In this example we hook the playerJoined event since that
* is one of the most common ones and can be useful in both
* client and server.
*/
on( [playerJoined] ) {
type, event ->
// Here we can add things to the player or send them messages.
// Typical uses for these scripts would be to set new tools
// or commands on the player.
// The player entity is provided as part of the environment to this
// script hook. So we can access things about the player such as
// their name... right now in single player the player does not
// have a name so we will use groovy's inline default operator
def myName = player.name?:"Unknown"
console.echo( "Welcome to Mythruna, " + myName );
// Here we will add a simple test command to the player's
// in-game console. Typing /hello in the console will
// then echo back "Hello, world." to the player.
addShellCommand( shell, "hello", "Just a test.", null ) {
console.echo( "Hello, world." );
}
}
That script will print a simple welcome message to the player when they join and will add a "hello" command to their in-game console.
Next up will be adding a custom tool to a player... but this one was the foundation for that one.