Forged Alliance Forever Forged Alliance Forever Forums 2016-01-05T14:40:36+02:00 /feed.php?f=41&t=11346 2016-01-05T14:40:36+02:00 2016-01-05T14:40:36+02:00 /viewtopic.php?t=11346&p=117073#p117073 <![CDATA[Re: NEED HELP : Accessing custom datas during modding]]>
As you said, i succeed with my bro in sending a global custom database of numbers and text from Unit.lua (sim) to Userui.lua. Very very usefull.

Statistics: Posted by Franck — 05 Jan 2016, 14:40


]]>
2016-01-04T10:09:01+02:00 2016-01-04T10:09:01+02:00 /viewtopic.php?t=11346&p=116985#p116985 <![CDATA[Re: NEED HELP : Accessing custom datas during modding]]>
As seen its not difficult to pass global variables across to the UI (userlayer) and vice versa (using a simcallback from the userlayer to the simlayer)...

there is actually an easier way to store unit specific data to the userlayer.. its actually a default function in unit.lua.

all you have to do is declare a variable on the units Sync table itself like so..

Code:
self.Sync.mytestvar = 1


This is then stored in the UnitData table in the userlayer under the units entityid and is updated when the variable changes, its also auto cleaned when the unit dies. (its a global table in the userlayer) :)

there are limitations to sending data from sim to userlayer, namely that you cannot send an object, like for instance.. self, you can only send numbers and text, you can ofcourse send tables, but they cannot contain C objects. if you need to send an object send its entityId instead :)

hope that helps.

Statistics: Posted by Domino — 04 Jan 2016, 10:09


]]>
2016-01-02T14:07:46+02:00 2016-01-02T14:07:46+02:00 /viewtopic.php?t=11346&p=116893#p116893 <![CDATA[Re: NEED HELP : Accessing custom datas during modding]]> changes to data. This is why on UserSync you need to merge with the last result to get a comprehensive list.

Statistics: Posted by nine2 — 02 Jan 2016, 14:07


]]>
2016-01-02T15:38:38+02:00 2016-01-02T14:05:40+02:00 /viewtopic.php?t=11346&p=116892#p116892 <![CDATA[Re: NEED HELP : Accessing custom datas during modding]]>
Edit: well this code works in general but not perfectly for my particular case. This doesn't reduce eco data for when you are crashing eco, so I need a different solution.

Edit: I wonder if it might just be easier to use unit:SetStat("hits", value) in sim and unit:GetStat("hits") in ui. Looks like it will work, which would bypass all of the code below - but data may be accessible to opponent - not sure.

SimSync.lua

Code:
local oldResetSyncTable = ResetSyncTable

ResetSyncTable = function()
    oldResetSyncTable()
   
   Sync.FixedEcoData = {}
end


Unit.Lua

Code:
function GetEconDataRecord(id)
   local row = Sync.FixedEcoData[id]
   if row == nil then
      row =  { massConsumed=0, energyConsumed=0 }
      Sync.FixedEcoData[id] = row
   end
   return row
end

local oldUnit = Unit
Unit = Class(oldUnit) {
   SetConsumptionPerSecondMass = function(self, n)
      oldUnit.SetConsumptionPerSecondMass(self, n)
      
      local econData = GetEconDataRecord(self:GetEntityId())
      econData.massConsumed = n
   end,

   SetConsumptionPerSecondEnergy = function(self, n)
      oldUnit.SetConsumptionPerSecondEnergy(self, n)
      
      local econData = GetEconDataRecord(self:GetEntityId())
      econData.energyConsumed = n
   end,
}


UserSync.lua

Code:
local oldOnSync = OnSync

FixedEcoData = {}

function OnSync()

   if not table.empty(Sync.FixedEcoData) then
      FixedEcoData = table.merged(FixedEcoData,Sync.FixedEcoData)
   end

   for id,v in Sync.ReleaseIds do
      FixedEcoData[id] = nil
   end

   oldOnSync()

end


Then in your ui somewhere

Code:
local data = FixedEcoData[unit:GetEntityId()]
LOG(data.massConsumed)
LOG(data.energyConsumed)

Statistics: Posted by nine2 — 02 Jan 2016, 14:05


]]>
2016-01-01T17:45:20+02:00 2016-01-01T17:45:20+02:00 /viewtopic.php?t=11346&p=116878#p116878 <![CDATA[Re: NEED HELP : Accessing custom datas during modding]]> ), my problem has been fixed.

I just needed to move the UserSync.lua file to the Mymod/hook/lua directory. My mistake was keeping the UserSync.lua file in the same original mohodata path in mymod.

Statistics: Posted by Franck — 01 Jan 2016, 17:45


]]>
2015-12-31T11:33:41+02:00 2015-12-31T11:33:41+02:00 /viewtopic.php?t=11346&p=116831#p116831 <![CDATA[Re: NEED HELP : Accessing custom datas during modding]]>
Myxir wrote:
the most likely possiblity is, that one of your code pieces is not hooking correctly. just add some LOG to make sure that is not the case


I will test this

