Skip to end of metadata
Go to start of metadata

Porting Enhanced Money Transfer mod to 3.50

I thought I would share how I ported my Enhanced Money Transfer mod over to the new system in 3.50 b1 I know most people haven't written mods that patch into the existing UI framework but it should help as a reference as how to do it in general. I won't cover the ui.xml as I think that's already fairly clear - just to reiterate that as things stand the game looks for a single ui.xml file in the mod's root folder. You can also now put your .lua files in the root folder and there's no need anymore to put them in a subst_xx.cat file or use a convoluted path in the xml file to locate them.

So, in the versions previous to 3.50 I was able to simply write a new version of the MoneyTransfer lua file and diff patch the ego_detailmonitor.xml file to replace Ego's version with my own. As of 3.50 b1 I can't do that so I now need to locate the existing (vanilla) menu and "hook" my functions into it. Here's how:

First I make a local empty table to hold a copy of the vanilla MoneyTransfer dialog:

local emt_menu = {}

I'm also going to make a table to store my collection of functions as this is how I like it (makes debug and cleanup a lot easier)

local Funcs = {}

Now, here's the important bit (with comments):

local function init()   -- This function runs at game startup to load menus

   -- iterate over the master "Menus" table to find the menu we want to plug in to
   -- Note that "Menus" is THE master table that holds a live copy of all XR's menus/dialogs

   for _, menu in ipairs(Menus) do     -- note that the underscore is simply a placeholder for an ignored variable 

      -- We're looking for the menu called "MoneyTransferMenu"

      if menu.name == "MoneyTransferMenu" then            

         -- We found our menu so let's first make a local copy of it that we can use, for example if we need to call any Helper functions
         -- or maybe access any passed parameters. In my functions below I can now access passed in parameters as e.g. emt_menu.param1

         emt_menu = menu

         -- Now we hook in our own functions to replace the originals

         menu.onShowMenu = Funcs.EMT_onShowMenu
         menu.onUpdate = Funcs.EMT_onUpdate
         menu.onSelectElement = Funcs.EMT_onSelectElement
         menu.onCloseElement = Funcs.EMT_onCloseElement
         menu.cleanup = Funcs.EMT_cleanup
         break      -- we found our menu so we can break out of the for loop as we don't need to carry on looking anymore
      end
   end
end

Job Done - well almost - we still need to define our own functions below, so (without all the boring details)

Funcs.EMT_cleanup = function()
   .........
end

Funcs.EMT_onShowMenu = function()
   ...........
end

-- Note here that I'm using the onUpdate function so I need to set an update interval - I access this via my local copy of the vanilla menu
emt_menu.updateInterval = 0.5
Funcs.EMT_onUpdate = function()
   ..............
end

Funcs.EMT_onSelectElement = function()
   ...........
end

Funcs.EMT_onCloseElement= function(dueToClose)
   .........
end

Finally, call the init() function and the mod is patched into the original menu framework

 init()

Remember that if you wish to call any of the functions in the member table (Funcs) locally i.e. inside the mod then use the : operator e.g.

 Funcs.LocalFunction = function()
   local foo = 5
   return foo
end

bar = Funcs:LocalFunction()
-- bar is assigned the value 5

Hope this helps,

Wysi (smile)

Disclaimer

This post was written by wysiwyg.

  • No labels