Scripting Reference (work in progress)

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
Blichew
Posts: 157
Joined: Thu Sep 27, 2012 12:39 am

Re: Scripting Reference (work in progress)

Post by Blichew »

John, JKoss, can you provide one example of this ? I'm not sure i understand it correctly. Working part of script would be great to test and then reverse-engineer :)
NutJob
Posts: 426
Joined: Sun Oct 19, 2014 6:35 pm

Re: Scripting Reference (work in progress)

Post by NutJob »

Blichew wrote:John, JKoss, can you provide one example of this ? I'm not sure i understand it correctly. Working part of script would be great to test and then reverse-engineer :)
If you used connectors in your dungeon already (floor trigger closing a door, a lever executing a script, etc) you can open dungeon.lua and there will be a lot of examples of use there. Search/find "addConnector"
NutJob
Posts: 426
Joined: Sun Oct 19, 2014 6:35 pm

Re: Scripting Reference (work in progress)

Post by NutJob »

Grimfan wrote:I wouldn't be too sure of that NutJob. You won't get a response from me but I bet Prozail or JKos might have a crack at answering your query if they feel inclined. ;)
I was able to make my own custom event (onTeleport) and got it working, finally. It processes that force field flash effect to simulate the way is barred.

It's pretty easy if you follow these 18 steps:

1) Remove all th
User avatar
JohnWordsworth
Posts: 1397
Joined: Fri Sep 14, 2012 4:19 pm
Location: Devon, United Kingdom
Contact:

Re: Scripting Reference (work in progress)

Post by JohnWordsworth »

@Bilchew: Not on my windows PC at the moment, so I cannot test this to ensure it's 100% correct, but you should be able to do something like this within a script component to add a timer to that same game object and hook up the timer's activate method to a method in the script...

Code: Select all

self.go.createComponent("Timer");
self.go.timer:addConnector("onActivate", self.go.id, "timerTick");
self.go.timer:setTimerInterval(0.5);
self.go.timer:start();

function timerTick()
  print("TICK!");
end
Similarly, you can hook into the party component's hooks (all apart from the GUI ones I think) by doing the following...

Code: Select all

party.party:addConnector("onMove", self.go.id, "partyMove");

function partyMove()
  print("Party Moved")
end
There are a couple of restrictions with the connectors. There doesn't appear to be a way to remove them once you have added them, so if you do add them - you need to either make sure that you will destroy the thing emitting the messages before you destroy the thing receiving them (otherwise you will get loads of warnings in the console). As JKos stated, it's also not possible to provide any feedback using this method. Which makes sense I guess - as I presume the order of connectors being fired is not strictly-required to always be the same (??) so connectors are more like "observers" as opposed to "delegates" provided in the object definition.

Hope this helps!
My Grimrock Projects Page with links to the Grimrock Model Toolkit, GrimFBX, Atlas Toolkit, QuickBar, NoteBook and the Oriental Weapons Pack.
User avatar
Blichew
Posts: 157
Joined: Thu Sep 27, 2012 12:39 am

Re: Scripting Reference (work in progress)

Post by Blichew »

JohnWordsworth wrote:
Similarly, you can hook into the party component's hooks (all apart from the GUI ones I think) by doing the following...

Code: Select all

party.party:addConnector("onMove", self.go.id, "partyMove");

function partyMove()
  print("Party Moved")
end
There are a couple of restrictions with the connectors. There doesn't appear to be a way to remove them once you have added them, so if you do add them - you need to either make sure that you will destroy the thing emitting the messages before you destroy the thing receiving them (otherwise you will get loads of warnings in the console). As JKos stated, it's also not possible to provide any feedback using this method. Which makes sense I guess - as I presume the order of connectors being fired is not strictly-required to always be the same (??) so connectors are more like "observers" as opposed to "delegates" provided in the object definition.

Hope this helps!
So it's possible to spawn onUseItem for one specific (let's say) potion_of_bear that on use can do something different ?

This one would be too good to be true - I cannot add non-existent hook/connector ? Like add onUseItem to rock item which (probably) doesn't have any onUseItem effects defined ?
User avatar
JohnWordsworth
Posts: 1397
Joined: Fri Sep 14, 2012 4:19 pm
Location: Devon, United Kingdom
Contact:

Re: Scripting Reference (work in progress)

Post by JohnWordsworth »

I haven't tested it with that many hooks yet, but assuming they all work the same (apart from the GUI ones, which I might guess don't work as expected because maybe the connector system isn't really designed to be called every frame), then I think you can add a hook to any item that has a component of a given class (you could even add a component to that object dynamically).

So, you could take the generic "Water Flask" item, and when you create a new instance of it, you could add one "drink potion" hook from a list of functions you have written - to make potions that have random effects say. Equally, I imagine you could spawn a rock, and then add a special effect that kicks in when you throw it by adding a connector to the item's "ItemActionComponent.onAttack(self, champion, slot, chainIndex)" hook. This is all hypothetical, as I've not tried it an I'm still not on the right computer to do so! In both cases, the new effect you have added will only occur on that specific item (not all rocks).
My Grimrock Projects Page with links to the Grimrock Model Toolkit, GrimFBX, Atlas Toolkit, QuickBar, NoteBook and the Oriental Weapons Pack.
User avatar
Blichew
Posts: 157
Joined: Thu Sep 27, 2012 12:39 am

Re: Scripting Reference (work in progress)

Post by Blichew »

Well, I guess we cannot add new components via script. I've tried:

Code: Select all

rock_1:addComponent(usableitem)
and

Code: Select all

rock_1.usableItem:enable()
unless you mean overriding createObject (or cloneObject) from script_entity, not (as usual) from within mon_assets
User avatar
JohnWordsworth
Posts: 1397
Joined: Fri Sep 14, 2012 4:19 pm
Location: Devon, United Kingdom
Contact:

Re: Scripting Reference (work in progress)

Post by JohnWordsworth »

Check my first post on this page - where I create a timer component on the current script entity! It's "createComponent", which you must call on the game object. You also have to use a capital letter for the first character in the component name.
My Grimrock Projects Page with links to the Grimrock Model Toolkit, GrimFBX, Atlas Toolkit, QuickBar, NoteBook and the Oriental Weapons Pack.
User avatar
Blichew
Posts: 157
Joined: Thu Sep 27, 2012 12:39 am

Re: Scripting Reference (work in progress)

Post by Blichew »

And how did I not see this :oops: ? I was just reading posts to catch up faster, not the code... :) Thanks for that !

:EDIT:

I'm still doing something wrong:

Code: Select all

rock_1:createComponent("UsableItem")
rock_1.usableitem:addConnector("onUseItem",self.go.id,"useRock()")

function useRock()
	hudPrint("rock_1 just got used !!")
end
Clicking this item in hand triggers throw (which is completly normal) while clicking this in inventory gives this error:
Image
User avatar
JohnWordsworth
Posts: 1397
Joined: Fri Sep 14, 2012 4:19 pm
Location: Devon, United Kingdom
Contact:

Re: Scripting Reference (work in progress)

Post by JohnWordsworth »

No need for the braces in the last parameter passed to the addConnector method. Also, I'm not 100% sure what happens when you right click a "Usable" and a throwable item when it is in your hands... But I'll leave you to experiment with that!

Code: Select all

rock_1.usableitem:addConnector("onUseItem",self.go.id,"useRock")
Hope this helps! :)
My Grimrock Projects Page with links to the Grimrock Model Toolkit, GrimFBX, Atlas Toolkit, QuickBar, NoteBook and the Oriental Weapons Pack.
Post Reply