Mouse Coordinates vs. Graphics Coordinates.

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!
User avatar
Isaac
Posts: 3185
Joined: Fri Mar 02, 2012 10:02 pm

Re: Mouse Coordinates vs. Graphics Coordinates.

Post by Isaac »

Equip a left and right hand melee weapon, and alternate some attacks; (unarmed attacks don't trigger the hook).

Code: Select all

--object.lua
defineObject{ 
	name = "party",
	baseObject = "party", --copy object
	components = {
		{	--component changes must define the entire component, not just the change
			class = "Party",
			onAttack = function (self, champion, action, slot)
				local side = iff(slot==1,'left','right') --iff is an LoG2 engine function iff(condition, true, false)
				hudPrint("")hudPrint("")hudPrint("")hudPrint("")hudPrint("") --clear hud
				hudPrint("The "..side..'-handed '..action:getName().." action")
				hudPrint("has been "..iff(side=="left", 'canceled!', 'allowed!'))
				if side == "left" then
					print("returned false")
					return false
				end		
			end
		},
	}		
}
User avatar
.rhavin
Posts: 25
Joined: Fri Oct 13, 2017 1:10 pm
Location: Berlin
Contact:

Re: Mouse Coordinates vs. Graphics Coordinates.

Post by .rhavin »

Isaac wrote:Equip a left and right hand melee weapon, and alternate some attacks; (unarmed attacks don't trigger the hook).

Code: Select all

--object.lua
defineObject{ 
	name = "party",
	baseObject = "party", --copy object
	components = {
		{	--component changes must define the entire component, not just the change
			class = "Party",
			onAttack = function (self, champion, action, slot)
				-- //…//
			end
		},
	}		
}
Ok, but wouldnt that conflict with other peoples skripts/modules that also need to react every time the party attacks? Lets say my special item will react on every attack the champion holding will commence. By overwriting party.party-component that will give my skript exclusive control over party.party and it will breake every one elses code that also redefines it. Unless really needed, i'd rather like to add functionality, not replace it. I thought by using addConnector, this is exactly what will happen, but i didnt test it.
User avatar
Isaac
Posts: 3185
Joined: Fri Mar 02, 2012 10:02 pm

Re: Mouse Coordinates vs. Graphics Coordinates.

Post by Isaac »

Both methods work. The normal method (style) is to define the hook in the object; and it commonly has the option to cancel the event. The connector method cannot cancel the event, but it can be added or removed at runtime.

The example isn't meant to consider anything else... it simply demonstrates the 'return false' behavior; its ability to cancel the outcome (in hooks that support it). This would work with death too, or even accepting items onto a shelf (or not). The onDie hook for monsters and PCs alike, could evaluate some condition to cancel the pending death. This can't be done with connectors. In the case of altars, and alcove surfaces (for instance), by returning false, the surface.onAcceptItem hook can refuse to accept the item.

In some cases one might use this (returning false) to cancel the default behavior in order to implement a custom one—that succeeds; and has access to the function arguments of the hook.

**The actual hook can return the result of function calls from within; where any one of them could effect a false return, to cancel the outcome. This certainly means that you can define the hook, and have it call a script_entity, and return true or false from that; it doesn't have to be a hard fail.

***Also btw: onDrawGui (specifically) needs to be defined in the party object (even just an empty function...) in order to actually get called—even by connectors, otherwise nothing happens; the hook doesn't get called.
Post Reply