PDA

View Full Version : What does self.update mean in number_crosslink.lua?



cscase
March 29th, 2014, 18:33
Hey all,
I am looking at number_crosslink.lua from CoreRPG because I'd like to modify it for a particular purpose, but there's something that I see a lot in there and am not quite understanding. What does this expression mean?
self.update


Is it a function call? If so, where is this function defined? I'm not seeing an update() function/method defined anywhere. Is it something like notifyUpdate()? Here's one of the functions that uses this, to give some context:



function onLinkUpdated()
if sLink and not bLocked then
bLocked = true;


setValue(DB.getValue(sLink, 0));

if self.update then
self.update();
end


bLocked = false;
end
end

I apologize in advance if I'm overlooking something obvious and this is a dumb question. I thought it might be a lua thing and googled, but had no luck finding anything about it. I'm just trying to understand what is going on in this code so that I can get it to do what I want.

Thanks!

Bidmaron
March 29th, 2014, 19:39
OK, I'll probably get this only partially right, but I'll give it a shot.
Lua scripts frequently use the architecture herein, and not just for update(). To improve code reusability, where you don't know if the environment your code is in has a particular functionality or not, you encapsulate a call:

if self.update then
self.update();
end
The if is a lua check to see if the function has been defined (you will have to search through all the nested templates to find the function if it exists). If it has, then it calls the actual routine (the parentheses cause function invocation).

Usually, the update routines get invoked in order to cascade changes made in one window or window list to related windows or window lists (e.g. if you change the name of something in a list and the window with the actual item is open, in the ideal world, you'd want that change to be reflected there).

Bidmaron
March 29th, 2014, 19:54
The self part of the call means to use any code that is defined on top of the script where it occurs (whereas super means to use anything defined earlier in the inheritance chain).

So, in the specific case you site, if you look where number_crosslinked.lua is used, you will see that it is the script for two templates. The first is number_ct_crosslink in the file template_ct.xml. This template is itself used in the ct_host.xml file in a combat tracker field called number_ct_crosslink. This field has the following script snippet:

<script>
function update()
window.windowlist.applySort();
end
</script>
So, what's going on here is that when the field using number_ct_crosslink gets its value changed, the update routine above is invoked, causing the sort order to change in the list to which the numeric field belongs.
The other template using the code you list is number_ct. There is no field using this number_ct template that really does anything interesting with update.

Attached is a 6341 I put together for my sanity to help wade through all the templates.

Nickademus
March 29th, 2014, 23:16
What does this expression mean? self.update Is it a function call? If so, where is this function defined? I'm not seeing an update() function/method defined anywhere. Is it something like notifyUpdate()?

In lua, functions are a variable type. So when referencing the function you use object.function(parameters). When checking for the existence of a function, you can check for the presence or absence of a nil value by simply referring to object.function. If the function does not exist on the object, it will return nil. If the function does exist it will simply return true. This is a good way to check an object for a function before calling it for good validation.

The function should be defined in the script you are seeing self.* since self refers to the current acting object (which is the script). Might be another script object that this one inherited from depending on how the hierarchy works in lua. Haven't dug that deep into it yet.

Bidmaron
March 29th, 2014, 23:23
Nick, in this case, at least for CoreRPG, it isn't there except for the one case I cited. I haven't looked in 3.5. The update mechanism is used like a big dog in the text fields, but not so much for the numbers.

Nickademus
March 29th, 2014, 23:28
I see a lot of updates in script blocks in the xml rather than in the lua files.

Bidmaron
March 30th, 2014, 01:22
But not for the template he listed in post #1. I could only find two for the templates using the lua file he mentioned (and they were both in the xml files).

Moon Wizard
March 30th, 2014, 05:39
When accessing a function or variable, Lua will start at the current object layer, then drill down to the next inherited object table, until it finds a reference or runs out of inherited object layers to check. This is actually a usage of Lua meta tables internal to FG client.

Since templates create nesting of script objects within FG, "self" can be used to point to the topmost layer of the nesting hierarchy. So, the topmost definition of the function or variable will be accessed when using "self".

If you didn't use "self", then you couldn't access functions or variables defined at higher levels of inheritance (ie templates and controls that use templates).

Hopefully that made sense. I can try to give examples if not, but on my phone now.

Cheers,
JPG

cscase
March 30th, 2014, 22:39
Thanks very much, Bidmaron, Nickademus, Moon Wizard! I'd be lying if I said this is all perfectly clear to me, but thanks to your explanation I think my understanding has passed the 80% mark :P Getting there.

I am working with the party sheet code, borrowed from the 4E ruleset, and some of the templates for that use this code - but as far as I can see none have an update, and my xml didn't include an update either, so I wondered if something was going on that I didn't grasp. This helps a lot, and thanks for your reference document, Bidmaron. This looks like it'll be helpful.

Thanks again!
Scott

Bidmaron
March 31st, 2014, 04:00
Happy to help, but I'm really very much in the learning phase myself.