How do I "translate" this simple hasTorch script?

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
Eightball
Posts: 48
Joined: Thu Jan 09, 2014 8:21 am

How do I "translate" this simple hasTorch script?

Post 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
Last edited by Eightball on Fri Jun 10, 2016 10:46 am, edited 1 time in total.
User avatar
AndakRainor
Posts: 674
Joined: Thu Nov 20, 2014 5:18 pm

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

Post 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
User avatar
Eightball
Posts: 48
Joined: Thu Jan 09, 2014 8:21 am

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

Post 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"
Last edited by Eightball on Fri Jun 10, 2016 10:50 am, edited 1 time in total.
minmay
Posts: 2768
Joined: Mon Sep 23, 2013 2:24 am

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

Post 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.
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
Eightball
Posts: 48
Joined: Thu Jan 09, 2014 8:21 am

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

Post 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.
User avatar
Eightball
Posts: 48
Joined: Thu Jan 09, 2014 8:21 am

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

Post 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.
User avatar
Eleven Warrior
Posts: 736
Joined: Thu Apr 18, 2013 2:32 pm
Location: Australia

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

Post 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
Post Reply