[Solved!] Parsing only items using entitiesAt

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
User avatar
Wolfrug
Posts: 55
Joined: Wed Oct 03, 2012 6:56 pm

[Solved!] Parsing only items using entitiesAt

Post 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,

Code: Select all

if i:weight() ~= then...
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
Last edited by Wolfrug on Fri Oct 19, 2012 6:37 pm, edited 1 time in total.
Try my Mordor: Depths of Dejenol LoG-ification. Feedback much appreciated!
Decayer
Posts: 65
Joined: Sat Oct 13, 2012 3:19 pm

Re: [Help] Parsing only items using entitiesAt

Post by Decayer »

if i.getWeight then...

Should work, off to bed.
Szragodesca
Posts: 59
Joined: Sun Oct 14, 2012 7:13 am

Re: [Help] Parsing only items using entitiesAt

Post by Szragodesca »

I noticed when defining an item for a script that it has

Code: Select all

class = "Item"
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".
User avatar
Wolfrug
Posts: 55
Joined: Wed Oct 03, 2012 6:56 pm

Re: [Help] Parsing only items using entitiesAt

Post 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. :evil:

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
Try my Mordor: Depths of Dejenol LoG-ification. Feedback much appreciated!
Decayer
Posts: 65
Joined: Sat Oct 13, 2012 3:19 pm

Re: [Help] Parsing only items using entitiesAt

Post 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?
User avatar
Wolfrug
Posts: 55
Joined: Wed Oct 03, 2012 6:56 pm

Re: [Help] Parsing only items using entitiesAt

Post 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?
:oops: 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! :D

(today seems to be my day for claiming things I have no idea about. Ah well, one learns!)

-Wolfrug
Try my Mordor: Depths of Dejenol LoG-ification. Feedback much appreciated!
Decayer
Posts: 65
Joined: Sat Oct 13, 2012 3:19 pm

Re: [Help] Parsing only items using entitiesAt

Post 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?
:oops: 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! :D

(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!
Post Reply