Scripting Help for "3 Item Menu"

Have trouble running Legend of Grimrock 2 or do you have questions about the purchasing options? Look for help here.
Post Reply
User avatar
David Ward
Posts: 103
Joined: Wed Jan 07, 2015 11:44 pm
Location: Vancouver, BC, Canada

Scripting Help for "3 Item Menu"

Post by David Ward »

Hello. I'm looking for some scripting help. There is a very specific script that I am trying to create but it is proving to be more difficult than I had first considered. I would be willing to trade services using the editor for some assistance in this regard.

The intention of the script is as follows.

There are 3 altars. There is 1 item on each altar, a prize, for completing a dungeon. Off to the side is an exit teleporter with an iron gate. The gate is open. If the player takes 1 item, the gate remains open. If the player takes 2 or more items, the gate closes. If the player puts the items back, so that only 1 item has been removed from the altar, the gate opens.

I cannot for the life of me figure out how to make this work. Any help would be muchly appreciated. Am willing to trade time for editor work (dungeon/map making, etc.). Thank you.
User avatar
David Ward
Posts: 103
Joined: Wed Jan 07, 2015 11:44 pm
Location: Vancouver, BC, Canada

Re: Scripting Help for "3 Item Menu"

Post by David Ward »

I've managed to figure this out, but the script code looks hilarious:

Code: Select all

function surfaceContains3(surface, item)
	for v,i in surface:contents() do
	if i.go.name == item then return true
	end
end
end


function deactivatebridge1()
	if surfaceContains3(altart11.surface, "long_sword") then
	magicbridget11.controller:deactivate()
	end
	
	if surfaceContains3(altart22.surface, "frostbite_necklace") then
	magicbridget11.controller:deactivate()
	end

	if surfaceContains3(altart33.surface, "full_helmet") then	
	magicbridget11.controller:deactivate()
	end

	if surfaceContains3(altart11.surface, "long_sword") and
	surfaceContains3(altart22.surface, "frostbite_necklace") and
	surfaceContains3(altart33.surface, "full_helmet") then
	
	magicbridget11.controller:activate()
	
	end
	
	if surfaceContains3(altart11.surface, "long_sword") and
	surfaceContains3(altart22.surface, "frostbite_necklace") then
	
	magicbridget11.controller:activate()
		
	end
	
	if surfaceContains3(altart11.surface, "long_sword") and
	surfaceContains3(altart33.surface, "full_helmet") then

	magicbridget11.controller:activate()	
		
	end
	
	if surfaceContains3(altart22.surface, "frostbite_necklace") and
	surfaceContains3(altart33.surface, "full_helmet") then

	magicbridget11.controller:activate()	
	
	end
	
end

Basically, If more than 1 of the 3 altar items is taken off their altars, the magic bridge leading to the exit deactivates. It reactivates until at least 2 items are on their altars (and the party has only then taken 1). I'm just glad it works.
User avatar
Isaac
Posts: 3175
Joined: Fri Mar 02, 2012 10:02 pm

Re: Scripting Help for "3 Item Menu"

Post by Isaac »

Here is an alternate method. This script doesn't care where the prizes are, only that at least two of them are on the alcoves.
[If placement is important, it will need to be modified.]

To try it: Add the script, and connect a set of three alcoves to it; their names must end in sequence [_1,_2,_3]. The name prefix used in the script is 'dungeon_alcove'. Change if needed; it should match the prefix of your alcoves. The script uses mine_door_spear_1 by default; change the name in the doorOpen function() to match your chosen door.

Connect each alcove to the script twice; once for insert, and once for removal. Change the string item IDs in the 'prizes' table to match your own.

Code: Select all

prizes = {"figure_skeleton", "figure_ice_guardian", "figure_ogre"}

function _itemCheck()
	local tally = 0
	for x = 1, 3 do
		for _,item in findEntity("dungeon_alcove_"..x).surface:contents() do
			if item ~= nil then
				for i,prize in pairs(prizes) do
					if item.go.name == prize then
						tally = tally + 1
						break
					end
				end
			end
		end	
	end
	return tally >1
end

function doorOpen()
	if _itemCheck() then
		mine_door_spear_1.door:open()
	else mine_door_spear_1.door:close()	
	end
end
https://www.dropbox.com/s/wkj8cubvgahzo ... e.avi?dl=0
User avatar
David Ward
Posts: 103
Joined: Wed Jan 07, 2015 11:44 pm
Location: Vancouver, BC, Canada

Re: Scripting Help for "3 Item Menu"

Post by David Ward »

Okay see, that's awesome. Thank you very much for the reply. I do not at all know the depth of what you have put together there in the code, but I will give it a shot and report back.
User avatar
David Ward
Posts: 103
Joined: Wed Jan 07, 2015 11:44 pm
Location: Vancouver, BC, Canada

Re: Scripting Help for "3 Item Menu"

Post by David Ward »

Followed your directions --> works perfectly. Thank you very kindly for writing that code and posting a reply to my inquiry. You are a gentleman and a scholar.
User avatar
David Ward
Posts: 103
Joined: Wed Jan 07, 2015 11:44 pm
Location: Vancouver, BC, Canada

Re: Scripting Help for "3 Item Menu"

Post by David Ward »

Hi Isaac. I know it's been awhile. I was wondering if there was a way to modify this "choose your 1 prize out of 3" system to make it, say, "2 out of 4". Is there a way to do that? To have 4 alcoves with 1 prize in each, and if more than 2 are taken, then the door shuts until there are at least 2 items still on the alcoves.
User avatar
Isaac
Posts: 3175
Joined: Fri Mar 02, 2012 10:02 pm

Re: Scripting Help for "3 Item Menu"

Post by Isaac »

Set the number of alcoves, and the number of gifts; in this case, 4 and 2.
*Remember the naming scheme for the alcoves.

Code: Select all


    prizes = {"figure_skeleton", "figure_ice_guardian", "figure_ogre", "figure_crowern"}
	alcoves = 4  
	gifts = 2
	
    function _itemCheck()
       local tally = 0
       for x = 1, alcoves do
          for _,item in findEntity("dungeon_alcove_"..x).surface:contents() do
             if item ~= nil then
                for i,prize in pairs(prizes) do
                   if item.go.name == prize then
                      tally = tally + 1
                      break
                   end
                end
             end
          end   
       end
       return tally >= alcoves - gifts
    end

    function doorOpen()
       if _itemCheck() then
          mine_door_spear_1.door:open()
       else mine_door_spear_1.door:close()   
       end
    end

User avatar
David Ward
Posts: 103
Joined: Wed Jan 07, 2015 11:44 pm
Location: Vancouver, BC, Canada

Re: Scripting Help for "3 Item Menu"

Post by David Ward »

Well then....that is service, sir. I did not expect such a quick response. The code works perfectly. You've thoroughly earned a mention in our game credits. Much obliged.
Post Reply