Modding Help commandProcessorScripts - Api change?

Discussion in 'Starbound Modding' started by TinoD, Jan 25, 2017.

  1. TinoD

    TinoD Scruffy Nerf-Herder

  2. Errors4l

    Errors4l Spaceman Spiff

    Since I'm too lazy busy to look up a proper post that must be around somewhere, I'm going to post this simple example that should help you set up your own command processor script.

    Instead of having functions that can be called as commands, there's one function "command" that is called for each custom command. The first parameter passed to this function is the command name, followed by the client id and command arguments.
    To keep things organized, I suggest putting all your custom commands in a table. If the called command is present in the table; call it. This can also be seen in the below example, even though it only offers one command.

    http://community.playstarbound.com/threads/request-rng-dice.129333/#post-3098001
     
  3. TinoD

    TinoD Scruffy Nerf-Herder

    Thanks a lot for your help! This worked!
     
  4. TinoD

    TinoD Scruffy Nerf-Herder

    Is there a way to access the global "world" table from the script?
    I thought "requiring" it but I can't seem to find the definition of the table anywhere within /scripts
     
  5. Errors4l

    Errors4l Spaceman Spiff

    I'm afraid that's not something the command processor is capable of. The command processor has nothing to do with worlds or what's in them.
     
  6. TinoD

    TinoD Scruffy Nerf-Herder

    I see.
    And is there a way to call one of the regular commands from within the script and get the results? Such as /list or /spawnitem or something in that nature.

    I would just like to add a /who command that would list the player names, since I can't seem to be able to do much else without the tables.
     
  7. Errors4l

    Errors4l Spaceman Spiff

    Pretty sure the command function is only called for non-vanilla commands. You can always try for yourself, though.
     
  8. TinoD

    TinoD Scruffy Nerf-Herder

    I figured it out. Wasn't able to implement exactly what I wanted but at least I got the /who command working so I am posting it here if anybody else wants to implement this. It was a lot simpler than I thought initially.

    Code:
    function commands.who(id, args)
        local who_result = string.format("There are currently %s people online:\n", universe.numberOfClients())
        for _, client_id in pairs(universe.clientIds()) do
            who_result = who_result .. universe.clientNick(client_id) .. "\n"
        end
        return who_result
    end
    
     
    Errors4l likes this.
  9. Errors4l

    Errors4l Spaceman Spiff

    When you start making more advanced commands, it is important to make sure the script will never throw errors regardless of user input. Best way to do this is to check each argument given by the user, before executing any code.
    Any errors cause the command processor to break until the server is reloaded using /serverreload, or restarted completely.
     
  10. TinoD

    TinoD Scruffy Nerf-Herder

    I am aware of lua script errors breaking commands. I broke it quite a few times :p
    Thanks for the heads up though
     
  11. Pilchenstein

    Pilchenstein Ketchup Robot

    This method is currently pretty iffy, since every command processor script seems to share a context, meaning that only the init() and command() functions from last mod to load will actually work.

    It's still viable if all mods use an identical command() function, no init() function and declare commands as
    Code:
    commands = commands or {}
    
    rather than a local but it all falls over if anyone doesn't play by those rules, so it's not ideal.
     
    Last edited: Mar 19, 2017
    Kislolord and Cyel like this.

Share This Page