Taking back custom data from ui

Everything about mods can be found here.

Moderator: Morax

Taking back custom data from ui

Postby Franck » 15 Jan 2016, 21:40

Hi all,

I'm still hard working on my mod, and the data connection between simside (unit.lua) and my module works fine thanks to Myxir's help ;) .

Now, i've got a custom database that the user modify from the ui (in my case the economy.lua by clicking on customs icons).
I know that i need to use callback functions to send data back to my lua module and to the sim side as it said in Myxir's page http://wiki.faforever.com/index.php?tit ... Sim_and_UI.

I'm not particulary familiar with callback function. I know that callback functions are function given by parameters in another function. I saw some of them in the scenarioFramework.lua file but i don't understand their practical use. Do i need to do something in the SimCallbacks.lua file too ?

Can somebody provide the most simple example of code for sending back a custom data content from ui to a mod module and simside ?

Thanks a lot for your help. It's not easy to write something i don't understand fully. I tried several hours without success.
Franck
Crusader
 
Posts: 28
Joined: 24 Dec 2015, 16:16
Has liked: 1 time
Been liked: 1 time
FAF User Name: Franck

Re: Taking back custom data from ui

Postby Franck » 16 Jan 2016, 23:48

Everything is working now thanks to my brother's help !

Here are the steps for tutorial purpose. A How to modify sim and module data from ui.


My goal was to spend skillpoints and increment the armor skill each time i click on ArmorPointsIcon in the ui (in this case economy.lua). My Data are saved in my module TransferDatas.lua.

Economy.lua code [Ui]

Code: Select all
local TD = import('/Mods/Mytestmod/Modules/TransferDatas.lua')


Code: Select all
ArmorPointsIcon.HandleEvent = function(self, event)
         if event.Type == 'ButtonPress' then
            local unit = GetSelectedUnits()
            if unit then
               local _id = unit[1]:GetEntityId()
               local Skpoints = TD.GetProperty(_id,"SkillPoints")-1
               if Skpoints >= 0 then
                  SimCallback( {    Func="OnApplyPoints",
                              Args={ id = _id}
                            }
                           )
               end
            end
         end
      end


SimCallbacks.lua code reference to my module TransferDatas.lua. This is the Bridge that will make all work

Code: Select all
Callbacks.OnApplyPoints = import('/Mods/Mytestmod/Modules/TransferDatas.lua').OnApplyPoints


My module TransferDatas.lua code :

Code: Select all
function OnApplyPoints(Unit)
   -- LOG(repr(Unit))
   local id = Unit.id
   IncProperty(id,"Armor",1)
   IncProperty(id,"SkillPoints",-1)   
end


GetProperty and IncProperty are custom function in my module to Get and Set my custom data. Don't forget to sync theses data to use them in simside.

You can notice that it seems simpler to do not modify my data in the ui (economy.lua) but from the ui in my module TransferDatas.lua.

Hope this may help :)
Franck
Crusader
 
Posts: 28
Joined: 24 Dec 2015, 16:16
Has liked: 1 time
Been liked: 1 time
FAF User Name: Franck

Re: Taking back custom data from ui

Postby nine2 » 17 Jan 2016, 07:38

cool
nine2
Councillor - Promotion
 
Posts: 2416
Joined: 16 Apr 2013, 10:10
Has liked: 285 times
Been liked: 515 times
FAF User Name: Anihilnine


Return to Mods & Tools

Who is online

Users browsing this forum: No registered users and 1 guest