Log in

View Full Version : Scope and syntax help



cscase
October 19th, 2013, 19:58
Hey all,
Doing some work today and here's a problem I often struggle with. It seems like there's just some simple thing that I'm missing, and I'm hoping one of you can tell me what it is.

Suppose I have two... windowlists, for example, on the same sheet. How can I have one of them call a function for the other? That is, syntactically, how do I write that reference? Let me set up a simple example:


<windowlist name="ItemA">
...whatever is in the windowlist here, doesn't matter...
<script>
function doSomeStuff()
Debug.chat("doing some stuff");
end
</script>
</windowlist>


<windowlist name="ItemB">
...bla bla...
<script>
function onInit()
RIGHT HERE
end
</script>
</windowlist>

In this example, say in the second part, I want to call the doSomeStuff() function in ItemA. What's the correct syntax to use to do that? This seems like it's probably a dumb question, like it's something really minor that I'm missing. If I say "ItemA.doSomeStuff()" I get an error, because ItemA is not a global. I understand what that error means and I understand the problem is one of scope, because ItemA is not globally visible. I just don't know the proper way to specifically reference ItemA by its location. I have tried using a relative reference, by saying ".ItemA.doSomeStuff()", but that seems to be invalid here. Maybe that style of reference is only valid for db nodes? Can somebody hit me over the head with some illumination?

Zeus
October 19th, 2013, 20:39
Its been a while since I did any coding for FGII but I believe the following should work.



window.ItemA.doSomeStuff();


where window is a reference from a window control to its parent window, ItemA. is the reference to the control we want to access and finally doSomeStuff() is the method we are calling.

cscase
October 19th, 2013, 23:04
Ah, okay! That is convenient and seems to work great for referencing another control in the same window - siblings, basically.

How would you do it if you want to reference a child? Say, you are referencing one of these methods from the character sheet that they are both on? Does this question make sense? I'm just trying to figure out how to reference items that are in different parts of a sheet relative to the one I am coming from. Are there different ways of going about it, depending on where the controls are located relative to one another?

Thanks Zeuss!

edit:
I think I just figured it out. It seems that in the case of a direct child reference, you can just say "ItemA.doSomeStuff" without any further referencing. I'm not sure why I thought it was more complicated than this.

How about if you are referencing a field in a windowlist? That seems to be a little more tricky.

edit again: I think I mostly figured it out. Thanks all!

Zeus
October 20th, 2013, 09:20
I would check out the Library and Ruleset Modification Guide, the Windows and Scripting sections contain some useful information for how to navigate through the window controls or through the database.