Mythruna
March 28, 2024, 09:01:48 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Welcome to the new forums. See "Announcements" for a note for new users.
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Ex #1: Basic Event Hooks Example  (Read 13153 times)
pspeed
Administrator
Hero Member
*****
Posts: 5612



View Profile
« on: January 09, 2012, 10:13:10 AM »

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.

Code:


/**
 *  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.
« Last Edit: January 09, 2012, 11:30:53 AM by pspeed » Logged
Rayblon
Donators
Hero Member
***
Posts: 1861


Hmmm...


View Profile
« Reply #1 on: September 07, 2015, 02:10:07 AM »

Since I'm clogging the wiki with junk right now, should I add this to the list or leave it to you?
Logged

pspeed
Administrator
Hero Member
*****
Posts: 5612



View Profile
« Reply #2 on: September 07, 2015, 05:24:25 PM »

Since I'm clogging the wiki with junk right now, should I add this to the list or leave it to you?

I don't know.  I don't plan to do anything with the wiki any time soon.  I don't know if that's an answer, though.
Logged
Michael
Donators
Hero Member
***
Posts: 2166



View Profile
« Reply #3 on: September 07, 2015, 06:12:23 PM »

Since I'm clogging the wiki with junk right now, should I add this to the list or leave it to you?
Go ahead. Let the force be with you.
Logged
Rayblon
Donators
Hero Member
***
Posts: 1861


Hmmm...


View Profile
« Reply #4 on: September 07, 2015, 06:18:36 PM »

I'll just... put these in a scripting help page, I guess. With copy+paste. Because that's professional.
Logged

Rayblon
Donators
Hero Member
***
Posts: 1861


Hmmm...


View Profile
« Reply #5 on: September 07, 2015, 07:15:55 PM »

So, I can do it, but... The wiki won't recognize a line as code if it doesn't have a space at the beginning. I don't know if that affects anything or not.

EDIT: I get it now, it eats a space on every line. If I add a space  on each line, the code should be unaffected.
« Last Edit: September 07, 2015, 07:19:43 PM by Rayblon » Logged

Rayblon
Donators
Hero Member
***
Posts: 1861


Hmmm...


View Profile
« Reply #6 on: September 07, 2015, 07:37:54 PM »

It was a pretty clean transplant when I got it all formatted right. I even worked in a scrolling textbox for the code Cheesy
Logged

Michael
Donators
Hero Member
***
Posts: 2166



View Profile
« Reply #7 on: September 30, 2015, 07:39:33 PM »

By the way, I already had a Scripting and Modding category:
http://mythruna.com/mediawiki/index.php?title=Mythruna_Wiki:Community_portal
Logged
Rayblon
Donators
Hero Member
***
Posts: 1861


Hmmm...


View Profile
« Reply #8 on: September 30, 2015, 08:37:39 PM »


I was just moving information over so yeh. If you want, you could always move the stuff elsewhere and create a redirect page.
Logged

Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.20 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!