Statistics: Posted by Franck — 31 Dec 2015, 11:33


]]>
2015-12-31T11:28:14+02:00 2015-12-31T11:28:14+02:00 /viewtopic.php?t=11346&p=116830#p116830 <![CDATA[Re: NEED HELP : Accessing custom datas during modding]]>
i checked the mod_info.lua, it seems ok...

Code:
ui_only = false

Statistics: Posted by Franck — 31 Dec 2015, 11:28


]]>
2015-12-31T11:08:32+02:00 2015-12-31T11:08:32+02:00 /viewtopic.php?t=11346&p=116829#p116829 <![CDATA[Re: NEED HELP : Accessing custom datas during modding]]>
Code:
DoTakeDamage = function(self, instigator, amount, vector, damageType)
      ---
      LOG('took damage')
      Sync.mytestvar = 100
      ---
      local preAdjHealth = self:GetHealth()
        self:AdjustHealth(instigator, -amount)
        local health = self:GetHealth()
        if( health <= 0 ) then

making sure that sync is hooked nicely spams the LOGs, but we don't have much choice, right?
Code:
local oldOnSync = OnSync
function OnSync()
   oldOnSync()
   LOG('on sync')
   ---   
   if Sync.mytestvar then
         LOG('My test var spotted in on sync func')
   end
   ---
end


something possibly related that i can imagine is that your mod claims to be UI only, in the mod_info file, thus it's not hooking sim code

also feel free to pm my irc account in FAF, "Washy"

Statistics: Posted by Myxir — 31 Dec 2015, 11:08


]]>
2015-12-31T10:50:45+02:00 2015-12-31T10:50:45+02:00 /viewtopic.php?t=11346&p=116827#p116827 <![CDATA[Re: NEED HELP : Accessing custom datas during modding]]>
I was not able to catch my sync var at the on sync function from the sim side.

I made the matter so simple possible with a test

I put a simple sync var declaration in the unit.lua like this :

Code:
DoTakeDamage = function(self, instigator, amount, vector, damageType)
      ---
      Sync.mytestvar = 100
      ---
      local preAdjHealth = self:GetHealth()
        self:AdjustHealth(instigator, -amount)
        local health = self:GetHealth()
        if( health <= 0 ) then


Then i just hook the UserSync.lua file like this :
Code:
local oldOnSync = OnSync
function OnSync()
   oldOnSync()
   ---   
   if Sync.mytestvar then
         LOG('My test var spotted in on sync func')
   end
   ---
end


The log test in the F9 console is never called ? That why my code is stucked.

what did i forget in this simple test ?

Statistics: Posted by Franck — 31 Dec 2015, 10:50


]]>
2015-12-28T21:50:25+02:00 2015-12-28T21:50:25+02:00 /viewtopic.php?t=11346&p=116653#p116653 <![CDATA[Re: NEED HELP : Accessing custom datas during modding]]>
I understand much more now. I succeed in building a stable code. i put somes LOG('...') everywhere to watch the functions interaction.

Everything seems ok just one thing still doesn't work : the event 'if Sync.hitsTaken then' in the UserSync.lua hook in the OnSync() is never called. I must be wrong somewhere.

I will investigate more time to fix this. Thx a lot for your help :)

Statistics: Posted by Franck — 28 Dec 2015, 21:50


]]>
2015-12-27T22:44:14+02:00 2015-12-27T22:44:14+02:00 /viewtopic.php?t=11346&p=116593#p116593 <![CDATA[Re: NEED HELP : Accessing custom datas during modding]]> http://wiki.faforever.com/index.php?tit ... Sim_and_UI
here's a bit of information about sim/ui states and interaction

i'm not really familar with unit modding, so my advice is more of a general explaination to sending stuff to UI, which is probably not a perfect fit for your problem
unfortunally i can't test the code out, but it should work something like this xD


basically, all files in the /lua/sim dir are for sim side and all in the /lua/ui are for ui, files located just in /lua belong to either one.
if you want to send your data from the sim to the UI, you can use
Code:
Sync.damagehits = 1

in any sim side code, for example the /lua/sim/Unit.lua

though, having one var for each unit that takes a hit is.. not good. let's make a table, in the form
Code:
hitsTaken = {
    [unitId_1] =  hitsTaken_1,
    [unitId_2] =  hitsTaken_2,
}


