question about lua

Everything about mods can be found here.

Moderator: Morax

question about lua

Postby asimov59 » 30 Dec 2015, 14:17

Hello everyone, and long life to FAF (yes this is my first message)

First sorry because my english is bad, so thank you for doing the reading effort.

I have some idea for FA mods and i want to create them, for information i have already using lua for scripting (crysis war on server side) so i'm not totally noob but not so good too ;)

I have find a quick reference here: http://supcom.wikia.com/wiki/LUADOC_1.5.3599

So i want to do some test with GetUnitsInRect()

When i try to call it i have a message error : "access to nonexistent global..."

No problem: i search in all Lua file the GetUnitsInRect déclaration location and next i import the function. But i don't see the declaration anywhere.

In wiki this function is under "Sim.<global>"

My question is pretty simple finally: how to call a function declared under "scope" like Sim.<global> for example ?


I'm sure is quite simple but i'm totally blocked with that.

Best Regard,

asimov59
asimov59
 
Posts: 3
Joined: 30 Dec 2015, 13:46
Has liked: 2 times
Been liked: 0 time
FAF User Name: asimov59

Re: question about lua

Postby Myxir » 30 Dec 2015, 14:21

Sim.<global> functions can be directly called from anywhere within the Sim, without importing the function
if that does not work for you, you're probably tinkering with UI code, which is unable to use the Sim functions

http://wiki.faforever.com/index.php?tit ... Sim_and_UI
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: question about lua

Postby asimov59 » 30 Dec 2015, 15:00

Exactly, I call this function from _BeatFunction from /lua/ui/game/economy.lua

If I understand, If I want to get some units with UnitInRect and draw informations about these units i must get the data somewhere in Sim and use UserSync to have theses data available from UI.

I think it should take me some time to understand that.

First mod i want to do is an economic helper, so i want access eco data to all of my units, do some stuff on data and draw infos on screen. Are they already available from UI ?

And last it's more an ethic question (probably better ask in other topic),

making a predictive economic graph(how is your eco and how much ressource in the next few minuts based on actual ressource and engies and structure action stack) is a borderline mod you think ? I have some pros and cons in mind
asimov59
 
Posts: 3
Joined: 30 Dec 2015, 13:46
Has liked: 2 times
Been liked: 0 time
FAF User Name: asimov59

Re: question about lua

Postby Myxir » 30 Dec 2015, 15:18

you can access all selected units from the UI, their focused units, ... in short, it's possible to have a list of all units that you own, their position, health, resource drain etc.
i advise to use the common mod tools mod by Crotalus for this

Code: Select all
local units = import('/mods/common/units.lua')

function getUnitsInRect(x0, z0, x1,z1)
    local inRect = {}
    local pos, x, z = {}, 0, 0
    for _,u in units.Get() do
        pos = u:GetPosition()
        x, z = pos[1], pos[3]
        if (x >= x0) and (x <= x1) and (z >= z0) and (z <= z1) then
            table.insert(inRect, u)
        end
    end
    return inRect
end


do not forget to require the mod for your own though (in your mod info file), to ensure you don't run into import errors
Code: Select all
requires = {"zcbf6277-24e3-437a-b968-Common-v1"}
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: question about lua

Postby asimov59 » 30 Dec 2015, 15:54

Exactly what I need ! now i have just to learn how to use UI and do the stuff :)

thank you so much for the help Myxir !
asimov59
 
Posts: 3
Joined: 30 Dec 2015, 13:46
Has liked: 2 times
Been liked: 0 time
FAF User Name: asimov59


Return to Mods & Tools

Who is online

Users browsing this forum: No registered users and 1 guest