Modding Help (Solved) Nearest entity id

Discussion in 'Starbound Modding' started by lerdarim, Nov 18, 2019.

  1. lerdarim

    lerdarim Void-Bound Voyager

    Hello again everyone (although it looks like not much left). I have yet another question, this time regarding world.entityQuery function.

    I'm have an ability that creates projectile on aim position but i'm trying to add some aim assist and lock onto a creature if one is found nearby. I took markedshot for example and almost completely stripped its scripts to

    Code:
    local newTarget = world.entityQuery(activeItem.ownerAimPosition(), 5, { includedTypes = {"creature"} })
        local coords = activeItem.ownerAimPosition()
          if #newTarget then
            coords = newTarget[1].position()
          end
    Then "coords" are used in projectile spawn function. It works peachy if no entity is found, but breaks if somebody's spotted. So my question is... WHAT THE HELL DOES THAT EVEN RETURN? I mean entityQuery creates a list of entity ids, right? So... specifying index in that list (which is done in markedshot btw) returns the id of that one entity? NO, it returns -65535. So... maybe it returns a ref to an entity? Wrong again, because newTarget[1].id doesn't work either.

    So... my question is simple. How the heck do you ever get entity's id from a list that contains those ids? >_<
     
  2. lerdarim

    lerdarim Void-Bound Voyager

    Oh wait, found something in util

    Code:
    function util.closestValidTarget(range)
      local newTargets = world.entityQuery(entity.position(), range, { includedTypes = {"player", "npc", "monster"}, order = "nearest" })
      local valid = util.find(newTargets, function(targetId) return entity.isValidTarget(targetId) and entity.entityInSight(targetId) end)
      return valid or 0
    end
    
    Gotta try that out.
     
  3. lerdarim

    lerdarim Void-Bound Voyager

    Also though it's not the question here, my understanding of entity.position() was incorrect since it works with current entity...

    Edit.
    Oh jee, got it to work. I would delete this thread if i could but meh. It did its purpose though.
     
    Last edited: Nov 18, 2019
  4. Zaakari

    Zaakari Zero Gravity Genie

    Well, posts like these can still be useful for other people that happen to have the same question.

    Good job figuring it out on your own (I've been doing some non-Starbound related stuff recently, so I haven't been around as much).
     

Share This Page