How to check which item is in alcove?

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
User avatar
Grimwold
Posts: 511
Joined: Thu Sep 13, 2012 11:45 pm
Location: A Dungeon somewhere in the UK

Re: How to check which item is in alcove?

Post 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.
User avatar
Montis
Posts: 340
Joined: Sun Apr 15, 2012 1:25 am
Location: Grimrock II 2nd playthrough (hard/oldschool)

Re: How to check which item is in alcove?

Post 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. :)
When destiny calls, the chosen have no choice.

My completed dungeon (LoG1): Hypercube
User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: How to check which item is in alcove?

Post by Komag »

yes, I've already added it to the script repository (under 1.1.7)
Finished Dungeons - complete mods to play
User avatar
petri
Posts: 1917
Joined: Thu Mar 01, 2012 4:58 pm
Location: Finland

Re: How to check which item is in alcove?

Post 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
User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: How to check which item is in alcove?

Post 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)
Finished Dungeons - complete mods to play
User avatar
petri
Posts: 1917
Joined: Thu Mar 01, 2012 4:58 pm
Location: Finland

Re: How to check which item is in alcove?

Post 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.
User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: How to check which item is in alcove?

Post by Komag »

:shock: hmm, I see, with all those possible scenarios and wants, I guess code is really the best way to go in this case
Finished Dungeons - complete mods to play
User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: How to check which item is in alcove?

Post 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?
Finished Dungeons - complete mods to play
vorebane
Posts: 41
Joined: Wed Oct 03, 2012 4:31 am

Re: How to check which item is in alcove?

Post 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.
User avatar
Komag
Posts: 3658
Joined: Sat Jul 28, 2012 4:55 pm
Location: Boston, USA

Re: How to check which item is in alcove?

Post by Komag »

I did notice those but I'm not sure how to apply it to the script
Finished Dungeons - complete mods to play
Post Reply