Page 3 of 8
Re: How to check which item is in alcove?
Posted: Wed Sep 26, 2012 3:35 pm
by Grimwold
Great. Thanks everyone. This final iteration seems to work well and is presumably more efficient. This should probably go in the scripting thread if it's not already.
Re: How to check which item is in alcove?
Posted: Wed Sep 26, 2012 3:37 pm
by Montis
As the previous posters said: break does break out of your innermost loop, return breaks out of the complete function and let's the function return values if need be.
Re: How to check which item is in alcove?
Posted: Wed Sep 26, 2012 3:40 pm
by Komag
yes, I've already added it to the script repository (under 1.1.7)
Re: How to check which item is in alcove?
Posted: Wed Sep 26, 2012 4:25 pm
by petri
I would write it like this (less redundancy and cleaner control flow):
Code: Select all
function gemCheckblue()
local itemFound = false
for i in alcoveblue:containedItems() do
if i.name == "blue_gem" then
itemFound = true
end
end
if itemFound then
doorblue:open()
else
doorblue:close()
end
end
This could also be generalized to:
Code: Select all
-- This function returns true if the entity contains a given item.
-- It works for any entity that implements the containedItems() method.
function containsItem(entity, item)
for i in entity:containedItems() do
if i.name == item then
return true
end
end
-- if we get here the item was not found
return false
end
-- example usage
if containsItem(alcoveblue, "blue_gem") then
doorblue:open()
else
doorblue:close()
end
Re: How to check which item is in alcove?
Posted: Wed Sep 26, 2012 5:45 pm
by Komag
ah, so that's how to apply that version, thanks!
(would be nice to do it in-editor, such as "require specific object? [] " checkbox, with entry for typing in the object)
Re: How to check which item is in alcove?
Posted: Wed Sep 26, 2012 5:52 pm
by petri
Komag wrote:(would be nice to do it in-editor, such as "require specific object? [] " checkbox, with entry for typing in the object)
That would not be very generic. Should it require a specific id, or specific name? Or maybe require a weapon (an item with attack power)? How about requiring more than one item? Should the alcove be deactivated when another item is placed in it when the correct item is already in it? These kind of conditions are better expressed in code.
Re: How to check which item is in alcove?
Posted: Wed Sep 26, 2012 6:13 pm
by Komag
hmm, I see, with all those possible scenarios and wants, I guess code is really the best way to go in this case
Re: How to check which item is in alcove?
Posted: Fri Oct 05, 2012 1:43 am
by Komag
I'm trying to recreate the weapon check alcove from the original game level 2, but I haven't seen any hints about how to check whether an item placed in an alcove is weapon, other than making a complete list of all the weapons and having it check for any of them specifically. There's got to be a better way! Ideas?
Re: How to check which item is in alcove?
Posted: Fri Oct 05, 2012 1:45 am
by vorebane
I really like the script, but I would like to get a little trickier, and check to make sure all of several items are in the alcove before anything happens. Because my understanding of the for loop is that it runs all through the code beneath it before it changes the object that i points to, I'll have to run the brute force method of for loops inside of my for loops. Is anything more elegant possible? I'll be sure to show off my brute strength for loops once I've written those, at least!
edit:
As far as figuring out whether something is a weapon or not, maybe look around
http://www.grimrock.net/modding/asset-d ... reference/. I suspect you could check for the boolean values rangedweapon, meleeweapon, or thrownweapon, or maybe even just accuracy or attackpower.
Re: How to check which item is in alcove?
Posted: Fri Oct 05, 2012 2:06 am
by Komag
I did notice those but I'm not sure how to apply it to the script