Jump to content

Looping entities


Rick
 Share

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"))
end

 

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.

 

 

[EDIT]

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")
GetEntityByName("Rick")
end

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

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

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

--GetEntityByName("test")

UpdateCR()
end

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.

Guest
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.

 Share

×
×
  • Create New...