Checking content in hands + Spell light is active [Solved]

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!
Post Reply
User avatar
Jouki
Posts: 127
Joined: Fri Oct 24, 2014 12:57 pm

Checking content in hands + Spell light is active [Solved]

Post by Jouki »

Hello, can you someone help me how can I get information if the party holds the torch in hand? I would like to create codnition if there's (or not) a light in room do something
Last edited by Jouki on Fri Dec 05, 2014 4:30 pm, edited 1 time in total.
User avatar
mahric
Posts: 192
Joined: Sun Nov 04, 2012 3:05 pm

Re: Checking content in hands

Post by mahric »

Jouki, I think the torchEquipped() script below should do the trick.

It checks all the champions with an item in their hand with the "TorchItemComponent", which should for all torches.
However, there might be other items with lightsources that don't have this component. This script does not check for those.

Code: Select all

function torchEquipped()
	local hasTorch = false
	for i = 1,4 do
		local c = party.party:getChampionByOrdinal(i)
		hasTorch = hasTorch or checkSlotForTorch(c, 1) or checkSlotForTorch(c, 2)
	end
	return hasTorch
end

function checkSlotForTorch(champion, slot)
	local torchInSlot = false
	local j = champion:getItem(slot)
	if j then
		torchInSlot = (j.go:getComponent("torchitem") ~= nil)
	end	
	return torchInSlot
end
Did you visit the Wine Merchant's Basement? And heard about the Awakening of Taarnab?
User avatar
Jouki
Posts: 127
Joined: Fri Oct 24, 2014 12:57 pm

Re: Checking content in hands

Post by Jouki »

mahric wrote:Jouki, I think the torchEquipped() script below should do the trick.

It checks all the champions with an item in their hand with the "TorchItemComponent", which should for all torches.
However, there might be other items with lightsources that don't have this component. This script does not check for those.

Code: Select all

function torchEquipped()
	local hasTorch = false
	for i = 1,4 do
		local c = party.party:getChampionByOrdinal(i)
		hasTorch = hasTorch or checkSlotForTorch(c, 1) or checkSlotForTorch(c, 2)
	end
	return hasTorch
end

function checkSlotForTorch(champion, slot)
	local torchInSlot = false
	local j = champion:getItem(slot)
	if j then
		torchInSlot = (j.go:getComponent("torchitem") ~= nil)
	end	
	return torchInSlot
end
this line sends error

local j = champion:getItem(slot)

"getItem a nil value", I have no idea why is it doing, it looks everything is right...
User avatar
Isaac
Posts: 3190
Joined: Fri Mar 02, 2012 10:02 pm

Re: Checking content in hands

Post by Isaac »

Jouki wrote:..."getItem a nil value", I have no idea why is it doing, it looks everything is right...
*I was wrong... I get a different error...

Ah... This happened when I called the wrong function.

Try this:
Temporarily add this to the script and call it with a button.

Code: Select all

function check()
	print(torchEquipped())
end
It should [when the button is pressed] print true or false ~whether anyone has a torch equipped. It's a neat script. 8-)
Grimfan
Posts: 369
Joined: Wed Jan 02, 2013 7:48 am

Re: Checking content in hands

Post by Grimfan »

You have probably linked both functions to the same floor trigger. The torchEquipped function calls on the checkSlotForTorch function (which doesn't need to be connected up). Because you've linked the second function it's running twice, but in the second instance it doesn't know what item in what slot it's supposed to be looking for (the torch called by the first function).
User avatar
Jouki
Posts: 127
Joined: Fri Oct 24, 2014 12:57 pm

Re: Checking content in hands

Post by Jouki »

Isaac wrote:
Jouki wrote:..."getItem a nil value", I have no idea why is it doing, it looks everything is right...
*I was wrong... I get a different error...

Ah... This happened when I called the wrong function.

Try this:
Temporarily add this to the script and call it with a button.

Code: Select all

function check()
	print(torchEquipped())
end
It should [when the button is pressed] print true or false ~whether anyone has a torch equipped. It's a neat script. 8-)

oh god, idk if I was calling wrong function, nevermind it works (printing true/false) :) btw could you write script checking if the spell "light" is actived, please? ^^
User avatar
Isaac
Posts: 3190
Joined: Fri Mar 02, 2012 10:02 pm

Re: Checking content in hands

Post by Isaac »

Jouki wrote: btw could you write script checking if the spell "light" is actived, please? ^^
I don't know if that's possible; it was not [really] possible in LoG1.
minmay
Posts: 2790
Joined: Mon Sep 23, 2013 2:24 am

Re: Checking content in hands

Post by minmay »

Code: Select all

-- Returns true if the Light spell is activated.
function lightSpellOn()
  -- no light source: 0.1, 0.13, 0.25
  -- torch: 1.2, 0.8, 0.6
  -- light spell: 0.56, 0.75, 1.2
  -- both: 1.76, 1.55, 1.8
  return party.torch:getColor()[3] > 1.19
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.
User avatar
Isaac
Posts: 3190
Joined: Fri Mar 02, 2012 10:02 pm

Re: Checking content in hands

Post by Isaac »

minmay wrote:

Code: Select all

-- Returns true if the Light spell is activated.
function lightSpellOn()
  -- no light source: 0.1, 0.13, 0.25
  -- torch: 1.2, 0.8, 0.6
  -- light spell: 0.56, 0.75, 1.2
  -- both: 1.76, 1.55, 1.8
  return party.torch:getColor()[3] > 1.19
end
This is new...
Worked out from the light components in the asset pack definitions? Very neat. 8-)
User avatar
Jouki
Posts: 127
Joined: Fri Oct 24, 2014 12:57 pm

Re: Checking content in hands

Post by Jouki »

minmay wrote:

Code: Select all

-- Returns true if the Light spell is activated.
function lightSpellOn()
  -- no light source: 0.1, 0.13, 0.25
  -- torch: 1.2, 0.8, 0.6
  -- light spell: 0.56, 0.75, 1.2
  -- both: 1.76, 1.55, 1.8
  return party.torch:getColor()[3] > 1.19
end
wow, thanks. It's much easier way to figure out.
Post Reply

Return to “Mod Creation”