Mythruna
April 25, 2024, 10:53:04 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: Manually Sticking a Blueprint on a Player  (Read 9384 times)
pspeed
Administrator
Hero Member
*****
Posts: 5612



View Profile
« on: January 09, 2012, 07:39:55 PM »

So I threw this together this evening.  If you know a specific blueprint that you want to add to a player then you can add this script to the mods/scripts or scripts directory and it will add it to any players that don't already have it.

I'm not making this an official example because the API will be improved to make this simpler in the future... and may slightly break this one.  But it works now.

It is still helpful to have read and tried the other examples:
http://mythruna.com/forum/index.php?topic=505.0
http://mythruna.com/forum/index.php?topic=506.0

If nothing else, the second example will let you easily be able to figure out which blueprint and object is and then you can manually copy it out of your mythruna.db/blueprints directory to give to someone else along with this script.

Code:

/**
 *  This script illustrates creating a blueprint
 *  entity on a player if it doesn't already exist.
 *  This is not a great learning example because much
 *  of this will be done differently soon but I wanted
 *  to show players how to manually add a blueprint.
 */


import mythruna.es.*;
import mythruna.script.*;


on( [playerJoined] ) {
    type, event ->

    // First we need to get the blueprints that
    // the player may already have
    // This requires a lot of manualy entity system interaction
    // that will be streamlined later.
    def items = entities.getEntities( new FieldFilter( InContainer.class, "parentId", player ),
                                      InContainer.class, BlueprintReference.class );
    
    // This is the blueprint we want to make sure that the player
    // has in their inventory
    long blueprintId = 1323480647086L;
 
    // Go through all of the ones they have to see if they
    // already have this one or not.
    boolean hasBlueprint = false;
    for( Entity e : items ) {
        if( blueprintId == e.get(BlueprintReference.class).blueprintId ) {
            hasBlueprint = true;
            break;
        }
    }

    // MUST ALWAYS RELEASE... bad things will happen otherwise
    // and this is why this API will be changed for scripts so
    // that they don't have to release.  
    items.release();    
    
    if( hasBlueprint )
        return;  // nothing else to do
    
    // Create the new entity and set it up
    def bp = entities.createEntity();
    bp << new BlueprintReference(blueprintId);
    bp << new InContainer(player, (byte)0);  

    console.echo( "Added blueprint:" + blueprintId );
}

For blueprints, the name of the file is also the ID.  So if you need to rename the file then just make sure it's still a number and then use that number in the script above.

It's not very elegant but it works... and like I said in another thread, there will be official support for this someday anyway.
Logged
chessikr
Newbie
*
Posts: 7


View Profile
« Reply #1 on: August 10, 2012, 08:06:06 PM »

so if i were to put

 boolean hasBlueprint = false;
    for( Entity e : items ) {
        if( blueprintId == e.get(BlueprintReference.class).blueprintId ) {
            hasBlueprint = true;
            break;
        }

nested in a for() loop that iterates per blueprint in a folder (for simplicity sake i would probably create an array of blueprint names and cycle through that), i could theoretically ensure that any imported blueprints are added to my (or whoever joins) inventory on load?

or, would the implementation be different for single player (as i assume this is geared towards server-side)?

also, this
import mythruna.es.*;
import mythruna.script.*;

loads the script library (?) and the access route to the blueprints file?


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



View Profile
« Reply #2 on: August 14, 2012, 08:04:39 AM »

so if i were to put

 boolean hasBlueprint = false;
    for( Entity e : items ) {
        if( blueprintId == e.get(BlueprintReference.class).blueprintId ) {
            hasBlueprint = true;
            break;
        }

nested in a for() loop that iterates per blueprint in a folder (for simplicity sake i would probably create an array of blueprint names and cycle through that), i could theoretically ensure that any imported blueprints are added to my (or whoever joins) inventory on load?

or, would the implementation be different for single player (as i assume this is geared towards server-side)?

In this case, if you write it properly for the server then it can work in both places.  It's possible to cheat on the single player version so that it doesn't work in multiplayer but in general as long as you reference the player properly it should work in both... you just have to put the script in different places.

also, this
import mythruna.es.*;
import mythruna.script.*;

loads the script library (?) and the access route to the blueprints file?


The imports make it so that you can reference classes by their "simple name" instead of the fully qualified name.

For example:
Code:
import mythruna.es.Name
...
...
prinln someEntity[Name.class]

Is the same as:
Code:
....
....
println someEntity[mythruna.es.Name.class]

The first one is just easier to read.
Logged
chessikr
Newbie
*
Posts: 7


View Profile
« Reply #3 on: August 16, 2012, 07:03:32 PM »

i see, so the imports are kind of like namespaces.
Logged
pspeed
Administrator
Hero Member
*****
Posts: 5612



View Profile
« Reply #4 on: August 16, 2012, 07:39:27 PM »

i see, so the imports are kind of like namespaces.

Yeah, they're just for programmer convenience.  They are just for the compiler to fully qualify the classes.
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!