Page 1 of 1

How do I "translate" this simple hasTorch script?

Posted: Fri Jun 10, 2016 10:27 am
by Eightball
I don't understand why this basic script doesn't work anymore. I have had to change it from LOG1 and tried a few different things, but I'm grasping at straws. All I want to do is check if a Torch_Holder hasTorch() and if not addTorch(). But I get a "nil value" error on the first line.

Code: Select all

function lightup1()
	if torch_holder_apothecary:hasTorch() == false then
        torch_holder_apothecary:addTorch()
        end
end
If I have to "translate" it into LOG2, I haven't figured out how. I tried a getHasTorch() but have the same problem.

I think I may give up on remake my LOG1 mod if this kind of thing happens with nearly every script. Maybe I should just try a part 2 in LOG2 and finish part 1 in LOG1 alone. Does anyone still play LOG1 mods?

EDIT:
oh here's the original LOG1 script which worked using a teleporter as a "torchSensor," but teleporters apparently don't have the script "isActivated()" anymore.

Code: Select all

function lightup1()
  if torchSensor:isActivated() then
	spawn("torch_holder", party.level, 5, 13, 0, "torch_holder_8")
	torch_holder_8:addTorch()
	torchSensor:deactivate()
else
	return false
  end
end

Re: How do I "translate" this simple hasTorch script?

Posted: Fri Jun 10, 2016 10:46 am
by AndakRainor
Hi!

Look at the scripting reference here:
http://www.grimrock.net/modding/scripti ... rComponent

So if your torch holder id is torch_holder_apothecary as in your code above, then you can write;

Code: Select all

if not torch_holder_apothecary.torchholdercontroller:getHasTorch() then
  torch_holder_apothecary.torchholdercontroller:setHasTorch(true)
end

Re: How do I "translate" this simple hasTorch script?

Posted: Fri Jun 10, 2016 10:48 am
by Eightball
Oh I thought the torchholdercontroller part was to be the specific name of the torch_holder. I'll try that.

EDIT: yeah, that comes back nil too. "attempt to index field 'torchholdercontroller' a nil value"

Re: How do I "translate" this simple hasTorch script?

Posted: Fri Jun 10, 2016 10:49 am
by minmay
You should consult the scripting reference and the asset pack. It seems you are not aware of the component system. For example, a torch holder in Grimrock 2 has:
- a ModelComponent (the sconce model)
- a SocketComponent (the socket for the torch)
- a ClickableComponent (defines the region where clicking will try to insert the mouse item into the SocketComponent)
- a TorchHolderControllerComponent (automatically enables/disables the light/sound/particle when a torch is inserted/removed, and lets you choose in the editor whether to start the holder with a torch or not)
- a LightComponent (the light for when a torch is in the holder)
- a ParticleComponent (the flame particles for when a torch is in the holder)
- a SoundComponent (the sound for when a torch is in the holder)

You cannot just call component methods from the GameObject, you need to call them on the component they belong to.

Code: Select all

function lightup1()
  if not torch_holder_apothecary.socket:getItem() then
    torch_holder_apothecary.socket:addItem(spawn("torch").item)
  end
end
AndakRainor's solution will not work; that's not what TorchHolderControllerComponent does. hasTorch only affects what happens when the torch holder is initially spawned. This torch holder already exists, so calling setHasTorch will have no effect. getHasTorch() does not return whether the torch holder currently contains a torch, it returns whether the controller originally gave it a torch.

Re: How do I "translate" this simple hasTorch script?

Posted: Fri Jun 10, 2016 11:00 am
by Eightball
Thanks! That actually helps a lot. I'll have to get re-educated with regards to the components. I wasn't aware of how that cashed out.
I'm still getting no torch in the existing sconce, but at least the script doesn't crash with an error now. I'm going to try to go back to spawning a new torch_holder to begin with and see if I have any luck with that instead.

Re: How do I "translate" this simple hasTorch script?

Posted: Fri Jun 10, 2016 11:14 am
by Eightball
Oh I got it now. I had made a silly mistake with your script, minmay. Thanks. I think I'll have to wait till summer vacation starts to get into scripting more. I'm still a beginner.

Re: How do I "translate" this simple hasTorch script?

Posted: Mon Jun 13, 2016 1:16 am
by Eleven Warrior
I use this Script to stop the player from taking torches from torch holders it work for me.

torch_holder_1 add connection (onRemoveItem) to this script stopRemove1()

Code: Select all

--------------------------
--- Torch Function ---
--------------------------

function stopRemove1(sender)
   sender.go.socket:addItem(spawn("torch").item)
   setMouseItem(nil)
end