Ask a simple question, get a simple answer

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!
bongobeat
Posts: 1076
Joined: Thu May 16, 2013 5:58 pm
Location: France

Re: Ask a simple question, get a simple answer

Post by bongobeat »

thanks!
My asset pack: viewtopic.php?f=22&t=9320

Log1 mod : Toorum Manor: viewtopic.php?f=14&t=5505
User avatar
Isaac
Posts: 3188
Joined: Fri Mar 02, 2012 10:02 pm

Re: Ask a simple question, get a simple answer

Post by Isaac »

Is there any context that would allow drawing over top of a playing video in LoG2?
User avatar
Mysterious
Posts: 226
Joined: Wed Nov 06, 2013 8:31 am

Re: Ask a simple question, get a simple answer

Post by Mysterious »

RE: Lock Problem.

Hi all. The below code works fine but when the correct key is used in the lock, the key falls to the ground. I want the key to be destroy not to be reused.

The idea is that the player has to use (lvl1thom_door_key_1) to open the lock which then opens the door etc.... Any help as to where I'm going wrong here Thank You.

PS: When the key is used by the way the Lock Socket is connected to a script which allows me to do things eg Open door maybe bad or good.

Code: Select all

-----------
--- Key ---
-----------
defineObject{
	name = "lvl1thom_door_key_1",
	baseObject = "base_item",
	tags = { "my_quest_keys" },
	components = {
		{
			class = "Model",
			model = "assets/models/items/key_iron.fbx",
		},
		{
			class = "Item",
			uiName = "Thomadars Key",
			gfxIndex = 22,
			weight = 0.2,
			traits = { "key" },
		},
		{
			class = "Particle",
			particleSystem = "glitter_gold",
		},
	},
}
------------------
--- Key Lock ---
------------------
defineObject{
	name = "thom_base_lock_1",
	components = {
		{
			class = "Model",
			model = "assets/models/env/wall_lock_round.fbx",
			offset = vec(0, 1.375, 0),
			staticShadow = true,
		},
		{
			class = "Clickable",
			offset = vec(0, 1.38, -0.16),
			size = vec(0.1, 0.1, 0.15),
			debugDraw = true,
		},
		{
			class = "Socket",
			offset = vec(0, 0, 0), --(-0.42, 1.18, 0),--
			--offset = vec(0, 0, 0),
			rotation = vec(0, 0, -3),
			onAcceptItem = function(self, item)
			if item.go.name ~= "lvl1thom_door_key_1" then
			hudPrint("You will need Thomadar's key to open the door.")
				return false
				else
				playSound("key_lock")
				return true
				end
			end,
		},
	},
	placement = "wall",
	editorIcon = 92,
	tags = { "my_quest_keys" },
}
User avatar
zimberzimber
Posts: 432
Joined: Fri Feb 08, 2013 8:06 pm

Re: Ask a simple question, get a simple answer

Post by zimberzimber »

You're using a socket component instead of a lock component.
Socket component doesn't destroy the item it accepts, and keeps it on display wherever it is defined to.
Lock component accepts only the item it is defined to accepts without any additional checkers, and automatically destroys it upon accepting it.
Try looking at some lock definitions inside the asset pack to get a better idea.

Also, instead of defining a new object, you could use one of the already present locks and attach it to the door.
My asset pack [v1.10]
Features a bit of everything! :D
minmay
Posts: 2789
Joined: Mon Sep 23, 2013 2:24 am

Re: Ask a simple question, get a simple answer

Post by minmay »

zimberzimber wrote:Lock component accepts only the item it is defined to accepts without any additional checkers, and automatically destroys it upon accepting it.
LockComponent doesn't actually destroy the item, it just removes the mouse item. The item still exists afterwards, but since it is not on a map you can only reach it by having a preexisting reference to it. For example, this is a lock that immediately puts the key item back into the mouse item slot:

Code: Select all

