Trying to activate Clickable:onClick() via script object.

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
unconscious
Posts: 3
Joined: Mon May 07, 2018 12:41 am

Trying to activate Clickable:onClick() via script object.

Post by unconscious » Mon May 07, 2018 12:48 am

Hello, I''m just getting into grim2 modding. I'm pretty familiar with lua, but it's not my forte as a programmer. I've searched for 12 hours before I broke down and asked this question, so I'm sorry if it's been solved before.

I have the asset pack v2 and Jkos's amazing grim2 hook framework.
One thing, I cannot use defineObject with a fw_addHooks call despite following install instructions.
fw_addHooks just throws an attempt to call global (nil val error). That's not too important yet.

The real thing I'm trying to get working is to activate an onClick() hook via script.
I'm trying to activate a healing crystal without clicking it, so I have this function that is called by floor trigger:

Code: Select all

function actcrystal()
	hc.clickable:onClick()
	hudPrint("Your mind feels at ease...")
end
where hc is the healing_crystal id. I've tried all varieties of clickable, Clickable, without that table deref, etc... Please help.

Thank you, I'm very frustrated as this should be extremely simple, but lua has had a history of doing this to me. (My touchscreen gui in minecraft opencomputers should not have taken 12 hours xD) Please let me know if you require any more information, thank you.

Unconscious

User avatar
Isaac
Posts: 2949
Joined: Fri Mar 02, 2012 10:02 pm

Re: Trying to activate Clickable:onClick() via script object

Post by Isaac » Mon May 07, 2018 3:59 am

onClick is a hook. To use it, you should define a connector to the clickable component.

Code: Select all

hc.clickable:addConnector('onClick', self.go.id, 'actcrystal')
active = true  --to prevent message if clicked while crystal is inactive.

function actcrystal()
	if active then
		active = false
		hudPrint("Your mind feels at ease...")
		hudPrint("") hudPrint("") --message spacer
		delayedCall(self.go.id, hc.crystal:getCooldown(), "resetActive")
	end	
end

function resetActive()
	active = true
end
Note that in the game (as opposed to the editor), the Hud prints "Game Saved" when a crystal is clicked.

If you want the "Game Saved" message not to display, then use this script instead:

Code: Select all

hc.clickable:addConnector('onClick', self.go.id, 'catchClick')
active = true

function actcrystal()
		hudPrint("") hudPrint("") hudPrint("")
		hudPrint("") hudPrint("") hudPrint("")
		hudPrint("Your mind feels at ease...")
end

function resetActive()
	active = true
end

function catchClick()  
	if active then
		active = false
		delayedCall(self.go.id, .001, "actcrystal")
		delayedCall(self.go.id, hc.crystal:getCooldown(), "resetActive")
	end	
end

Last edited by Isaac on Mon May 07, 2018 4:23 am, edited 3 times in total.

unconscious
Posts: 3
Joined: Mon May 07, 2018 12:41 am

Re: Trying to activate Clickable:onClick() via script object

Post by unconscious » Mon May 07, 2018 4:15 am

Dude, thank you! That just told me a ton about how the engine works. I've been just looking through all the asset files to get a glimpse but wasn't making a connection.(ba dum tsh)

I look forward to becoming active in this community, I'm developing a nightmare designed to force the player to explore himself as a person. So it's story driven along with programming practice driven.

Thank you again Isaac! You propelled me up the learning curve.
Last edited by unconscious on Mon May 07, 2018 4:24 am, edited 2 times in total.

User avatar
Isaac
Posts: 2949
Joined: Fri Mar 02, 2012 10:02 pm

Re: Trying to activate Clickable:onClick() via script object

Post by Isaac » Mon May 07, 2018 4:20 am

Image

Here is a link to the most current (user updated) LoG2 scripting reference.

https://github.com/JKos/log2doc/wiki

unconscious
Posts: 3
Joined: Mon May 07, 2018 12:41 am

Re: Trying to activate Clickable:onClick() via script object

Post by unconscious » Mon May 07, 2018 4:24 am

Hate to be a buggy bumper, I already really appreciate your help! Do you happen to know anything about why fw_addHooks is throwing global nil inside defineObject?

User avatar
Isaac
Posts: 2949
Joined: Fri Mar 02, 2012 10:02 pm

Re: Trying to activate Clickable:onClick() via script object

Post by Isaac » Mon May 07, 2018 4:26 am

unconscious wrote:Hate to be a buggy bumper, I already really appreciate your help! Do you happen to know anything about why fw_addHooks is throwing global nil?
Offhand... no (I've never really used it myself).

The man to ask is minmay; he'll be around shortly. ;) (or Jkos of course... but he's not been around in years.)

https://sites.google.com/site/jkoslog2/
unconscious wrote:Dude, thank you! That just told me a ton about how the engine works.
About the engine hooks... There are two ways to use them. The one shown above, and to define them in the object. When defined in the object, many of the hooks can cancel the event that called them, by returning false.

So... returning false from the onPickUpItem() hook, means that the item cannot be picked up.

Code: Select all

defineObject{
	name = "party",
	baseObject = "party",
	components = {
		{
			class = "Party",
			onPickUpItem = function(self, item)
				if item.go.name == "boulder" then
						for x = 1,7 do hudPrint("") end
						hudPrint("It\'s too heavy to lift!")
						item.go:setWorldPositionY(.2)
						if not item.go.gravity then item.go:createComponent('Gravity'):setDestroySelf(true) end
						playSound('party_move_overloaded')
					return false
				end	
			end
		},
	},
}

Zo Kath Ra
Posts: 829
Joined: Sat Apr 21, 2012 9:57 am
Location: Germany

Re: Trying to activate Clickable:onClick() via script object

Post by Zo Kath Ra » Mon May 07, 2018 8:35 pm

unconscious wrote:I'm trying to activate a healing crystal without clicking it, so I have this function that is called by floor trigger:

Code: Select all

function actcrystal()
	hc.clickable:onClick()
	hudPrint("Your mind feels at ease...")
end
where hc is the healing_crystal id.
Isaac described how you can call a script when the player clicks on a healing crystal.
But your post sounds like you want the opposite:
A script that clicks on the healing crystal for the player.

Instead of (for example) a pressure plate that opens/closes/toggles a door, you want a pressure plate that clicks on a healing crystal.

Doors have open/close/toggle functions that you can call, but as far as I'm aware, healing crystals (or clickable components) do not have a click function.

But there is CrystalComponent:deactivate() which you can use to simulate what you what.
Connect a pressure plate to this function:

Code: Select all

function heal()
	if hc.crystal:isEnabled() then
		hc.crystal:deactivate()
		party.party:heal()
		hudPrint("Your mind feels at ease...")
	end
end
edit: this doesn't save the game, though :-(

Post Reply