Modding Help [tech] lua script update problem.

Discussion in 'Starbound Modding' started by C0bra5, Dec 22, 2014.

  1. C0bra5

    C0bra5 Oxygen Tank

    i've been trying to fix the script of one of my tech and it looks like i can't find what's stopping it from doing any thing....

    it is supposed to allow the user to take a monster in the air by clicking on it and move it to a different place.
    some images since images seems to explain things better than i do:http://imgur.com/UfKZ7Hd

    i've done my best to fix it but so far the only thing it does is consume energy and it find the position of the entity i'm pointing at.
    original code:
    Code:
    function init()
        data.active = false
        data.grabbed = false
        data.timer = 0
    end
    
    function uninit()
      if data.active then
        tech.setParentFacingDirection(nil)
      end
    end
    
    function input(args)
    
        if args.moves["special"] == 1 then
            if not data.active then
                return "active"
            else
                return "inactive"
            end
        end
       
        if args.moves["primaryFire"] then
            return "grab"
        end
       
        if args.moves["down"] and tech.onGround() then
            return "crouch"
        end
       
        return nil
    end
    
    function update(args)
      local energyUsagePerSecond = tech.parameter("energyUsagePerSecond")
      local energyUsage = energyUsagePerSecond * args.dt
      local usedEnergy = 0
    
        if not data.active and args.actions["active"] then
            tech.setToolUsageSuppressed(true)
            data.active = true
        elseif data.active and args.actions["inactive"] then
            tech.setToolUsageSuppressed(false)
            data.active = false
        end
     
      if data.active then
        local diff = world.distance(args.aimPosition, tech.position())
        local aimAngle = math.atan2(diff[2], diff[1])
        local flip = aimAngle > math.pi / 2 or aimAngle < -math.pi / 2
        tech.setAnimationState("glowing", "low")
        if flip then
          tech.setFlipped(true)
          tech.setParentFacingDirection(-1)
        else
          tech.setFlipped(false)
          tech.setParentFacingDirection(1)
        end
        if args.actions["crouch"] then
            tech.setAnimationState("glowing", "crouch")
        end
      else
        tech.setAnimationState("glowing", "off")
        tech.setParentFacingDirection(nil)
      end
       
        if data.active and args.actions["grab"] and args.availableEnergy > energyUsage and data.timer <= 0 then
            local IDs = world.monsterQuery(args.aimPosition,5)
            table.insert(IDs, world.npcQuery(args.aimPosition,5)[1])
            if IDs[1] ~= nil then
                local position = {}
                local velocity = {}
                position = world.entityPosition(IDs[1])
                --world.logInfo("%d, %d", position[1], position[2])
                velocity[1] = (args.aimPosition[1] - position[1]) * 10
                velocity[2] = (args.aimPosition[2] - position[2]) * 10
                world.callScriptedEntity(IDs[1], "entity.setVelocity", velocity)
                world.callScriptedEntity(IDs[1], "entity.setGravityEnabled", false)
                --world.monsterQuery(args.aimPosition,3, { callScript = "entity.setVelocity", callScriptArgs = { velocity } })
                data.grabbed = true
            else
                world.monsterQuery(args.aimPosition,30, { callScript = "entity.setGravityEnabled", callScriptArgs = { true } })
                data.grabbed = false
            end
            tech.setAnimationState("glowing", "low")
            return energyUsage
        elseif data.grabbed and (not args.actions["grab"] or args.availableEnergy < energyUsage) then
            data.timer = 1
            world.monsterQuery(args.aimPosition,30, { callScript = "entity.setGravityEnabled", callScriptArgs = { true } })
            --world.npcQuery(args.aimPosition,30, { callScript = "entity.setGravityEnabled", callScriptArgs = { true } })
            tech.setAnimationState("glowing", "low")
            data.grabbed = false
        end
       
        data.timer = data.timer - args.dt
    
       
    
        return 0
    end
    
    and this is what i've achived to do so far:
    Code:
    function init()
        self.active = false
        self.grabbed = false
        self.timer = 0
    end
    
    function uninit()
      if self.active then
        mcontroller.controlFace(nil)
      end
    end
    
    function input(args)
    
        if args.moves["special"] == 1 then
            if not self.active then
                return "active"
            else
                return "inactive"
            end
        end
       
        if args.moves["primaryFire"] then
            return "grab"
        end
       
        if args.moves["down"] and mcontroller.onGround() then
            return "crouch"
        end
       
        return nil
    end
    
    function update(args)
      local energyUsagePerSecond = tech.parameter("energyUsagePerSecond")
      local energyUsage = energyUsagePerSecond * args.dt
      local usedEnergy = 0
    
        if not self.active and args.actions["active"] then
            tech.setToolUsageSuppressed(true)
            self.active = true
        elseif self.active and args.actions["inactive"] then
            tech.setToolUsageSuppressed(false)
            self.active = false
        end
     
      if self.active then
        local diff = world.distance(tech.aimPosition(), mcontroller.position())
        local aimAngle = math.atan2(diff[2], diff[1])
        local flip = aimAngle > math.pi / 2 or aimAngle < -math.pi / 2
        tech.setAnimationState("glowing", "low")
        if flip then
          tech.setFlipped(true)
          mcontroller.controlFace(-1)
        else
          tech.setFlipped(false)
          mcontroller.controlFace(1)
        end
        if args.actions["crouch"] then
            tech.setAnimationState("glowing", "crouch")
        end
      else
        tech.setAnimationState("glowing", "off")
        mcontroller.controlFace(nil)
      end
       
        if self.active and args.actions["grab"] and tech.consumeTechEnergy(energyUsage) and self.timer <= 0 then
            local IDs = world.monsterQuery(tech.aimPosition(),5)
            table.insert(IDs, world.npcQuery(tech.aimPosition(),5)[1])
            if IDs[1] ~= nil then
                local position = {}
                local velocity = {}
                position = world.entityPosition(IDs[1])
                --world.print("%d, %d", position[1], position[2])
                velocity[1] = (tech.aimPosition()[1] - position[1]) * 10
                velocity[2] = (tech.aimPosition()[2] - position[2]) * 10
                world.callScriptedEntity(IDs[1], "entity.setVelocity", velocity)
                world.callScriptedEntity(IDs[1], "entity.setGravityEnabled", false)
                --world.monsterQuery(tech.aimPosition(),3, { callScript = "entity.setVelocity", callScriptArgs = { velocity } })
                self.grabbed = true
            else
                world.monsterQuery(tech.aimPosition(),30, { callScript = "entity.setGravityEnabled", callScriptArgs = { true } })
                self.grabbed = false
            end
            tech.setAnimationState("glowing", "low")
            return energyUsage
        elseif self.grabbed and (not args.actions["grab"] or tech.consumeTechEnergy(energyUsage)) then
            self.timer = 1
            world.monsterQuery(tech.aimPosition(),30, { callScript = "entity.setGravityEnabled", callScriptArgs = { true } })
            --world.npcQuery(tech.aimPosition(),30, { callScript = "entity.setGravityEnabled", callScriptArgs = { true } })
            tech.setAnimationState("glowing", "low")
            self.grabbed = false
        end
       
        self.timer = self.timer - args.dt
    
       
    
        return 0
    end
    
     
  2. C0bra5

    C0bra5 Oxygen Tank

    Best Answer
    I finally found the patches.... here is the final code in case someone want's it... for some reason or an other.

    I've put in red the code that was changed form the original

    -----start of script-----

    function init()
    self.active = false
    self.grabbed = false
    self.timer = 0​
    end

    function uninit()
    if self.active then
    mcontroller.controlFace(nil)​
    end​
    end

    function input(args)

    if args.moves["special"] == 1 then
    if not self.active then
    return "active"​
    else
    return "inactive"​
    end​
    end

    if args.moves["primaryFire"] then
    return "grab"​
    end​

    if args.moves["down"] and mcontroller.onGround() then
    return "crouch"​
    end​

    return nil​
    end

    function update(args)
    local energyUsagePerSecond = tech.parameter("energyUsagePerSecond")
    local energyUsage = energyUsagePerSecond * args.dt
    local usedEnergy = 0​

    if not self.active and args.actions["active"] then
    tech.setToolUsageSuppressed(true)
    self.active = true​
    elseif self.active and args.actions["inactive"] then
    tech.setToolUsageSuppressed(false)
    self.active = false​
    end​

    if self.active then
    local diff = world.distance(tech.aimPosition(), mcontroller.position())
    local aimAngle = math.atan2(diff[2], diff[1])
    local flip = aimAngle > math.pi / 2 or aimAngle < -math.pi / 2
    tech.setAnimationState("glowing", "low")

    if flip then
    tech.setFlipped(true)
    mcontroller.controlFace(-1)​
    else
    tech.setFlipped(false)
    mcontroller.controlFace(1)​
    end

    if args.actions["crouch"] then
    tech.setAnimationState("glowing", "crouch")​
    end​
    else
    tech.setAnimationState("glowing", "off")
    mcontroller.controlFace(nil)​
    end​

    if self.active and args.actions["grab"] and tech.consumeTechEnergy(energyUsage) and self.timer <= 0 then

    local IDs = world.monsterQuery(tech.aimPosition(),5)
    table.insert(IDs, world.npcQuery(tech.aimPosition(),5)[1])

    if IDs[1] ~= nil then
    local position = {}
    local velocity = {}
    position = world.entityPosition(IDs[1])
    --world.print("%d, %d", position[1], position[2])
    velocity[1] = (tech.aimPosition()[1] - position[1]) * 10
    velocity[2] = (tech.aimPosition()[2] - position[2]) * 10
    world.callScriptedEntity(IDs[1], "mcontroller.setVelocity", velocity)
    world.callScriptedEntity(IDs[1], "mcontroller.setGravityEnabled", false)
    --world.monsterQuery(tech.aimPosition(),3, { callScript = "mcontroller.setVelocity", callScriptArgs = { velocity } })
    self.grabbed = true​
    else
    world.monsterQuery(tech.aimPosition(),30, { callScript = "mcontroller.setGravityEnabled", callScriptArgs = { true } })
    self.grabbed = false​
    end

    tech.setAnimationState("glowing", "low")

    return energyUsage​
    elseif self.grabbed and (not args.actions["grab"] or tech.consumeTechEnergy(energyUsage)) then
    self.timer = 1
    world.monsterQuery(tech.aimPosition(),30, { callScript = "mcontroller.setGravityEnabled", callScriptArgs = { true } })
    --world.npcQuery(tech.aimPosition(),30, { callScript = "mcontroller.setGravityEnabled", callScriptArgs = { true } })
    tech.setAnimationState("glowing", "low")
    self.grabbed = false​
    end​

    self.timer = self.timer - args.dt​

    return 0​
    end

    -----end of script-----

    now i just need to fix the it toggling problem when i press f... it toggles and un-toggles continuously when i press to to toggle it[DOUBLEPOST=1419268754][/DOUBLEPOST]
    turns out that most of the entity.* has been moved to the mcontroller.*
    and btw doing world.logInfo("%s", mcontroller) crashes the tech script
    but yes the .entity did exits, it even had a page in the lua api wiki: http://starbounder.org/Modding:Lua/Tables/Entity#entity.setGravityEnabled
     
    Last edited: Dec 22, 2014
    severedskullz likes this.
  3. The | Suit

    The | Suit Agent S. Forum Moderator

  4. C0bra5

    C0bra5 Oxygen Tank

    it might do the same thing but since the last unstable update(the xs corporation mechs are still in enraged koala) most of the lua api has been revamped and most of the old script dosen't work anymore.
    i've been using this file that kawa put online to try to fix the script:
    http://helmet.kafuka.org/sbmods/diffs/20141217_to_stable.txt
     
  5. I dont know if the entity.* commands you are sending in even exist anymore... I'm pretty sure the whole change to the movement controller kinda invalidated that entire thing. Sadly thats not something I have messed with yet so I cant give you any pointers on exactly how to fix it. But that would be where to start looking.

    Do me a favor and print out the mcontroler table and we can see its contents of which functions are in there now. I dont have access to the game right now, or I would do it for you:

    Code:
    function init()
        self.active = false
        self.grabbed = false
        self.timer = 0
        world.logInfo("%s", mcontroller)
    end
    
    function uninit()
      if self.active then
        mcontroller.controlFace(nil)
      end
    end
     
  6. C0bra5

    C0bra5 Oxygen Tank

    Best Answer
    I finally found the patches.... here is the final code in case someone want's it... for some reason or an other.

    I've put in red the code that was changed form the original

    -----start of script-----

    function init()
    self.active = false
    self.grabbed = false
    self.timer = 0​
    end

    function uninit()
    if self.active then
    mcontroller.controlFace(nil)​
    end​
    end

    function input(args)

    if args.moves["special"] == 1 then
    if not self.active then
    return "active"​
    else
    return "inactive"​
    end​
    end

    if args.moves["primaryFire"] then
    return "grab"​
    end​

    if args.moves["down"] and mcontroller.onGround() then
    return "crouch"​
    end​

    return nil​
    end

    function update(args)
    local energyUsagePerSecond = tech.parameter("energyUsagePerSecond")
    local energyUsage = energyUsagePerSecond * args.dt
    local usedEnergy = 0​

    if not self.active and args.actions["active"] then
    tech.setToolUsageSuppressed(true)
    self.active = true​
    elseif self.active and args.actions["inactive"] then
    tech.setToolUsageSuppressed(false)
    self.active = false​
    end​

    if self.active then
    local diff = world.distance(tech.aimPosition(), mcontroller.position())
    local aimAngle = math.atan2(diff[2], diff[1])
    local flip = aimAngle > math.pi / 2 or aimAngle < -math.pi / 2
    tech.setAnimationState("glowing", "low")

    if flip then
    tech.setFlipped(true)
    mcontroller.controlFace(-1)​
    else
    tech.setFlipped(false)
    mcontroller.controlFace(1)​
    end

    if args.actions["crouch"] then
    tech.setAnimationState("glowing", "crouch")​
    end​
    else
    tech.setAnimationState("glowing", "off")
    mcontroller.controlFace(nil)​
    end​

    if self.active and args.actions["grab"] and tech.consumeTechEnergy(energyUsage) and self.timer <= 0 then

    local IDs = world.monsterQuery(tech.aimPosition(),5)
    table.insert(IDs, world.npcQuery(tech.aimPosition(),5)[1])

    if IDs[1] ~= nil then
    local position = {}
    local velocity = {}
    position = world.entityPosition(IDs[1])
    --world.print("%d, %d", position[1], position[2])
    velocity[1] = (tech.aimPosition()[1] - position[1]) * 10
    velocity[2] = (tech.aimPosition()[2] - position[2]) * 10
    world.callScriptedEntity(IDs[1], "mcontroller.setVelocity", velocity)
    world.callScriptedEntity(IDs[1], "mcontroller.setGravityEnabled", false)
    --world.monsterQuery(tech.aimPosition(),3, { callScript = "mcontroller.setVelocity", callScriptArgs = { velocity } })
    self.grabbed = true​
    else
    world.monsterQuery(tech.aimPosition(),30, { callScript = "mcontroller.setGravityEnabled", callScriptArgs = { true } })
    self.grabbed = false​
    end

    tech.setAnimationState("glowing", "low")

    return energyUsage​
    elseif self.grabbed and (not args.actions["grab"] or tech.consumeTechEnergy(energyUsage)) then
    self.timer = 1
    world.monsterQuery(tech.aimPosition(),30, { callScript = "mcontroller.setGravityEnabled", callScriptArgs = { true } })
    --world.npcQuery(tech.aimPosition(),30, { callScript = "mcontroller.setGravityEnabled", callScriptArgs = { true } })
    tech.setAnimationState("glowing", "low")
    self.grabbed = false​
    end​

    self.timer = self.timer - args.dt​

    return 0​
    end

    -----end of script-----

    now i just need to fix the it toggling problem when i press f... it toggles and un-toggles continuously when i press to to toggle it[DOUBLEPOST=1419268754][/DOUBLEPOST]
    turns out that most of the entity.* has been moved to the mcontroller.*
    and btw doing world.logInfo("%s", mcontroller) crashes the tech script
    but yes the .entity did exits, it even had a page in the lua api wiki: http://starbounder.org/Modding:Lua/Tables/Entity#entity.setGravityEnabled
     
    Last edited: Dec 22, 2014
    severedskullz likes this.
  7. Thats what I figured. Also you can fix the toggling continuously issue by saving the previous state of the keypress in the tech and check for it later.
    Code:
    function update(args)
        self.lastActive = self.activePress
        self.activePress = args.actions["active"]
      
        -- Toggle
        if self.activePress and not self.lastActive then
            -- If we enter this IF, then we toggled the button. Not holding it down.
            -- DO STUFF
        end
    end
    EDIT: Actually that would probably be better to wrap that around your Input() function for returning "active" and "inactive"
     
  8. C0bra5

    C0bra5 Oxygen Tank

    even by putting it around the if didn't help but i found how.
    Code:
    function init()
        self.active = false
        self.grabbed = false
        self.timer = 0
        self.pressed = false
    end
    
    function uninit()
      if self.active then
        mcontroller.controlFace(nil)
      end
    end
    
    function input(args)
            if args.moves["special"] == 1 then
                if not self.pressed then
                    self.pressed = true
                    if not self.active then
                        return "active"
                    else
                        return "inactive"
                    end
                end
            else
                self.pressed = false
            end
          
      
        if args.moves["primaryFire"] then
            return "grab"
        end
      
        if args.moves["down"] and mcontroller.onGround() then
            return "crouch"
        end
      
        return nil
    end
     
  9. I didnt mean quite literally... It was an example, not a copy paste solution. Either way, happy you got it working.
     
  10. C0bra5

    C0bra5 Oxygen Tank

    i wasn't taking that literally the code would never had worked. but still, thank's for the help!
     

Share This Page