Problem with 'Surface Contains' script Please Help

Ask for help about creating mods and scripts for Grimrock 2 or share your tips, scripts, tools and assets with other modders here. Warning: forum contains spoilers!
Mortis_of_Midian
Posts: 14
Joined: Wed Dec 10, 2014 6:07 pm

Problem with 'Surface Contains' script Please Help

Post by Mortis_of_Midian »

Please Help

As part of a puzzle I'm working on I have an Altar onto which the player places a spiked_club and the club is teleported away to an inaccessible room and an Ogre is spawned.
This is supposed to give the illusion that the Altar performs some kind of necromatic magic and resurrects the Ogre that the club belonged to.

Code: Select all

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

function ogreAltar()
   if surfaceContains(catacomb_altar_candle_01_1.surface, "spiked_club") then 
   hudPrint("Club Placed")
teleporter_24.controller:activate()
timer_2.timer:start()

   else
   hudPrint("That does nothing")
   end
end
I've used code from an alcove puzzle to perform this function - and it does work - but only the once.
The first time I place spiked_club it works as expected - but any time after that it doesn't matter what I place on the Altar it always activates. I don't understand why this is, do I need to 'reset' the code or the surface component some how? Or have I just stumbled upon a limitation of LoG2?
User avatar
Zo Kath Ra
Posts: 940
Joined: Sat Apr 21, 2012 9:57 am
Location: Germany

Re: Problem with 'Surface Contains' script Please Help

Post by Zo Kath Ra »

How do you teleport the club away?
Mortis_of_Midian
Posts: 14
Joined: Wed Dec 10, 2014 6:07 pm

Re: Problem with 'Surface Contains' script Please Help

Post by Mortis_of_Midian »

Zo Kath Ra wrote:How do you teleport the club away?
A teleporter (teleporter_24) is placed over the Altar - it's starts deactivated and is activated by the script ( "teleporter_24.controller:activate()" )

when timer_2 finishes it's cycle it then deactivates the teleporter ready for next time
User avatar
Zo Kath Ra
Posts: 940
Joined: Sat Apr 21, 2012 9:57 am
Location: Germany

Re: Problem with 'Surface Contains' script Please Help

Post by Zo Kath Ra »

The problem is this:
If there's an item on the altar, and you teleport the item away, then the item is still on the altar's surface (as far as the game is concerned).
The only way to remove an item from a surface (other than picking up the item) is to destroy the surface, which also destroys the item.

You can use this function instead:

Code: Select all

function ogreAltar(self, item)
	if (item.go.name == "spiked_club") then
		hudPrint("Club Placed")
		teleporter_24.controller:activate()
		timer_2.timer:start()
	else
		hudPrint("That does nothing")
	end
end
Connect it to the altar's onInsertItem connector, and it should work.
minmay
Posts: 2788
Joined: Mon Sep 23, 2013 2:24 am

Re: Problem with 'Surface Contains' script Please Help

Post by minmay »

There is no known way to remove an item from a surface using the scripting interface, except for outright destroying the ItemComponent. Even destroying the surface taints the item and will result in a crash on saving or attempting to pick up the item.
It would be very nice if someone found a way to remove an ItemComponent from a surface without destroying it, but currently I know that the following do NOT work:
- naively changing the parent object's position (teleporters, setPosition(), etc)
- adding the item to an inventory (player, monster, surface, socket, container, doesn't matter, none of them actually remove the item from the surface, even adding it to a monster's inventory and making the monster drop it)
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
Mortis_of_Midian
Posts: 14
Joined: Wed Dec 10, 2014 6:07 pm

Re: Problem with 'Surface Contains' script Please Help

Post by Mortis_of_Midian »

ok I'm not too bothered about where the club goes (I only used a telepoter as it seemed like most simple way at the time) so it might as well be destroyed - how would I go about that?

if the surface is destroyed to clear it, can I put the surface back? will it work a 2nd time?

I've seen 'shop' type setups which make one item disappear and replace it with another, but I'm not sure how they work

thanks for the replies :)
minmay
Posts: 2788
Joined: Mon Sep 23, 2013 2:24 am

Re: Problem with 'Surface Contains' script Please Help

Post by minmay »

Code: Select all

function ogreAltar(self, item)
   if (item.go.name == "spiked_club") then
      hudPrint("Club Placed")
      item.go:destroyDelayed()
   else
      hudPrint("That does nothing")
   end
end
Grimrock 1 dungeon
Grimrock 2 resources
I no longer answer scripting questions in private messages. Please ask in a forum topic or this Discord server.
Mortis_of_Midian
Posts: 14
Joined: Wed Dec 10, 2014 6:07 pm

Re: Problem with 'Surface Contains' script Please Help

Post by Mortis_of_Midian »

Here's a thought - is there a way to make teleporter do the 'detecting' rather than the altar?

I will also try the code posted above tomorrow, thanks guys
User avatar
Isaac
Posts: 3188
Joined: Fri Mar 02, 2012 10:02 pm

Re: Problem with 'Surface Contains' script Please Help

Post by Isaac »

Mortis_of_Midian wrote:ok I'm not too bothered about where the club goes ...
If it's just a visual prop (that they never touch before it's gone), then it doesn't actually need to be the functional item. You could make it part of the altar, and disable it when it needs to vanish.
Mortis_of_Midian
Posts: 14
Joined: Wed Dec 10, 2014 6:07 pm

Re: Problem with 'Surface Contains' script Please Help

Post by Mortis_of_Midian »

Isaac wrote: If it's just a visual prop (that they never touch before it's gone), then it doesn't actually need to be the functional item. You could make it part of the altar, and disable it when it needs to vanish.
Ah no, the characters need to be able to handle the club. I've got a puzzle where you need to trap 4 ogres in 4 cages. Of course if the player kills an ogre then the puzzle can't be completed so I wanted to be able to replace dead ogres with fresh ones and with something a bit more immersive than a button that spawns them on demand or a new ogre spawning upon the death of an old one. Also exchanging a club for an ogre via the altar naturally limits the number of ogres in game and allows for the additional element of searching for clubs hidden around the cages.
Post Reply