Modding Help .lua sword problem

Discussion in 'Starbound Modding' started by Magmatico, Jul 20, 2018.

Tags:
  1. Magmatico

    Magmatico Void-Bound Voyager

    Hello. Im currently making a .lua file for an energy sword i made. I want it to retract and go inactive once you run out of energy. However, there is a problem. If the sword deactivates (either by activeTime expiring or energy running out) it becomes unrotatable (and unflippable) and doesn't attack. Im still new in lua so it probably is a very stupid mistake :wut:

    require "/scripts/util.lua"
    require "/scripts/vec2.lua"
    require "/items/active/weapons/weapon.lua"

    function init()
    animator.setGlobalTag("paletteSwaps", config.getParameter("paletteSwaps", ""))
    animator.setGlobalTag("directives", "")
    animator.setGlobalTag("bladeDirectives", "")

    self.weapon = Weapon:new()

    self.weapon:addTransformationGroup("weapon", {0,0}, util.toRadians(config.getParameter("baseWeaponRotation", 0)))
    self.weapon:addTransformationGroup("swoosh", {0,0}, math.pi/2)

    local primaryAbility = getPrimaryAbility()
    self.weapon:addAbility(primaryAbility)

    local secondaryAttack = getAltAbility()
    if secondaryAttack then
    self.weapon:addAbility(secondaryAttack)
    end

    self.weapon:init()

    self.activeTime = config.getParameter("activeTime", 2.0)
    self.activeTimer = 0
    animator.setAnimationState("blade", "inactive")
    end

    function update(dt, fireMode, shiftHeld)
    self.weapon:update(dt, fireMode, shiftHeld)

    local nowActive = self.weapon.currentAbility ~= nil
    if nowActive then
    if self.activeTimer == 0 then
    animator.setAnimationState("blade", "extend")
    status.overConsumeResource("energy", 10)
    end
    self.activeTimer = self.activeTime
    elseif self.activeTimer > 0 then
    self.activeTimer = math.max(0, self.activeTimer - dt)
    if self.activeTimer == 0 or status.resourceLocked("energy") then
    animator.setAnimationState("blade", "retract")
    util.wait(0.15)
    animator.setAnimationState("blade", "inactive")
    self:setState(self.init)
    end
    end
    end

    function uninit()
    self.weapon:uninit()
    end


    function update(dt, fireMode, shiftHeld)
    self.weapon:update(dt, fireMode, shiftHeld)

    local nowActive = self.weapon.currentAbility ~= nil
    if nowActive then
    if self.activeTimer == 0 then
    animator.setAnimationState("blade", "extend")
    status.overConsumeResource("energy", 10)
    end
    self.activeTimer = self.activeTime
    elseif self.activeTimer > 0 then
    self.activeTimer = math.max(0, self.activeTimer - dt)
    if self.activeTimer == 0 or status.resourceLocked("energy") then
    animator.setAnimationState("blade", "retract")
    util.wait(0.15)
    animator.setAnimationState("blade", "inactive")
    self:setState(self.init)
    end
    end
    end
     
  2. projectmayhem

    projectmayhem Spaceman Spiff

    Can you log in, use the sword until energy expires, get the issue again and then post your error log. Might be something in there that will point to the problem.

    Have you tried dropping this part

    util.wait(0.15)
    animator.setAnimationState("blade", "inactive")
    self:setState(self.init)

    After going into retract state, your animation file should automatically kick it to inactive after your retract time defined in the animation file. Should need the lua to make it switch to the inactive.

    Not sure what the self:setState(self.init) does, since im no lua expert.
     
    Last edited: Jul 20, 2018
  3. Magmatico

    Magmatico Void-Bound Voyager

    I tried dropping the part, but what i got is an infinitely looping retract animation while my energy is recharging. But i didn't think of using the log though :p
     
  4. Magmatico

    Magmatico Void-Bound Voyager

    Code:
    [Error] Exception while invoking lua function 'update'. (LuaException) Error code 2, attempt to yield from outside a coroutine
    stack traceback:
        [C]: in ?
        [C]: in function 'coroutine.yield'
        [string "/scripts/util.lua"]:340: in field 'wait'
        [string "/items/active/weapons/redplasma/redplasmamele..."]:44: in function <[string "/items/active/weapons/redplasma/redplasmamele..."]:30>
    There's the problem. I have no idea where did the coroutine start, and since i haven't put coroutine.yield() anywhere i dont see any problem...
    EDIT: I found the problem - apparently util.wait needs a coroutine to work in. However, i have no idea where to start one xD
     
    Last edited: Jul 21, 2018
  5. Magmatico

    Magmatico Void-Bound Voyager

    upload_2018-7-21_11-43-19.png
    After i changed up some code so it would instead make the retract loop continuously once you run out of energy, retracting animation is played once more once the active time is over, and if you swing before it happens the blade disappears.
     

Share This Page