Page 1 of 1
[Solved!] Parsing only items using entitiesAt
Posted: Thu Oct 18, 2012 10:58 pm
by Wolfrug
Hello everyone,
I'm stumped. There's a command, entitiesAt, that lets you find all entities at a given level,x,y. Great! Now I want to use it to find all ITEMS in a given square for a puzzle. The problem here is that it also finds doors. And fx. And wall hangings. And all kinds of things it's really not supposed to find. And then it crashes when you try to do item commands on these non-items.
The problem is, however, that as soon as you try to differentiate items and non-items, it also crashes. You can't go, for instance,
because the entity in question does not have a 'weight', which - rather than then returning nil - instead crashes the game. This is imho dumb to begin with (it should return something else instead of CRASH), but what I'm wondering now is if there's some way past this? Some way to do the standard "for i in entitiesAt ..." thing, parsing out all non-items and checking the rest. I can't use .id or .name since I want it to find ANY item (it's for a kind of 'need this much weight to activate' puzzle, meaning players can put anything on there from torches to junk)
Any help much appreciated!
-Wolfrug
Re: [Help] Parsing only items using entitiesAt
Posted: Fri Oct 19, 2012 12:24 am
by Decayer
if i.getWeight then...
Should work, off to bed.
Re: [Help] Parsing only items using entitiesAt
Posted: Fri Oct 19, 2012 7:06 am
by Szragodesca
I noticed when defining an item for a script that it has
in its code. Perhaps if you tried to nest your weight script inside an if statement checking the entity's class to make sure it == "Item" first?
Example, this taken from the New Spells thread.
Code: Select all
defineObject{
name = "scroll_healing",
class = "Item",
uiName = "Scroll of Healing",
model = "assets/models/items/scroll_spell.fbx",
gfxIndex = 113,
scroll = true,
spell = "healing",
weight = 0.3,
}
I bet you can look for the i.class attribute before anything, and ignore any entity where i.class ~= "Item".
Re: [Help] Parsing only items using entitiesAt
Posted: Fri Oct 19, 2012 6:03 pm
by Wolfrug
Thanks, but neither of those suggestions work, unless you can prove to me how. The .dot operator only works for the things listed in the Scripting Reference (.name, .id, .x, .y etc), and there's no way to access the object definition data except through the commands like getWeight (if .weight worked, why would there even -be- a command like that?) -- and, again, if you try to run an invalid command it crashes the game.
So we're still back at zero. What frustrates me right now is that my workaround isn't working either. I've got this massively ugly if...then...elseif script running, trying to parse out the non-items, but it DOESN'T WORK and I really don't know why.
Code: Select all
function weightCheck()
local allItems = {}
for i in entitiesAt(self.level, self.x, self.y) do
if i.name ~= "prison_secret_door"
then table.insert(allItems,i)
elseif i.name ~= "prison_wall_2"
then table.insert(allItems,i)
elseif i.name ~= "script_entity"
then table.insert(allItems,i)
elseif i.name ~= "prison_pillar"
then table.insert(allItems,i)
elseif i.name ~= "prison_floor_drainage"
then table.insert(allItems,i)
elseif i.name ~= "prison_ceiling_lamp"
then table.insert(allItems,i)
elseif i.name ~= "party"
then table.insert(allItems,i) end
end
print (allItems[1].name)
end
Prints "prison_secret_door" even though it really shouldn't.

Anyone else?
-Wolfrug
Re: [Help] Parsing only items using entitiesAt
Posted: Fri Oct 19, 2012 6:29 pm
by Decayer
Code: Select all
for i in entitiesAt(wherever) do
if i.getWeight then
print(i.id, i:getWeight())
end
end
Does that work for you?
Re: [Help] Parsing only items using entitiesAt
Posted: Fri Oct 19, 2012 6:33 pm
by Wolfrug
Decayer wrote:Code: Select all
for i in entitiesAt(wherever) do
if i.getWeight then
print(i.id, i:getWeight())
end
end
Does that work for you?

Yes it does. So the .dot operator can be used to find out if a given command works on a given entity? I honestly had no idea! This means its possible to actually parse through allEntities in a more useful way - finding all things that can be open()'d and activate()'d and so on. Thanks a ton!
(today seems to be my day for claiming things I have no idea about. Ah well, one learns!)
-Wolfrug
Re: [Help] Parsing only items using entitiesAt
Posted: Fri Oct 19, 2012 6:45 pm
by Decayer
Wolfrug wrote:Decayer wrote:Code: Select all
for i in entitiesAt(wherever) do
if i.getWeight then
print(i.id, i:getWeight())
end
end
Does that work for you?

Yes it does. So the .dot operator can be used to find out if a given command works on a given entity? I honestly had no idea! This means its possible to actually parse through allEntities in a more useful way - finding all things that can be open()'d and activate()'d and so on. Thanks a ton!
(today seems to be my day for claiming things I have no idea about. Ah well, one learns!)
-Wolfrug
The dot operator simply returns that particular entity property (or nil if it doesn't exist); you might also want to see if type(i.getWeight) returns "function" before trying to call it to make sure that it is a function and not some other type.
Glad it worked!