Modding Help [Help needed] Setting text in a label of a machine gui

Discussion in 'Starbound Modding' started by SilvanaDamikola, Jun 7, 2024.

  1. SilvanaDamikola

    SilvanaDamikola Intergalactic Tourist

    Hi gurus,
    I'm developing a small machine and inspired by Wellbott and their Universal Uncrafter I also used a simple approach with recycling the functionality of a container. The basic functions work all fine, I would only like to change the text of some labels dynamically from my lua script.
    I'm trying to figure this out for some time now and am going slowly crazy.
    What I mean is this:
    I have a replicator.object file where I define the location of the ui configuration and the backing script. Like this:
    Code:
    (....)
      "state" : {
        "kind" : "storage",
        "openSounds" : [ "/sfx/objects/apexcooldoor_open.wav" ],
        "closeSounds" : [ "/sfx/objects/apexcooldoor_close.wav" ],
        "slotCount" : 3,
        "uiconfig" : "/objects/crafting/replicator/replicator.config",
        "containerCallback": "containerCallback"
      },
    (....)
      "scripts" : [ "replicator.lua"],
    (....)
    
    
    Then in the replicator.config I have some label definitions like this:
    Code:
    {
      "gui" : {
    (....)
        "text1Label" : {
          "type" : "label",
          "position" : [8, 32],
          "hAnchor" : "left",
          "color" : [100, 140, 225],
          "value" : "Object Cloning"
        },
    (....)
    Now, inside my update callback I would like to change the text of this label to some additional text like the price depending on some context I calculate.

    I have no idea how to do this :(

    I found the function
    Code:
    widget.setText("textLabel", "HUBBAB")
    but I have no idea how to get to the the appropriate widget object so I get this error:
    Code:
    [Error] Exception while invoking lua function 'update'. (LuaException) Error code 2, [string "/objects/crafting/replicator/replicator.lua"]:43: attempt to index a nil value (global 'widget')
    
    Could anyone help?

    Cheers
    Silvana.
     
  2. Rezetrx

    Rezetrx Void-Bound Voyager

    "text1Label" would be the name of the label and the first parameter of the setText function.

    Code:
    widget.setText("text1Label", "HUBBAB")
    
    Here an example I used:
    Code:
    (...)
        "btnLootAll" : {
          "type" : "button",
          "position" : [79, 35],
          "base" : "/interface/button.png",
          "hover" : "/interface/buttonhover.png",
          "disabledImage" : "/interface/buttongrayed.png",
          "caption" : "Loot all",
          "callback" : "doLootall"
        },
    (...)
    
    Code:
    function update(dt)
        if self.canSteal then
            widget.setText("btnLootAll", "Steal")
        else
            widget.setText("btnLootAll", "Tsss")
        end
    end
    
    
     
  3. SilvanaDamikola

    SilvanaDamikola Intergalactic Tourist

    Thanks a lot for your assistance.
    But, well, that's more or less what I figured out. It just doesn't work in my case. I guess I'm trying this on a wrong kind of object or something.
    In your example, in your call to widget.setText, the global object widget is apparently set so the call works.
    As I have written in my original post, in my case the object widget is nil, so I get the posted error. (I did post a mismatched code example, but this is just a typo here, thie problem is the widget object.)
    Can you tell me on what type of object you call this function? Or do you do some special includes via "require"?
     
  4. Rezetrx

    Rezetrx Void-Bound Voyager

    I call it inside a interface script. If your logic is within the object script, you might need to do some messaging stuff, similar to how the mannquin object does it.
     
  5. SilvanaDamikola

    SilvanaDamikola Intergalactic Tourist

    Ah, right. I called it inside an object script so that's the reason why the widget object was nil.
    I have analyzed your suggestion and the mannequin object. I have verified that I can set the content of the label inside the interface script.

    My problem now is:
    1. The callbacks of the interface seem only to work on buttons
    2. The interface script knows about the widget object but I need access to the entity when calculating the price and the interface script knows nothing about it (enity == nil)
    3. The object script knows about the entity object but knowns nothing about the widget object (widget == nil)

    So my questions now are:
    Q1: Is it possible to trigger some callbacks on the interface elements other than buttons, preferably automatically without user interaction?
    Q2: Is it possible to somehow get to the widget object of the underlying interface inside an object script?
    Q3: Vice versa; Is it possible to get to the entity object of the associating object script inside the interface script?

    I still didn't try the approach with messaging since this is pretty advanced and in the case of the mannequin interface, the message is sent inside a callback triggered by one of its buttons.
     
  6. SilvanaDamikola

    SilvanaDamikola Intergalactic Tourist

    All right, I have figured out how to access the items in my object from the interface script so I can now calculate the price and display it correctly.
    Code:
    local items = widget.itemGridItems("itemGrid")
    local cloneItem = items[1]
    if cloneItem ~= nil then
      local neededFuel = fuelFormula(cloneItem)
      widget.setText("lblPrice", "Fuel needed: " .. neededFuel)
    else
      widget.setText("lblPrice", "Put something in the INPUT slot")
    end
    
    The only problem remains how to trigger the display without an extra button.
    So from the last post only the first question (Q1) remains.
     

Share This Page