Jump to content

Error in entity parsing code


Recommended Posts

I want to use the following code to get entities near each player:

AddPlayerPostInit(function(inst)
    inst:DoPeriodicTask(1.0, function(inst)
        local pp = inst.Transform:GetWorldPosition()
        local ents = GLOBAL.TheSim:FindEntities(pp.x, pp.y, pp.z, 100)
    end)
end)

However it crashes my game with the following error:

[string *../mods/MyMod/modmain.lua"]:25: attempt to index local 'pp' (a number value)
LUA ERROR stack traceback:
	../mods/MyMod/modmain.lua:25 in (field) fn (Lua) <23-26>
      scripts/scheduler.lua:177 in (method) OnTick (Lua) <155-207>
      scripts/scheduler.lua:371 in (global) RunScheduler (Lua) <369-377>
      scripts/update.lua:185 in () ? (Lua) <164-243>

Any idea what I'm doing wrong? I'm brand new to modding DST so I don't know if I have a syntax error or what.

 

Link to comment
Share on other sites

This being said, you can use GetPosition but it's a vector3:

local pp = inst.Transform:GetPosition()
print(pp.x, pp.y, pp.z) -- By part manually
print(pp:Get()) -- Returns the x, y, and z components

There are advantages in keeping it as a Vec3 as there are wrapper functions with the object type to make doing math with them simpler.

They're all defined in scripts/vector3.lua.

Edited by CarlZalph
  • Health 1
Link to comment
Share on other sites

It worked, thanks guys. Though I don't get why pp.x etc... don't work and you have to use pp:GET(), as surely if you can print them they're public members and can be accessed directly? At any rate that doesn't matter to the mod, which now works as intended. Thanks again!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...