1. Hey Guest! We've announced our new game Wargroove, check out the new forums!
    Dismiss Notice

Modding Help Getting the ground position of ownerAimPosition

Discussion in 'Starbound Modding' started by Tamamo89, Feb 13, 2017.

  1. Tamamo89

    Tamamo89 Subatomic Cosmonaut

    Code:
    function ControlProjectile:groundposition()
      local startline = activeItem.ownerAimPosition()
      local endline = vec2.add(startline,{0,-25.0})
      local blocks =  world.collisionBlocksAlongLine(startLine, endLine, {"Null", "Block", "Dynamic"})
      if #blocks > 0 then
        return vec2.add(blocks[#blocks], {0, 1.25})
      end
    end
    Here's the error I'm getting

    Code:
    [13:22:20.226] [Error] Exception while invoking lua function 'update'. (LuaException) Error code 2, [string "/items/active/weapons/weapon.lua"]:117: [string "/items/active/weapons/weapon.lua"]:178: [string "/items/active/weapons/staff/abilities/crystal..."]:233: (LuaConversionException) Error converting LuaValue to type 'class Star::Vector<float,2>'
     
  2. bk3k

    bk3k Ketchup Robot

    try this
    if (#blocks) > 0 then

    edit: actually is the problem even that function? The error references update not ControlProjectile:groundposition
    It also mentions the line number - where the error actually triggered(not necessarily the root cause), but what is on that line is something only you know.
     
    Last edited: Feb 14, 2017
  3. Tamamo89

    Tamamo89 Subatomic Cosmonaut

    Code:
    require "/scripts/vec2.lua"
    require "/scripts/util.lua"
    
    ControlProjectile = WeaponAbility:new()
    
    function ControlProjectile:init()
      storage.projectiles = storage.projectiles or {}
    
      self.elementalType = self.elementalType or self.weapon.elementalType
    
      self.baseDamageFactor = config.getParameter("baseDamageFactor", 1.0)
      self.stances = config.getParameter("stances")
    
      activeItem.setCursor("/cursors/reticle0.cursor")
      self.weapon:setStance(self.stances.idle)
    
      self.weapon.onLeaveAbility = function()
        self:reset()
      end
      self.castposition = nil
    end
    
    function ControlProjectile:update(dt, fireMode, shiftHeld)
      WeaponAbility.update(self, dt, fireMode, shiftHeld)
    
      self:updateProjectiles()
    
      world.debugPoint(self:focusPosition(), "blue")
    
      if self.fireMode == (self.activatingFireMode or self.abilitySlot)
        and not self.weapon.currentAbility
        and not status.resourceLocked("energy") then
    
        self:setState(self.charge)
      end
    end
    
    function ControlProjectile:charge()
      self.weapon:setStance(self.stances.charge)
    
      animator.playSound(self.elementalType.."charge")
      animator.setAnimationState("charge", "charge")
      animator.setParticleEmitterActive(self.elementalType .. "charge", true)
      activeItem.setCursor("/cursors/charge2.cursor")
    
      local chargeTimer = self.stances.charge.duration
      while chargeTimer > 0 and self.fireMode == (self.activatingFireMode or self.abilitySlot) do
        chargeTimer = chargeTimer - self.dt
    
        mcontroller.controlModifiers({runningSuppressed=true})
    
        coroutine.yield()
      end
    
      animator.stopAllSounds(self.elementalType.."charge")
    
      if chargeTimer <= 0 then
        self:setState(self.charged)
      else
        animator.playSound(self.elementalType.."discharge")
        self:setState(self.cooldown)
      end
    end
    
    function ControlProjectile:charged()
      self.weapon:setStance(self.stances.charged)
    
      animator.playSound(self.elementalType.."fullcharge")
      animator.playSound(self.elementalType.."chargedloop", -1)
      animator.setParticleEmitterActive(self.elementalType .. "charge", true)
    
      local targetValid
      while self.fireMode == (self.activatingFireMode or self.abilitySlot) do
        targetValid = self:targetValid(activeItem.ownerAimPosition())
        activeItem.setCursor(targetValid and "/cursors/chargeready.cursor" or "/cursors/chargeinvalid.cursor")
    
        mcontroller.controlModifiers({runningSuppressed=true})
    
        coroutine.yield()
      end
    
      self:setState(self.discharge)
    end
    
    function ControlProjectile:discharge()
      self.weapon:setStance(self.stances.discharge)
    
      activeItem.setCursor("/cursors/reticle0.cursor")
    
      if self:targetValid(activeItem.ownerAimPosition()) and status.overConsumeResource("energy", self.energyCost * self.baseDamageFactor) then
        animator.playSound(self.elementalType.."activate")
        self:createProjectiles()
      else
        animator.playSound(self.elementalType.."discharge")
        self:setState(self.cooldown)
        return
      end
    
      util.wait(self.stances.discharge.duration, function(dt)
        status.setResourcePercentage("energyRegenBlock", 1.0)
      end)
    
      while #storage.projectiles > 0 do
        if self.fireMode == (self.activatingFireMode or self.abilitySlot) and self.lastFireMode ~= self.fireMode then
          self:killProjectiles()
        end
        self.lastFireMode = self.fireMode
    
        status.setResourcePercentage("energyRegenBlock", 1.0)
        coroutine.yield()
      end
    
      animator.playSound(self.elementalType.."discharge")
      animator.stopAllSounds(self.elementalType.."chargedloop")
    
      self:setState(self.cooldown)
    end
    
    function ControlProjectile:cooldown()
      self.weapon:setStance(self.stances.cooldown)
      self.weapon.aimAngle = 0
    
      animator.setAnimationState("charge", "discharge")
      animator.setParticleEmitterActive(self.elementalType .. "charge", false)
      activeItem.setCursor("/cursors/reticle0.cursor")
    
      util.wait(self.stances.cooldown.duration, function()
    
      end)
    end
    
    function ControlProjectile:targetValid(aimPos)
      local focusPos = self:focusPosition()
      return world.magnitude(focusPos, aimPos) <= self.maxCastRange
          and not world.lineTileCollision(mcontroller.position(), focusPos)
          and not world.lineTileCollision(focusPos, aimPos)
    end
    
    function ControlProjectile:createProjectiles()
      self.castPosition = self.groundposition()
      local fireDirection = vec2.withAngle(0)
      local xoffset = {
        0,
        0.5,
        1,
        0.5,
        0,
        0.5,
        -1,
        -0.5,
      }
      local basePos = self.castPosition
    
      local pCount = self.projectileCount or 1
    
      local pParams = copy(self.projectileParameters)
      pParams.power = self.baseDamageFactor * pParams.baseDamage * config.getParameter("damageLevelMultiplier") / pCount
      pParams.powerMultiplier = activeItem.ownerPowerMultiplier()
    
      for i = 1, pCount do
        local position = vec2.add(basePos,  {xoffset[((i-1)%8)+1], 1.25 * (i - 1)})
        local projectileId = world.spawnProjectile(
            self.projectileType,
            position,
            activeItem.ownerEntityId(),
            fireDirection,
            false,
            pParams
          )
    
        if projectileId then
          table.insert(storage.projectiles, projectileId)
          --world.sendEntityMessage(projectileId, "updateProjectile", aimPosition)
        end
      end
    end
    
    function ControlProjectile:focusPosition()
      return vec2.add(mcontroller.position(), activeItem.handPosition(animator.partPoint("stone", "focalPoint")))
    end
    
    -- give all projectiles a new aim position and let those projectiles return one or
    -- more entity ids for projectiles we should now be tracking
    function ControlProjectile:updateProjectiles()
      --local aimPosition = activeItem.ownerAimPosition()
      --local newProjectiles = {}
      for _, projectileId in pairs(storage.projectiles) do
        if world.entityExists(projectileId) then
          --projectile movement
          if world.magnitude(world.entityPosition(projectileId), self.castPosition) >= 1.25 then
            world.sendEntityMessage(projectileId, "reverse")
          end
          --[[local projectileResponse = world.sendEntityMessage(projectileId, "updateProjectile", aimPosition)
          if projectileResponse:finished() then
            local newIds = projectileResponse:result()
            if type(newIds) ~= "table" then
              newIds = {newIds}
            end
            for _, newId in pairs(newIds) do
              table.insert(newProjectiles, newId)
            end
          end]]
        end
      end
      --storage.projectiles = newProjectiles
    end
    
    function ControlProjectile:killProjectiles()
      for _, projectileId in pairs(storage.projectiles) do
        if world.entityExists(projectileId) then
          world.sendEntityMessage(projectileId, "kill")
        end
      end
    end
    
    function ControlProjectile:reset()
      self.weapon:setStance(self.stances.idle)
      animator.stopAllSounds(self.elementalType.."chargedloop")
      animator.stopAllSounds(self.elementalType.."fullcharge")
      animator.setAnimationState("charge", "idle")
      animator.setParticleEmitterActive(self.elementalType .. "charge", false)
      activeItem.setCursor("/cursors/reticle0.cursor")
    end
    
    function ControlProjectile:uninit(weaponUninit)
      self:reset()
      if weaponUninit then self:killProjectiles() end
    end
    
    function ControlProjectile:groundposition()
      local startline = activeItem.ownerAimPosition()
      local magnitude = {0,25}
      local blocks =  world.collisionBlocksAlongLine(startline, vec2.sub(startline,magnitude), {"Null", "Block", "Dynamic"})
      if #blocks > 0 then
        return vec2.add(blocks[#blocks], {0, 1.25})
      end
    end
    
    
     
    Last edited: Feb 14, 2017
  4. Inf_Wolf14

    Inf_Wolf14 Giant Laser Beams

    I'm gonna make an educated guess...
    In your script, within ControlProjectile:createProjectiles():
    You are using "aimVec" for your argument but I don't see the variable referenced throughout the rest of the script.
    Im assuming undeclared variables default as LuaValues, so that would explain the error.



    Only a guess, I'm viewing this on the go, and cant match lines in the error to your segment.
    Just my best guess at the moment. :geek:
     
    Last edited: Feb 14, 2017
  5. Tamamo89

    Tamamo89 Subatomic Cosmonaut

    whoops, yeah but i'm still getting an error on line 233. The aimVec error would of generated afterwards.
     
  6. Inf_Wolf14

    Inf_Wolf14 Giant Laser Beams

    For a little reference, what line is it referring to? I cant pinpoint which one exactly is 233...
     
  7. Tamamo89

    Tamamo89 Subatomic Cosmonaut

    Code:
    function ControlProjectile:groundposition()
      local startline = activeItem.ownerAimPosition()
      local magnitude = {0,25}
      local blocks =  world.collisionBlocksAlongLine(startline, vec2.sub(startline,magnitude), {"Null", "Block", "Dynamic"}) --thisline
      if #blocks > 0 then
        return vec2.add(blocks[#blocks], {0, 1.25})
      end
    end
     

Share This Page