Page 2 of 2

Re: [FUNCTION] Find entities with text and method filtering

Posted: Tue Dec 04, 2012 5:31 am
by Komag
Okay, thanks, that does help it make much more sense.

And I tested and the ( ) are not necessary, but maybe Lark has a - perhaps wise/good - habit of including them even if they sometimes are redundant, because other times they are needed.

so now it basically boils down to:

Code: Select all

totalMonsters = 0
function countMonsters()
  for i=1,13 do
    for j in allEntities(i) do
      if j["setAIState"] ~= nil then
         j:setLevel(2)
         totalMonsters = totalMonsters + 1
      end
    end
  end
  print("there are "..totalMonsters.." monsters")
end
[/color][/b]

with the key to it all being the line:
if j["setAIState"] ~= nil then
which says "in the table of properties/info for entity j, if there is an item with the string "setAIState" then do something"

Is there a way to "print" all the properties of an entity, to print all its table values?
I tried:
for i in findEntity("spider_shock_1") do print(i) end and
for i in findEntity("spider_shock_1") do print(spider_shock_1) end and
for i in spider_shock_1 do print(i) end
but I got ":1: attempt to call a table value" error message or ":1: attempt to call a nil value"

Re: [FUNCTION] Find entities with text and method filtering

Posted: Tue Dec 04, 2012 6:56 am
by Batty
So far, I've found that this:

Code: Select all

print(entity["name"])
gives the same result as this:

Code: Select all

print(entity.name)
I tried with interesting things like "model" but only got nil, would be cool to spit out all an objects info. but it may be inaccessible.

Re: [FUNCTION] Find entities with text and method filtering

Posted: Tue Dec 04, 2012 8:24 am
by petri
In Lua entity["something"] is exactly the same as entity.something. The latter notation is just a shortcut and it exists in Lua because it would be very tedious to write the brackets and quotations all the time when accessing tables. The bracket notation is useful when e.g. the key between the brackets is not a string or contains spaces. For example,

entity["some property with spaces"] can't be written as "entity.some property with spaces" because that would be a syntax error.

Re: [FUNCTION] Find entities with text and method filtering

Posted: Tue Dec 04, 2012 4:43 pm
by Komag
Ah, that helps make it more clear, thanks :)