NEED HELP : Accessing custom datas during modding

Everything about mods can be found here.

Moderator: Morax

NEED HELP : Accessing custom datas during modding

Postby Franck » 25 Dec 2015, 22:33

Hi everybody ! My question seems strange but it's a specific programming matter.

During modding i can easily access to some units blueprints datas by calling a simple method like Unit:GetBlueprint().Defense.Health. Again, I can access to my custom blueprints datas like Unit:GetBlueprint().mycustomdata.

It works fine to catch some data. But how can i modify and store theses datas and access to them in real time ? It's ok for health because a specific method exists called unit:AdjustHealth(). So I can manage the health data in realtime. But How can i do it for my customs datas ? If i store some custom datas in a lua module like unit.lua (for example : unit.XP = 7500) i cannot access theses in another module like unitview.lua ? When i want to access it says 'nil' value when i recall unit.XP

My general goal is to store my custom datas (for example unit.XP) and managing theses datas during the modding execution.


Any idea ? I must be possible, i saw a lot of mods storing customs datas (i m thinking about total veterancy or others...).

Thanks a lot for your help ;) !
Franck
Crusader
 
Posts: 28
Joined: 24 Dec 2015, 16:16
Has liked: 1 time
Been liked: 1 time
FAF User Name: Franck

Re: NEED HELP : Accessing custom datas during modding

Postby nine2 » 26 Dec 2015, 02:32

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

Re: NEED HELP : Accessing custom datas during modding

Postby Resin_Smoker » 26 Dec 2015, 04:26

Custom data as in data tables?
Resin_Smoker
Evaluator
 
Posts: 858
Joined: 14 Mar 2012, 17:58
Has liked: 54 times
Been liked: 106 times

Re: NEED HELP : Accessing custom datas during modding

Postby Franck » 26 Dec 2015, 08:46

All kind of custom datas i wanna create in my mod.

Each time a unit is created, the engine takes the health data from blueprint(). After, the Unit:AdjustHealth(self, instigator, amount,...) method can modify during the game in realtime global health data. It s the same thing for some other specific datas like healthRegen, unit speed....where specific methods exist. So we can access these datas. For example health, regen datas are registered as global variable.

But can we store and modify in realtime custom datas ? Not just taking theses from blueprint. It seems that the engine do not store custom datas as global variables, but just as local ones, during a function execution. So each time i'm taking custom datas from my blueprint and i modify them during mod execution, i cannot store back them before i'm leaving my function.

Is there a global data table for all units i can access to add, remove or delete some custom datas ?
Franck
Crusader
 
Posts: 28
Joined: 24 Dec 2015, 16:16
Has liked: 1 time
Been liked: 1 time
FAF User Name: Franck

Re: NEED HELP : Accessing custom datas during modding

Postby Franck » 26 Dec 2015, 14:28

Resin_Smoker wrote:Custom data as in data tables?


Simple variables will be fine for me.
Franck
Crusader
 
Posts: 28
Joined: 24 Dec 2015, 16:16
Has liked: 1 time
Been liked: 1 time
FAF User Name: Franck

Re: NEED HELP : Accessing custom datas during modding

Postby Sheeo » 26 Dec 2015, 16:14

The sim doesn't care where you store data. Nor does the UI state.

Store it wherever using normal reference assignment given to you by the lua language.
Support FAF on patreon: https://www.patreon.com/faf?ty=h

Peek at our continued development on github: https://github.com/FAForever
Sheeo
Councillor - Administrative
 
Posts: 1038
Joined: 17 Dec 2013, 18:57
Has liked: 109 times
Been liked: 233 times
FAF User Name: Sheeo

Re: NEED HELP : Accessing custom datas during modding

Postby Franck » 27 Dec 2015, 00:02

Thanks for your reply Sheeo.

My level of LUA is somewhere low...but how to deal with global variable in lua between files ?

I made a simple test to explain myself : imagine I want to store the incremential unit damage hits in a global variable then link the variable to the name of the unit info windows.


This the variable _G.Damagehits = 0 declaration in unit.lua. I use the _G prefix in lua to force the same environnements between lua files.

Code: Select all
#****************************************************************************
#**
#**  File     :  /lua/unit.lua
#**  Author(s):  John Comes, David Tomandl, Gordon Duclos
#**
#**  Summary  : The Unit lua module
#**
#**  Copyright © 2005 Gas Powered Games, Inc.  All rights reserved.
#****************************************************************************

