Mythruna

Modder's Workbench => Scripting => Topic started by: pspeed on January 09, 2012, 11:28:04 AM



Title: Ex #2: Basic "Build-mode" Custom Tool Example
Post by: pspeed on January 09, 2012, 11:28:04 AM
Continuing the previous post:
http://mythruna.com/forum/index.php?topic=505.0

In this script, we hook into the playerJoined event to add a new set of build-mode tools to the player.

In the final game, players will be in "game mode" and objects and tools won't be handled this way.  In build mode, the tools available are sort of a raw grouped list of actions they can perform.  In "game mode", actions will mostly be based on using items and not executing scripts directly like this.  Mostly.

Anyway, here is an example that creates a couple of simple new tools that give info about the world and its objects.  This is also the first introduction to Mythruna's entity system and how the scripts can access it.

Code:
/**
 *  In the previous "player-hook-example" script, we learned
 *  how to hook scripts to the playerJoined event.  In this script,
 *  we'll be extending that to add a new build-mode tool to the player.
 *
 *  To make it more interesting, I will be adding two tools to a
 *  tool group.  So when the general tool category is selected then
 *  ctrl + mouse wheel will switch between them.
 */

// This script will access entity data and so need to import
// that packages that contains those components
import mythruna.es.*;
import mythruna.script.*;
 

/**
 *  This is a simple action that dumps some information about a
 *  clicked on block.
 */
action( group:"Example Tools", name:"Block Info", type:ActionType.Block ) {

    def loc = it.getBlock();    
    type = world.getType( loc.x, loc.y, loc.z, null );

    console.echo( "Block type:" + type + "  at:" + loc );
}

/**
 *  Dumps some information about an object that is clicked.
 */
action( group:"Example Tools", name:"Object Info", type:ActionType.Object ) {

    console.echo( "Object:" + it.object + " at:" + it.location );
    
    // The 'object' in this case is an entity.
    //
    // In Mythruna, all objects are entities and are part of the 'entity
    // system'.  This includes players, blueprints, placeable objects, etc..
    // You can find many confusing and enlightening articles on 'Entity Systems'
    // online.  They are a flexible and scalable way of supporting these
    // kinds of games.  The deeper topic is too much for this example but
    // the Mythruna-script API provides convenient access to entity data.
    //
    // An entity can have various types of data components associated
    // with it.  Here we will access some data components of the clicked
    // placeable object.
    //
    // The raw entity system is available as the 'entities' reference.
    // So you can manually use:
    // entities.getComponent( someEntity, SomeComponent.class )
    // or you can more simlpy use:
    // someEntity[SomeComponent.class]
    //
    // Likewise, you can set data by directly calling:
    // entities.setComponent( someEntity, new Name("My Name") )
    // or you can use the shortcut:
    // someEntity << new Name("My Name")
    //
    // Some data components like name are common enough that a more
    // convenient access was provided:
    // someEntity.name
    // ...will return someEntity[Name.class].name if the name component
    // is not null or null if it is.
    //
    // Let's dump some information about the clicked object now:
    
    def entity = it.object;
    console.echo( "Position:" + entity[Position.class] );
    console.echo( "Name:" + entity[Name.class] );
    console.echo( "Blueprint info:" + entity[ModelInfo.class] );
    console.echo( "Created by:" + entity[CreatedBy.class] );
    
    // We can go one further and get the name of the creator if it
    // has one
    if( entity[CreatedBy.class] != null )
        console.echo( "   " + entity[CreatedBy.class].creatorId.name );
}
 
 
  
on( [playerJoined] ) {
    type, event ->
    
    // When a player entity is created, certain tool actions are
    // added to them.  The tool group controls which mouse-wheel tool
    // they fall under and the name controls the ctrl + mouse-wheel
    // tool.  Right now, user defined tools only work with left-click.
    
    def refs = []

    // Get the existing tools that might be on the player since we
    // don't want to blow them away.
    ToolActions existing = player[ToolActions.class];
 
    // Note: 'player' in this case is an entity just like any other
    //       and may have similar data components to what was described
    //       above in the object info action.
 
    refs += actions.getRef( "Example Tools", "Block Info" );
    refs += actions.getRef( "Example Tools", "Object Info" );
    
    player << new ToolActions(refs, existing)    
}


Title: Re: Ex #2: Basic "Build-mode" Custom Tool Example
Post by: Toboi on January 09, 2012, 07:59:01 PM
Hey, that`s great!
Thanks for writing this tutorials!


Title: Re: Ex #2: Basic "Build-mode" Custom Tool Example
Post by: Michael on July 10, 2013, 11:01:07 AM
About the imports:
Code:
mythruna.es.*
mythruna.script.*

I put the Mythruna-core.jar into my IDE (Eclipse) and I put the  exact same code, I pressed CTRL + SHIFT + O and it imported:
Code:
mythruna.script.ActionType
mythruna.script.ToolActions

and it worked fine. I have tested it only on a server, but could it cause complications with singleplayer?


Title: Re: Ex #2: Basic "Build-mode" Custom Tool Example
Post by: Sean on July 10, 2013, 12:16:49 PM
About the imports:
Code:
mythruna.es.*
mythruna.script.*

I put the Mythruna-core.jar into my IDE (Eclipse) and I put the  exact same code, I pressed CTRL + SHIFT + O and it imported:
Code:
mythruna.script.ActionType
mythruna.script.ToolActions

and it worked fine. I have tested it only on a server, but could it cause complications with singleplayer?
Only one way to find out.


Title: Re: Ex #2: Basic "Build-mode" Custom Tool Example
Post by: Michael on July 10, 2013, 02:12:48 PM
About the imports:
Code:
mythruna.es.*
mythruna.script.*

I put the Mythruna-core.jar into my IDE (Eclipse) and I put the  exact same code, I pressed CTRL + SHIFT + O and it imported:
Code:
mythruna.script.ActionType
mythruna.script.ToolActions

and it worked fine. I have tested it only on a server, but could it cause complications with singleplayer?
Only one way to find out.
Fine ;P I will do it myself.


Title: Re: Ex #2: Basic "Build-mode" Custom Tool Example
Post by: pspeed on July 10, 2013, 02:29:39 PM
About the imports:
Code:
mythruna.es.*
mythruna.script.*

I put the Mythruna-core.jar into my IDE (Eclipse) and I put the  exact same code, I pressed CTRL + SHIFT + O and it imported:
Code:
mythruna.script.ActionType
mythruna.script.ToolActions

and it worked fine. I have tested it only on a server, but could it cause complications with singleplayer?

One is a short cut to easily get all classes imported for a package.  The other is just importing specific ones one at a time.  Personal preference, really.


Title: Re: Ex #2: Basic "Build-mode" Custom Tool Example
Post by: Michael on July 10, 2013, 04:00:04 PM
Only reason why I asked because I only used classes from one of those packages so yeah :P