Modding Help Method to get random String value

Discussion in 'Starbound Modding' started by odkupiciel375, Feb 26, 2017.

  1. odkupiciel375

    odkupiciel375 Starship Captain

    ok so since i had some time again ive dig a bit dipper and for some reason game resets value of storage.state and on wiki it says that it ll persists even when u ll shut down whole game...
    i thought that i ll try in update to check if my animator state is same as (it should be) storage.state (this is bool value) and i've found out that game resets both of those and i can understand that it ll reset animator but why storage.state too? (Yes im mad and that's why this message may be a bit chaotic :) )
     
  2. bk3k

    bk3k Oxygen Tank

    The thing about storage.state is it will persist(but reduced to a boolean if anything else was stored there), however you have to be careful about not re-initializing. Yet you need to initialize.

    Since this is a boolean value, this will solve the issue.
    Code:
    storage.state = storage.state or false
    if true, it will stay true and the right hand side of or won't be evaluated. if false or nil, the right hand side of or will be evaluated. Thus the value will maintain true and false, otherwise initializing to false. You can preserve other value types that way too, except that you don't want them initializing to false so change what's to the right of or into your desired default value.
     
  3. odkupiciel375

    odkupiciel375 Starship Captain

    Code:
    function init()
      if storage.state == nil then storage.state = config.getParameter("defaultLightState", true) end
    ...
    
    and every single time when im teleporting back to a planet i see that animator.animationState() is default (off) and that would be ok if my storage wouldn't reset it self too
    I even removed uninit function as i had there
    Code:
    storage.state = nil
    
    but that didn't helped at all

    Code:
    function update()
        object.say("storage.state = "..tostring(storage.state).." animator.animationState('fjukeboxState') = "..animator.animationState("fjukeboxState"))
    end
    
    this is for testing purpose only!!!
     
  4. bk3k

    bk3k Oxygen Tank

    The animator state definitely won't stick. So set it during init based off of storage.state
    This is code from Automatic Doors. I call this function in init but also when anything happens.

    Code:
    function updateAnimation(wasOpen)
      local aState = ""
    
      if (storage.state ~= wasOpen) then
        if storage.state then --door opening
          aState = storage.openingAnimation_stateName
          animator.playSound("open")
        else  --door closing/locking
          if storage.locked or storage.wireControlled then
            aState = storage.lockingAnimation_stateName
          else
            aState = "closing" --already vanilla supported transition state
          end
          animator.playSound("close")
        end
      else --door is not opening nor closing
        if storage.state then
          aState = "open"
        elseif storage.locked or storage.wireControlled then
          aState = storage.lockedAnimation_stateName
        else
          aState = "closed"
        end
      end
    
      animator.setAnimationState("doorState", aState)
    end
    Don't worry about things like storage.openingAnimation_stateName because those are set by JSON config or default to a string. For example "opening" is a state that transitions to "open" but your vanilla doors only have "open".

    This example is probably more complex than you need. That function receives the former state of storage.state, and looks at the current state too. If it was false(door closed) and now true(door open) then the door should go to an "opening" or "open" state. If it was closed(storage.state == false), but now is locked, no opening or closing but rather switch to "locked" directly.

    As for init and storage.state,
    Code:
    storage.state = storage.state or ((config.getParameter("defaultState", "closed") == "open") and not storage.locked)
    I guess that's slightly more complicated than storage.state = storage.state or false.
    Make sure no other function is altering storage.state that doesn't need to(because something changed). You probably want to tell the animator to stop playing in uninit, but don't change storage.state itself.

    I should point out something else. When you teleport around, upon returning init will be called again. So set a value to indicate that it has ran before to avoid running most of the code again. But do let it set the animator states again to make sure they are correct. That is one area where a separate animator handling function will come in handy.
     

Share This Page