local Entity = import('/lua/sim/Entity.lua').Entity
local explosion = import('/lua/defaultexplosions.lua')
local EffectTemplate = import('/lua/EffectTemplates.lua')
local EffectUtilities = import('/lua/EffectUtilities.lua')
local Game = import('/lua/game.lua')
local utilities = import('/lua/utilities.lua')
local Shield = import('/lua/shield.lua').Shield
local UnitShield = import('/lua/shield.lua').UnitShield
local AntiArtilleryShield = import('/lua/shield.lua').AntiArtilleryShield
local Buff = import('/lua/sim/buff.lua')
local AIUtils = import('/lua/ai/aiutilities.lua')
local Blueprints = import('/lua/system/blueprints.lua')
-- my code
_G.damagehits = 0
-- end of my code


Then i go to the DoTakeDamage function still in unit.lua to increment the variable. I put a log to watch the variable content during execution.

Code: Select all
   DoTakeDamage = function(self, instigator, amount, vector, damageType)
        local preAdjHealth = self:GetHealth()
-- my code       
        _G.Damagehits = _G.Damagehits + 1
        LOG('_G.Damagehits variable in function DOTAKEDAMAGE :', _G.Damagehits)
-- end of my code
        self:AdjustHealth(instigator, -amount)


Then i go to unitview.lua file. I link the _G.Damagehits variable to the unit name.

Code: Select all
if info.customName then
-- my code     
            controls.name:SetText(LOCF('%d: %s', _G.Damagehits, description))
-- end of my code   
    elseif bp.General.UnitName then
            controls.name:SetText(LOCF('%s: %s', bp.General.UnitName, description))
        else
            controls.name:SetText(LOCF('%s', description))
        end


I run the mod. I run the F9 debug windows. I start a ACU battle. And i watch the results

The ACU unitview windows name stay at 0 during all the test, just the _G.damagehits declaration value.

In the unit DOtakeDamage LOG test, it says that the _G.Damagehits increments well.


So my question is :

Why did the _G.Damagehits global variable increment in the function DoTakeDamage in unit.lua but says at the defaut value of 0 in unitview.lua ?

Each time i select my ACU the view function refresh but the _G.Damagehits stays at '0'.

Why this variable doesn't refresh between lua files ? What i am missing ? Any Idea ?
Franck
Crusader
 
Posts: 28
Joined: 24 Dec 2015, 16:16
Has liked: 1 time
Been liked: 1 time
FAF User Name: Franck

Re: NEED HELP : Accessing custom datas during modding

Postby Myxir » 27 Dec 2015, 01:07

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: Select all
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: Select all
local oldOnSync = OnSync
function OnSync()
    oldOnSync()
    if Sync.myModData then
        -- do your stuff here
    end
end
Unhappy with balance http://i.imgur.com/q5G2BlM.png
User avatar
Myxir
Evaluator
 
Posts: 791
Joined: 09 Apr 2012, 14:01
Has liked: 94 times
Been liked: 306 times
FAF User Name: Washy (irc)

Re: NEED HELP : Accessing custom datas during modding

Postby Sheeo » 27 Dec 2015, 01:11

The game has two primary lua execution states.

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.
Support FAF on patreon: https://www.patreon.com/faf?ty=h

Peek at our continued development on github: https://github.com/FAForever
Sheeo
Councillor - Administrative
 
Posts: 1038
Joined: 17 Dec 2013, 18:57
Has liked: 109 times
Been liked: 233 times
FAF User Name: Sheeo

Re: NEED HELP : Accessing custom datas during modding

Postby Sheeo » 27 Dec 2015, 01:12

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: Select all
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: Select all
local oldOnSync = OnSync
function OnSync()
    oldOnSync()
    if Sync.myModData then
        -- do your stuff here
    end
end


He doesn't necessarily need to extend OnSync :)
Support FAF on patreon: https://www.patreon.com/faf?ty=h

Peek at our continued development on github: https://github.com/FAForever
Sheeo
Councillor - Administrative
 
Posts: 1038
Joined: 17 Dec 2013, 18:57
Has liked: 109 times
Been liked: 233 times
FAF User Name: Sheeo

Next

Return to Mods & Tools

Who is online

Users browsing this forum: No registered users and 1 guest