-- The mirror "lock" should not consume the item used to open it; in Dungeon
-- Master, using the Mirror of Dawn on it doesn't destroy the mirror. We do
-- this by simply setting the mouse item back to the key after it is used. The
-- ItemComponent reference here is removed before the end of the frame so it
-- cannot cause a serialization error.
defineObject{
	name = "dm_lock_mirror_slot",
	baseObject = "lock",
	components = {
		{
			class = "Model",
			model = "mod_assets/dmcsb_pack/models/env/dm_mirror_slot.fbx",
			offset = vec(0, 1.74, 0),
			staticShadow = true,
		},
		{
			class = "Clickable",
			offset = vec(0, 1.74, 0),
			size = vec(0.5, 0.4, 0.4),
			onClick = function(self)
				if self.go.lock:isEnabled() and getMouseItem() and getMouseItem().go.name == self.go.lock:getOpenedBy() then
					GameMode.dm_tempLockItem = getMouseItem()
				end
			end,
		},
		{
			class = "Lock",
			sound = "key_lock",
			openedBy = "dm_mirror_of_dawn",
			onActivate = function(self)
				setMouseItem(GameMode.dm_tempLockItem)
				GameMode.dm_tempLockItem = nil
			end,
		},
	},
	tags = {"dm","dm_user"},
}
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.
User avatar
zimberzimber
Posts: 432
Joined: Fri Feb 08, 2013 8:06 pm

Re: Ask a simple question, get a simple answer

Post by zimberzimber »

I see... That reminds me.
Is there a safe way of getting rid of an item for good?
Currently I'm using the 'add-it-to-a-surface-and-remove-the-surface-component' method and it works, but I really don't like it.
I need it because I have to return the item from a surface, and since there is no removeItem function, I had to settle for this.
My asset pack [v1.10]
Features a bit of everything! :D
User avatar
AndakRainor
Posts: 674
Joined: Thu Nov 20, 2014 5:18 pm

Re: Ask a simple question, get a simple answer

Post by AndakRainor »

You can use GameObject:destroy() on an item added to a SurfaceComponent. You can check it; after this command, SurfaceComponent:count() will return the correct updated value. The real problem with SurfaceComponent is when you want to remove an item from it but not destroy it, that is where the scripting interface is lacking a function.

In my mod I use this in a script entity named "utils":

Code: Select all

self.go:createComponent("Surface")
self.go.surface:setSize(vec(1.5, 1.5))

function destroyItem(item)
  if not item then return end
  if not item.go.map then self.go.surface:addItem(item) end
  item.go:destroy()
end
User avatar
zimberzimber
Posts: 432
Joined: Fri Feb 08, 2013 8:06 pm

Re: Ask a simple question, get a simple answer

Post by zimberzimber »

Yup, my problem is unlikning the item from the surface instead of deleting it.
Maybe someone has a better solution in mind for this instead of removing and re-adding the surface component?
Here is the script I'm using:

Code: Select all

for v,i in enchanterAltar.surface:contents() do
	if i and i.go.item then
		for c = 1,4 do
			local champion = party.party:getChampion(c)
			if champion then 
				for invSlot = 13, 32 do
					if champion:getItem(invSlot) == nil then
						champion:insertItem(invSlot, i)
						mi.data.gfxIndex = 0.5
						mi.data.size = {0, 0}
						enchanterAltar:removeComponent("surface")
						enchanterAltar:createComponent("Surface")
						enchanterAltar.surface:setSize(vec(1,1))
						return
					end
				end
			end
		end
		hudPrint("Inventory is full")
		playSound("dispel_launch")
		return
	end
end
My asset pack [v1.10]
Features a bit of everything! :D
User avatar
Isaac
Posts: 3188
Joined: Fri Mar 02, 2012 10:02 pm

Re: Ask a simple question, get a simple answer

Post by Isaac »

Minmay has a script [somewhere] ~IIRC, that adds the item to an invisible monster's inventory, then kills it; and so it drops on the map.
User avatar
zimberzimber
Posts: 432
Joined: Fri Feb 08, 2013 8:06 pm

Re: Ask a simple question, get a simple answer

Post by zimberzimber »

I tried that, but it didn't unlink the item from the surface.
My asset pack [v1.10]
Features a bit of everything! :D
Post Reply