PDA

View Full Version : Question on over laying LUA



Jaxilon
September 21st, 2019, 20:08
I'm working on some updates for the GURPS ruleset and want to do some stuff when an item in the "inventory" window is changed for "equipped/carried/not carried".

I would like to hook the Event on the char_invlist.lua during the onCarriedChanged which seems logical to me.

I attempted to overlay this in my base.xml using:

<script name="CharInvList" file="campaign/scripts/char_invlist.lua" />
after copying the script to the campaign folder in GURPS.

On load of FG however I have problems:

Runtime Notice: Reloading ruleset
Script Error: [string "campaign/scripts/char_invlist.lua"]:25: attempt to call global 'getDatabaseNode' (a nil value)
Script Error: [string "campaign/scripts/char_invlist.lua"]:13: attempt to call global 'registerMenuItem' (a nil value)

Now, I could be doing something completely noob on this but somewhere in my reading I saw a note saying you can't always overlay certain codeblocks?? Thinking maybe that was it...

I then did my changes by over laying the code for "manager_char.lua" using:

<script name="CharManager" file="campaign/scripts/manager_char.lua" />

The only way I could see to hook the "equipped/carried/not carried" in this script is by putting my code in the "updateEncumbrance" code block.

It works!! But I don't like hooking it there as it is inefficient because every time ANY item is changed it goes through ALL the items to recalculate the Encumbrance.

So, my question is, can I over layer char_invlist.lua and/or is there a better way to capture this event so I can do my code?

Thanks in advance, I am quite new to LUA and moding in FG so I know there are holes in my understanding.

PS. Before anyone jumps me on it, I am running my changes through ronnke already, I just don't want to wear him out with my dumb questions and it seems unlikely I'm the first to run into this.)

Happy gaming!

Jaxilon
September 21st, 2019, 22:18
AHA! Thanks to Moon Wizard I finally had the epiphany that had eluded me. I will try to explain but feel free to correct anything I say inaccurately. I've only been playing with lua for less than a month.

I don't need any of the above! I can create a code block and include it in the base.xml and by adding an easy to connect Handler get what I need. Actually, I almost had this in a code block I had tossed aside because I had just missed the correct code by a hair.

So for those like myself who didn't get the memo:

You can create your own lua codeblock and add it to the base.xml file using:


<script name="WhatIWantToDo" file="campaign/scripts/whatever_you_want_to_do.lua" /> <-- use whatever directory is appropriate obviously.


Then in your whatever_you_want_to_do.lua code make sure you have in your onInit() a DB.addHandler(... <-- DB.addHandler is a part of what comes along with FG.

For example:


function onInit()

print("INITIALIZING WHAT I WANT TO DO");
DB.addHandler(DB.getPath("charsheet.*.inventorylist.*.carried"), "onUpdate", onCarriedChanged); <-- this is the event I wanted to hook to but it could be anything.
end

Then you can have your code for the onCarriedChanged:


function onCarriedChanged(nodeCarried)

-- whatever you want to do here
end



One more question though:

I figured I wanted to have a 'removeHandler' in the onClose() block but when I attempted to duplicate the addHandler and change it to removeHandler all I got was an error about it.
example:

in the onInit --> DB.addHandler(DB.getPath("charsheet.*.inventorylist.*.carried"), "onUpdate", onCarriedChanged); <-- this is the event I wanted to hook to but it could be anything.
in the onClose --> DB.removeHandler(DB.getPath("charsheet.*.inventorylist.*.carried"), "onUpdate", onCarriedChanged); <-- this is the event I wanted to hook to but it could be anything.


Here is the error on /reload obviously:

Runtime Notice: Reloading ruleset
Script Error: [string "campaign/scripts/char_item_equip.lua"]:12: attempt to index global 'DB' (a userdata value)


So, I don't have it now and it doesn't say boo but it seems like I missed out on some housekeeping.

edit: seems like after reading some past posts I probably didn't need to try to do this. So I'm going with that until told otherwise. :)

Hope this helps others as much as it has me. Thanks again Moon Wizard.

Jaxilon
September 21st, 2019, 23:10
Oh, and for those wondering what this is all about here's a brief video of what I'm doing:
https://youtu.be/ePQFhu1dugg