Page 1 of 1

entitiesAt()

Posted: Tue Jan 12, 2016 5:46 am
by RayB
I have written various pieces of script in Grimrock1 where I need to find an entity at a certain tile in the game; for example looking to see if there is a pit in front of the party. The 'for i in entitiesAt(level,x,y) do' script would return (I believe) the table value which could be used to see if a pit was there. I have noticed there is no entitiesAt function in Grimrock2. Can anyone tell me how this would be done in Grim2? Also, in viewing some of the tutorials for Grimrock2 I have notice that the iteration statements start like this: 'for v,i in ...'. Can anyone tell me what the v is for. I am trying to get a handle on this but the new script is a lot different than Grim1 and I am a bit confused. Any help would be appreciated. Thank you.

Re: entitiesAt()

Posted: Tue Jan 12, 2016 8:09 am
by minmay
entitiesAt is no longer a global function, it belongs to the Map class.

Re: entitiesAt()

Posted: Wed Jan 13, 2016 5:39 pm
by RayB
Thank you.
Any help on the extra variable in the iteration loop (the '"'v'"' variable mentioned in the latter part of the post)?
P.S. I think I may be in the wrong part of the forum for this. Sorry!

Re: entitiesAt()

Posted: Wed Jan 13, 2016 10:31 pm
by minmay
The construct is:

Code: Select all

for [variables] in [iterator] do [block] end
An iterator, like any other function in Lua, can have multiple return values. The iterators returned by pairs and ipairs are examples; they return both a key and the corresponding value in the table they're iterating over. So, for example, if you wanted to copy a table, you could use code like

Code: Select all

for k,v in pairs(table1) do
  table2[k] = v
end
A common convention is to use an underscore if you don't care about one or more of the return values, so you might see code like

Code: Select all

for _,dog in ipairs(dogs) do
  pet(dog)
end
but this is purely a convention and is not part of the language ('_' is a valid variable name like any other).

The iterator returned by Map:entitiesAt() and Map:allEntities() only returns one value, a GameObject. So blocks for those just look like

Code: Select all

for e in party.map:allEntities() do
  print(e.id)
end

Re: entitiesAt()

Posted: Thu Jan 14, 2016 3:54 am
by RayB
Thank you so much for the explanation. I was using iterators because I saw examples of such in the Grim 1 Scripting Reference but I didn't really understand them. Your explanation and code examples helped a lot.

I have other questions but will ask them in the Mod Creation forum as I think that is where I should be for this stuff. Please correct me if I am wrong.

Thanks again!