here is the current faf modded Unit.lua: https://github.com/FAForever/fa/blob/de ... m/Unit.lua
to store this information on the sim side, we can easily add a new var to the Unit.lua class body by hooking the class, add the var, increment it there, and send changes to the sync
Code:
-- saving reference to the old class
local oldUnit = Unit
-- replacing the old class with our modified one, so we can make changes work
Unit = Class(oldUnit) {
    -- saving your value, each instance of the class (unit) has its own hitsTaken counter
    hitsTaken = 0,

     -- store the reference to the old Unit.lua OnDamage function
     local oldOnDamage = self.OnDamage
     OnDamage = function(self, instigator, amount, vector, damageType)
          -- calling the old OnDamage function, otherwise it would not receive any damage anymore
          oldOnDamage(self, instigator, amount, vector, damageType)
          self.hitsTaken = self.hitsTaken + 1
          -- make sure the table exists, otherwise we run into an error
          if not Sync.hitsTaken then
               Sync.hitsTaken = {}
          end
          -- save the var in the sync table as in the above mentioned format
          Sync.hitsTaken[self:GetUnitId()] = self.hitsTaken
     end
end


ok now we have modified the class, the sync table will receive the updated values in the above described form. the sync table will be copied to the sync table in /lua/UserSync.lua, where we can access it from the UI side.

let's hook /lua/UserSync.lua, get the values in the sync table, and store them in the UI side for later use
Code:
-- use your mod file in your mod folder: /mods/<modname>/modules/<modfile>.lua
local myModFile = import('/mods/myMod/modules/myFile.lua')

local oldOnSync = OnSync
function OnSync()
    oldOnSync()
    if Sync.hitsTaken then
        myModFile.onHitsTaken(Sync.hitsTaken)
    end
end


ok now we have the info on the user side, and are sending it to /mods/myMod/modules/myFile.lua, to the onHitsTaken function... you surely have that one already? ;)
there we should store it, to access it later, actually a table in the already used format is simple and will do well

Code:
local storedValues = {}

-- storing them as simple as it can get
function onHitsTaken(hitsTakenTable)
    for id, value in hitsTakenTable do
        storedValues[id] = value
    end
end

-- and providing a way to get them later
function getHitsTakenByUnit(id)
    -- will return a value if we have one, 0 otherwise
    return storedValues[id] or 0
end


alright, now you should be able to access them

Statistics: Posted by Myxir — 27 Dec 2015, 22:44


]]>
2015-12-27T21:06:51+02:00 2015-12-27T21:06:51+02:00 /viewtopic.php?t=11346&p=116588#p116588 <![CDATA[Re: NEED HELP : Accessing custom datas during modding]]>

You are gaving me keys to understand, but my brain went totally out of sync with you :roll:

if i understand well, by sim side you mean all the lua files located in the sim directory ? and by UI layer all the files located in the ui directories ? Then the user side, all my personnal lua mod files ?


I understand the idea. The simside and the usersides are locked. In need to use the sync datas. Ok for the concept but it doesn't mean any practical solution in my head. i looked in the forum
I found this link : /viewtopic.php?f=41&t=9574. I sadly have less skills than locutus in modding.

So in my example : i need to use a Sync.damagehits variable in the unit.lua files instead of _G.Damagehits. I increment this one in my function DoTakeDamage with Sync.damagehits = Sync.damagehits + 1. Then i understand that my datas have been copied during the sync tick. So how can i access them now ? I don't know what i need to do in the Onsync() function...If i understand well, the onsync function is the event called when the datas are copied ?

how can i have access to the data in the ui side ?


Thanks for your kind patience. Just imagine you were talking to a kid of 10 year who wanna modding ;)

Franck

Statistics: Posted by Franck — 27 Dec 2015, 21:06


]]>
2015-12-27T01:12:43+02:00 2015-12-27T01:12:43+02:00 /viewtopic.php?t=11346&p=116544#p116544 <![CDATA[Re: NEED HELP : Accessing custom datas during modding]]>
Myxir wrote:
my guess is that you are trying to access information of the Sim side, from the user side, which the game is preventing you from doing.
to send that information to the UI side, where you can access it from the user interface, you need to use something like
Code:
Sync.myModData = 5


and then, in UserSync.lua, you need to hook the onsync function to extend it to react to your updated value
Code:
local oldOnSync = OnSync
function OnSync()
    oldOnSync()
    if Sync.myModData then
        -- do your stuff here
    end
end



He doesn't necessarily need to extend OnSync :)

Statistics: Posted by Sheeo — 27 Dec 2015, 01:12


]]>
2015-12-27T01:11:41+02:00 2015-12-27T01:11:41+02:00 /viewtopic.php?t=11346&p=116543#p116543 <![CDATA[Re: NEED HELP : Accessing custom datas during modding]]>
You're storing data from within the simulation, and attempting to access it from the UI state, which can't access the simulation data directly. If you want to know why this is the case, you should read up on how the engine works, there are various articles spread about but I don't have any links handy for you, sorry.

To get data from the sim to the UI layer, you need to put it into the global sync table. Look at SimSync.lua. For every simulation beat this table is copied to the user layer, and you can access it from the UI state.

Statistics: Posted by Sheeo — 27 Dec 2015, 01:11


]]>
2015-12-27T01:07:17+02:00 2015-12-27T01:07:17+02:00 /viewtopic.php?t=11346&p=116542#p116542 <![CDATA[Re: NEED HELP : Accessing custom datas during modding]]> to send that information to the UI side, where you can access it from the user interface, you need to use something like
Code:
Sync.myModData = 5


and then, in UserSync.lua, you need to hook the onsync function to extend it to react to your updated value
Code:
local oldOnSync = OnSync
function OnSync()
    oldOnSync()
    if Sync.myModData then
        -- do your stuff here
    end
end

Statistics: Posted by Myxir — 27 Dec 2015, 01:07


]]>