Jump to content

Recommended Posts

I'm trying to get the coordinate location of each wormhole and the location of its targetTeleporter.

 

My initial approach was to use TheSim:FindEntities(x,y,z, 10000, "wormhole") but from some other post on this forum I learnt that wormholes don't spawn with tags so I have to add my own. (it was the one with the colourful wormhole openings)

 

Using the debug entity on a wormhole shows the tag is added correctly.

 

There are two places where stuff is written to console (once in populate_wormhole_list and another in wormhole_target_printer) but the latter part never runs because "attempt to index field 'teleporter' (a nil value)".

 

The first dumptable runs for very very long and uses lots of memory before printing what appears to be a list of every entity in the world (...) the last one has the name "flower".

 

So I guess TheSim:FindEntities is not giving me a list of wormholes with the teleporter component but instead a list of every object.

 

I have a feeling there is a lot that's wrong with what I have written.

local require = GLOBAL.requirerequire "debugtools"GLOBAL.CHEATS_REQUIRED = truerequire "debugkeys"local wormholes = {}function populate_wormhole_list()	local wh_index = 1	local x,y,z = GLOBAL.GetPlayer().Transform:GetWorldPosition()	local ents = TheSim:FindEntities(x,y,z, 10000, "is_wh")	GLOBAL.dumptable(ents, 3, 1)	for k, v in pairs(ents) do		wormholes[wh_index] = {}		wormholes[wh_index].pos = v.Transform:GetWorldPosition();		wormholes[wh_index].tg_pos = v.components.teleporter.targetTeleporter.Transform:GetWorldPosition();		wh_index = wh_index + 1	endendfunction wormhole_target_printer() -- this is the entry point	populate_wormhole_list()	GLOBAL.dumptable(wormholes, 3)endfunction wormhole_tagger(inst)	inst:AddTag("is_wh")endAddPrefabPostInit("wormhole", wormhole_tagger)

I am using simplex's Mod Testing Toolbox. Anyone have any ideas? Thanks in advance.

Here's a different approach, which should be faster, cleaner and easier:

Use the global table Ents, which contains all instances in the world. You can check for the wormholes using .prefab

for k, v in pairs(GLOBAL.Ents) do    if v.prefab == "wormhole" then--stuff    endend

Thanks, that worked, much faster too. All the code I've seen used TheSim:FindEntities but maybe that's because they only want ents a certain distance from the player?

 

Exactly. TheSim:FindEntities seeks all instances with certain criteria in the radius given. If you only want to know whether something is in range, there is IsNear, if you need a single instance you can do inst:FindEntity, and to rummage through all existing entities you can use the Ents table.

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
×
  • Create New...