Giter Club home page Giter Club logo

starbound-messagehandling-demo's Introduction

Message Handling Demo

Entity messages allow us to share data between scripts and even between different clients. This project contains some simple examples of sending and handling entity messages.

If you already know how to work with entity messages, there's probably not much more in this project for you. The Wiki may still contain some useful information, though.

Table of Contents

Deployment Script

Deployment scripts are automatically loaded alongside the player.

Message Handlers

Our deployment script simply handles the message mhDeployment_loaded by responding with true, but only locally.

This message will always respond true when sent to yourself, but will never respond with true when sent to or received by other entities.

Note: Due to the way message handlers and Lua in general work, you can never "not" return a value from your handler. Even local handlers will return 'nil', and the promise will still succeed.
In this use case, it would mean users could check if your deployment script is loaded simply by checking if the promise succeeded or not (regardless of the result value).

Back to top

Object

The mhComputer object can multiply two numbers. It is truly a miracle this tiny machine is capable of such complex requests /s.

You can obtain the object with /spawnitem mhComputer.

The object script sets two message handlers, which will be covered individually.

Multiplying

The message handler mhComputer_multiply can be used to multiply two numbers.

We want everyone to be able to use this computer, so a simpleHandler is used instead of a localHandler.

Because anyone could send any data, verification of parameters is important. If either value is not a number, we return an error message.

Note: As a message is a valid response, this "error" message is simply a string. The message will still succeed and yield a result! Never call error() in your message handler, it will simply break your own script!

If both numbers are valid, the answer is returned as a Lua object. This makes both the actual answer and a formatted message easily accessible. The object will also say this message in a text bubble.

Secret

The computer knows of a secret code, only accessible to local entities. Good luck trying to access it, though!

When sending the message mhComputer_secret from our tech, deployment or active item script, the response will always be nil.

We might have placed the object, but the object is not controlled/managed by the player. Even in single player, it will not work from a player script. Objects will be able to communicate with each other though, since they're all controlled by the server.

Back to top

Tech

A script has been added to the dash tech, which we'll use to send messages to objects and other player scripts.

To use this script, you must equip the dash tech.

Message Handlers

Just like the deployment script, the tech script has a local message handler (mhTech_loaded) that'll simply respond with true.

Although this feature may seem useless due to the presence of player.equippedTech, it can still be used to determine if this specific module is loaded.

Sending Messages

When pressing f, the script will send a couple of messages to the player, and a couple of messages to the objects near your cursor.

  • mhDeployment_loaded
    Sent to yourself, and will respond true.
  • mhItem_held
    Sent to yourself, and will respond with true if the demo item is held.
  • mhComputer_multiply
    Sent to nearby objects. Expected: "Cannot multiply nil with nil.".
  • mhComputer_multiply ("apple", "banana")
    Sent to nearby objects. Expected: "Cannot multiply apple with banana.".
  • mhComputer_multiply (5, 3)
    Sent to nearby objects. Expected: { answer = 15, message = "5 * 3 = 15"}.
  • mhComputer_secret
    Sent to nearby objects. Expected: nil.

Since the mhComputer messages are sent to all objects near your cursor, you may see some failed messages as well ("Message not handled by entity"). If no objects were found near your cursor, nothing will happen as no messages were sent.

Promise Keeper

All message responses are managed by the promise keeper created in mhTech.init. We update the promise keeper in mhTech.update.

In mhTech.sendMessage, the message is sent to the entity and the response is passed to the promise keeper. For our onSuccess and onError callback we simply log the results.

Back to top

Active Item

The active item mhItem will reward you, but only if you have the best tech equipped: dash.

You can obtain the item with /spawnitem mhItem.

Message Handlers

This script comes with a local message handler (mhItem_held) which can be used to check if this item is being held. The feature itself is basically useless due to the presence of world.entityHandItem, but let us ignore that.

Sending Messages

On firing, the item will send the message mhTech_loaded, which will check if the tech module is loaded.

Promise Keeper

Just like the tech module, promises are managed by a promise keeper.

If the module is loaded (onSuccess), the item will be consumed and the player will receive 100 coins and the confetti animation will play.

If the module is not loaded (onError), the item will make an error noise.

Back to top

starbound-messagehandling-demo's People

Contributors

silverfeelin avatar

Stargazers

Gen avatar DracoMan671 avatar Flavia Muramoto avatar Eti // Xan avatar  avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.