Jump to content

calling functions specified in xml


Recommended Posts

ok, I'm not sure if this is even possible, just asking the question now so that I have the answer for when I get home from work later...

I want to have an xml file, where the elements in the xml file have an attribute, and the value of that attribute is the name of a function in an accompanying .lua file. E.g.:

While I'm reading out of the xml file, I would want to create the button and attach an OnClientGUIClick eventhandler and specify the function to be called as the value of OnClick, from the xml file.

I'm assuming there would be a problem here because addEventHandler() expects a function, rather than the string name? I think in javascript and some other languages you have the concept of eval() where it does some magic and allows you to do something like this:

addEventHandler( "OnClientGUIClick", button, eval(onclickvalue))

Does anyone know of a way to do something similar in LUA?

Link to comment

Well, I don't think that's too difficult... Might be quite easy. :/

Make a loop that reads all of those subnodes, then for each subnode you can let it read out and use bindKey(getRootElement(),xmlReadKey,"down",xmlReadFunction)... Or something like that.

Link to comment
i just asked around in the scripting channel and here's how it works:
_G["functionname"](arguments) 
--e.g. 
function test () 
outputChatBox ("blubb") 
end 
  
addEventHandler ("onClientLick", getLocalPlayer(), _G["test"]) 

I expect _G etc would hold the function... Or.... Something... And that you meant "onClientClick" instead of "onClientCLick". :P

Link to comment

I would recommend using a table, as you can store functions within tables.

For your example you should be able to do something similar to the following:

  
table = { 
     button = {label="a button",  onClick = function() outputChatBox("OMGZ BUTTON CLICKAGENESS!") end }  
} 
  

then table.button.onClick would be your function that you should be able to stick in the eventhandler... not 100% on that one tho - you would need to test it out. (i can't as i'm not at home).

Link to comment

I've had some success with _G, but not quite there yet.

Interestingly, when I added an event handler I can pass _G[sFunctionName] as the function, where sFunctionName is the string name (minus the parentheses, e.g. "myFunction", rather than "myFunction()") of the function I want to call, which itself is in a different .lua file, and that works fine.

As a very simple test example, in javascript you can do this:

function doTest() { 
    alert('Hello world') 
} 
  
eval("doTest()") 

but in LUA, I would have hoped that this would do something similar:

function doTest()  
    outputChatBox("Hello world") 
end 
  
-- one of these should work? 
--G["doTest()"] 
--or without the brackets 
--_G["doTest"] 
  

Can anyone see something obviously wrong with that to make it work, or is it just not possible? yes, I know I've commented out the actual line that calls the function, before anyone suggests that's why it's not working :)

I've also not worked out if it's possible to send arguments through to that function at the same time, e.g. _G[sFunctionName .. "(1000)"] for example, as I couldn't get that to work in my working event handler version, but that's not important.

A little more specifically, what I'm trying to achieve right now is do something like this:

function SomeFunctionName()  
     if (1==1) then -- would be replaced by some other conditional statements to check a scenario before proceeding. 
          return true 
     else 
          return false 
     end 
end 
  
if (_G["SomeFunctionName"]) then 
    outputChatBox("it's true!") 
else 
    outputChatBox("it's false!") 
end  

Willy: I've not tried your recommendation of tables yet - but that would appear to be sending the entire function in, rather than just a function name, which would probably mean storing the entire function in the xml file? I don't really want to do that!

Link to comment

Yes, robhol.

-- lets say we have this function: 
function funcName() 
    outputChatBox( "Hello world" ) 
end 

_G[ "funcName" ] - returns the function type variable, they are used the most for attaching function to event, like:

addEventHandler( "onPlayerJoin", getRootElement( ), _G[ "funcName" ] ) 
-- 3rd param in addEventHandler must be a function, so this returns the function 

_G[ "funcName" ]( ) - calls the function

addEventHandler( "onResourceStart", getResourceRootElement( getThisResource( ) ), 
    function( ) 
        _G[ "funcName" ]( ) -- now you call the function, so it will output "Hello world" in your chatbox 
    end 
) 

You can pass arguments too:

_G[ "funcName" ]( "hello world", 1, false ) or whatever you like.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...