Get an Xml without using states – ShiVa Engine

Get an Xml without using states

States suffice when you only have one xml to get, but when you want to load more than one file, or when you want to use states for another usage, it becomes difficult to use them for the xml.
With handlers, you only have to create a handler and an AI variable per xml to get.

Setup

Firstly, create a variable in your AIModel, named myXml1 in my case, type xml. Then created a handler (onGetMyXml1 for instance).
The handler will be called once when you will decide to get the xml file, and then, the handler will (down)load it and call itself until the xml is not received.

Test the status

At the beginning of the script, get the status of the xml variable:

local status = xml.getReceiveStatus ( this.myXml1 ( ) )

If the status is 1, it means the xml has been received and you can parse it.

if ( status == 1 )
then
     local root = xml.getRootElement ( this.myXml1 ( ) )
     --To do
else

I have written the first step of the parse process (get the root element), you now have replace the “–To do” by your own parse process, according to your xml structure.
In the other case, the xml hasn’t been received yet, and as I said previously, the handler will call itself while receiving the xml, so in the “else”, you have to put:

this.postEvent ( 0, "onGetMyXml1" )

Using the postEvent function with 0 as time parameter mean that the event will be executed at the next frame.
In that case, the status can be < 0 or 0<=status<1. If the status is lesser than 0, it means that the xml hasn’t been started or the receive process has failed, so you have to start the receive process:

if ( status < 0 )
then
   local sUrl = “http://www.myWebsite.com/myXml.xml”
   xml.receive ( this.myXml1 ( ), sUrl )
end

Of course, you have to replace the string sURL by your own URL.

Conclusion

Congratulations, you have finished this tutorial. Note there is no dedicated script when the status is >= 0 and < 1, because you just have to wait until the xml receive process ends, so the script will do nothing except calling the handler again at next frame.
If you have another xml, you just have to create a new xml variable, copy/paste your handler and change the variable name and the handler name in the postEvent function.
Don’t forget, you have to call each handler only one time on a onInit for example or when you want to receive the xml file.
You can see the fully code there:

--------------------------------------------------------------------------------
function myGame.onGetMyXml1 (  )
--------------------------------------------------------------------------------
local status = xml.getReceiveStatus ( this.myXml1 ( ) )
if ( status == 1 )
then
  local root = xml.getRootElement ( this.myXml1 ( ) )
   --To do
else
      if ( status < 0 )
      then
           local sUrl = "http://www.myWebsite.com/myXml.xml"
           xml.receive ( this.myXml1 ( ), sUrl )
      end
      this.postEvent ( 0, "onGetMyXml1" )
end
--------------------------------------------------------------------------------
end
--------------------------------------------------------------------------------

  • slackBanner