Jump to content

Looping entities


Recommended Posts

OK I'm confused as to what is happening here. I'm trying to create a GetEntityByName(name) function. In it, it will loop CurrentWorld().entities looking for the key name you pass in. So step one I just want to print all entity names.


for model in iterate(CurrentWorld().entities) do
	Print("Searching name.. "..model:GetKey("Name"))


The thing I don't get is if I put this in the Update() method it prints all the entities, BUT if I put this in a function that gets ran just once from inside Update() (it's like an init thing inside Update() where it'll only run the function inside and if statement) in my log all I see is 1 line of "Searching name... ". It's blank.




Here is the code in monster truck lua


How it's setup below I see Inside test print in the log but no entities looped through in the log. If I uncomment the GetEntityByName("test") in the Update() method it does loop through all entities. So what's the difference?

function Test()
Print("Inside test")

function Update(world)
local model
local entity
for model,entity in pairs(entitytable) do

if(init == false) then
	if(GetGlobalNumber("game") == 1) then
		--Print("Running MyScript")
		init = true

--for m in iterate(CurrentWorld().entities) do
--	Print("Searching name.. "..m:GetKey("Name"))



Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...