Modding Help Is there a way to call Lua function on widget state change?

Discussion in 'Starbound Modding' started by MelOzone, Apr 2, 2017.

  1. MelOzone

    MelOzone Pangalactic Porcupine

    Example:
    I need to call a Lua function when an item is dropped into the object's ItemgridWidget. Is there a way to do that?
     
  2. Cyel

    Cyel Scruffy Nerf-Herder

    Yes, widget do have callbacks upon certain actions. I'm not sure about the ones for itemGrid, but you can try
    Code:
      "itemgrid" : {
      "type" : "itemgrid",
      "position" : [0, 0],
      "callback" : "callback"
      }
    [...]
    
      "scriptWidgetCallbacks" : [ "callback" ]
    
    in your interface.config, then define a callback() function in your interface's Lua script
     
  3. bk3k

    bk3k Oxygen Tank

    I think what you really want is containerCallback() if your object is a container.

    For reference, that's added to the object's scripts rather than the widgets. I don't know if you're doing a scripted interface or not and if the widget's own scripts need to know this. I don't THINK that works on the widget side but could be wrong. Maybe sent a message. You'd need the widget's id but I "think" entity.id() is valid on widgets too? But I'm not 100% what functions are available.

    As long as you can get the id, the widget and object can message each other. Short of that, there are functions for the script to at least get the calling object's id. It can send a message periodically to check for different things.
     
  4. Storm_UK

    Storm_UK Existential Complex

    The itemgrids have default engine callbacks which you'll break if you try to add your own making the slots non-interactive, so currently you have to check what item is in the corresponding slot each tick and whether it has changed to determine whether something has been placed in it, using widget.itemGridItems("itemGridName") to fetch a table of those items. I do such in my macrochip workstation object (which only has a single itemgrid slot) - feel free to look at the script for that.

    However I have it on good authority that with the 1.3 release of Starbound there will be some additional lua available for item slot manipulation which will make such script a lot easier to implement.
     
    MelOzone, bk3k and Cyel like this.
  5. bk3k

    bk3k Oxygen Tank

    That's useful information. I wondered about that. So the hard-coded stuff goes away when objects are scripted.

    I tried(failed) to script the player inventory. It seemed to simply ignore my script entirely. Unless I used the wrong parameter. I tried "scripts" : [ ] instead of "script" : " " etc. I should try that... At risk of breaking things but that's always the case.

    Edit: wait...
    I need to try adding callbacks to the player environment itself. All these ideas I didn't think to try before...
     
  6. MelOzone

    MelOzone Pangalactic Porcupine

    I finally went with this option myself, but thanks for the hint anyway.

    What I required was doing something ONCE when a new item is dropped into the slot. In the end I decided to "bruteforce" it:
    1) Set "called" variable to false
    2) Check every tick if there is an item
    3) If there is no item and "called" is true, set it to false
    4) If there is an item and the function is not "called", call function A & set "called" to true

    This way every time an item is dropped into the slot, there is a one-time call of function A.
     

Share This Page