Scripting help

Talk about creating Grimrock 1 levels and mods here. Warning: forum contains spoilers!
Post Reply
LGreymark
Posts: 4
Joined: Mon Apr 07, 2014 1:00 pm

Scripting help

Post by LGreymark »

Hi there,
I've been working on a custom dungeon (Mostly for my own amusement) and have just started tackling scripting, so far everything has been working fine, save when i tried to get this to work:

Code: Select all

--Opening and closing doors
function open()
	for a in alcove:containedItems() do
		if a.name == "golden_chalice" and
		door1:isClosed() then
			door1:open()
		end
	end
end
Now this is slightly misleading, the code as it is works fine, when the player places the golden_chalice in the alcove, the door opens. The issue comes when I want the door to close if the chalice is removed, I.E to stop the character taking it with them. I've gotten very close, able to set the alcove with a connector that shuts the door if it is deactivated. Unfortunately this carried the issue of allowing the player to simply place any other item on the alcove and remove the golden_chalice, leaving the gate open and allowing them to walk through.

Help please?
User avatar
Leki
Posts: 550
Joined: Wed Sep 12, 2012 3:49 pm

Re: Scripting help

Post by Leki »

Edit: See bellow.
Last edited by Leki on Mon Apr 07, 2014 2:58 pm, edited 2 times in total.
I'm the Gate I'm the Key.
Dawn of Lore
LGreymark
Posts: 4
Joined: Mon Apr 07, 2014 1:00 pm

Re: Scripting help

Post by LGreymark »

I tried your suggestion and found that, instead of the door closing when the chalice is removed, rather it closes only when the chalice is placed on the alcove when the door is open.

In reality what will be needed I feel is a way of checking the contained items for the chalice and have it return a "false" prompt of some description, so that it checks BOTH for a positive and negative result opening and closing the door respectively. I just don't have the talent with LUA to achieve this.
User avatar
Leki
Posts: 550
Joined: Wed Sep 12, 2012 3:49 pm

Re: Scripting help

Post by Leki »

Sorry, I rewrote your's code, but my keyboard battery is weak so new ctrl+c does not worked and I paste "old one" - here's your right one:

Code: Select all

function openGoldenChaliceDoor()
	if my_door:isClosed() then
 	   for a in my_alcove:containedItems() do
			if a.name == "long_sword" and my_alcove:getItemCount() == 1 then
		 	   my_door:open()
		    end
		end
	else
		my_door:close()
	end
end
Edit: use "any" on your alcove for calling script.
I'm the Gate I'm the Key.
Dawn of Lore
LGreymark
Posts: 4
Joined: Mon Apr 07, 2014 1:00 pm

Re: Scripting help

Post by LGreymark »

We're nearly there, that works perfectly so long as we don't introduce another item, for instance if I place a torch on the alcove after the chalice, then take the chalice off, the door remains open.

EDIT

Curiously after thinking about it, I discovered an odd but effective workaround. Simply by setting the alcove to always active, then adding another connector to the scripting entity. Finally I set one connector to 'activate' and one to 'deactivate', this coupled with the alcove ticking as if it were activated every time something is added and deactivated every time something is removed, it correctly located the foreign body of the torch and closes the gate.
User avatar
Isaac
Posts: 3192
Joined: Fri Mar 02, 2012 10:02 pm

Re: Scripting help

Post by Isaac »

LGreymark wrote:We're nearly there, that works perfectly so long as we don't introduce another item, for instance if I place a torch on the alcove after the chalice, then take the chalice off, the door remains open.
To restrict the alcove to only accept the Chalice is easy, (if that is what you want), by adding to the object definition:

Code: Select all

onInsertItem = function(alcove,item)
return item.name == "golden_chalice"
end

______________
*Clone the alcove, and use the clone; or that will affect all alcoves.
If you want other items allowed; (but only one at a time), then add this instead:

Code: Select all

onInsertItem = function(alcove,item)
return alcove:getItemCount() == 0
end
______________

For anything else, you just add to or replace the onInsertItem hook's function with your custom code, and it will run when the
player tries to add items to the alcove.
User avatar
Leki
Posts: 550
Joined: Wed Sep 12, 2012 3:49 pm

Re: Scripting help

Post by Leki »

Preventing drop into is not the best way, if there is an "general alcove" imho.

You no need to add activate /deactivate, just use one "any". Alcove must be set to always active. Check this
Example. If you add right item, door --> open, if there is more items in alcove, or wrong item, door --> close.
I'm the Gate I'm the Key.
Dawn of Lore
LGreymark
Posts: 4
Joined: Mon Apr 07, 2014 1:00 pm

Re: Scripting help

Post by LGreymark »

Thanks for the help, but, as this is working fine and I actually understand why and how it works the way it does I'll keep using it, no sense in delving into things over my head.
Post Reply