Function Overloading – ShiVa Engine

Function Overloading

BĂ©renger Dalle-Cort: “Did you ever wonder how to change the ShiVa API functions to make them fit your needs better? Me too! Read on to figure out how to overload ShiVa Lua functions.”

xml.send with automatic logging

For this tutorial, we want to adapt the function XML.send to include automatic logging. If we wanted to do that with ShiVa’s on-board functions, it would look like this:

xml.send ( hXML, sURI )
log.message ( "XML sending to " .. sURI .. " has started." )

You will have to repeat the log call every time when XML.send is used. This can bloat your code quite a bit. The solution is to copy the runtime API function into a temporary function, and then call the second function to replace the one in the API:

-- empty function for storing the API function
------------------------------------------------------
function MyAIM.original_xml_send ( hXML, sURI )
------------------------------------------------------
------------------------------------------------------
end
------------------------------------------------------
-- function calling the original function with the additional part:
------------------------------------------------------
function MyAIM.override_xml_send ( hXML, sURI )
------------------------------------------------------
-- original call
local bOK = MyAIM.original_xml_send ( hXML, sURI )
-- new addition: log call
log.message ( "XML sending to " .. sURI .. " has started." )
return bOK
------------------------------------------------------
end
------------------------------------------------------

To overload the original function with your enhanced version, call the following code in onInit of an AIModel:

MyAIM.original_xml_send = xml.send
xml.send = MyAIM.override_xml_send

This will give you a compilation error, because it works in Lua only and is not compatible with C++ exports. As long as you are not planning on converting your code into C++, this will work fine though.




Need more answers?

  • slackBanner