About Script instanciating...

Everything about mods can be found here.

Moderator: Morax

About Script instanciating...

Postby Redmoth » 13 Apr 2016, 05:00

Hi. I'm currently working on Lua scripts for SupCom 2. Not sure where I should ask this, but seeing as lua scripting is so similar in supcom 2 to FA, I guess I'll give here a shot :D

I am new to Lua, but able to write a few scripts. One thing I dont understand, is how SupCom instances things.
For example: SupCom 2 has an aiBrain.lua class.

When you start a game, each player (humans and ais) each get their own instance of an aiBrain class, Which lets you control each player's aiBrain individually without affecting others. You can call GetBrain() on a player, and that will return the player's aiBrain instance.

What I dont get is: how is this class instanced for the player? How is it that a new aiBrain is created when you start a game?
Does local var = import('lua/sim/aiBrain.lua') make a new instance of aiBrain.lua?

I am trying to write a custom AI class instance that will coexist with the normal aiBrain.lua instance.

so when the aiBrain class is instanced, it calls its own OnCreate() function, which in turn instances a 'My custom ai.lua'.

Another blatant example is instances of a unit class on every unit in the game. unless I am mistaken.

Any answers? Any answer, from simple to complicated, I will apreciate :)
Redmoth
 
Posts: 8
Joined: 13 Apr 2016, 04:49
Has liked: 4 times
Been liked: 0 time
FAF User Name: Redmoth

Re: About Script instanciating...

Postby Crotalus » 13 Apr 2016, 08:34

The engine uses import('/lua/aibrain.lua').AIBrain as class during the init steps, then returns this object through OnCreateArmyBrain() in simInit.lua. AFAIK you cannot get it to use anything other than "AIBrain" in that class so you have to do a bit of magic if you want to extend depending on AI. A first step could be:


Code: Select all
Funcs = {
    SomeCoolFunc = function(self)
        print "This is a cool function I will inject on the brain
    end,
}

-- assume you have 'brain' as reference to the brain you want to change
for k, f in Funcs do
    brain[k] = f
end


That will dynamically add / overwrite the functions to the brain.

(paths for Forged Alliance, have no idea if SupCom2 has brain at same place)
Crotalus
Priest
 
Posts: 319
Joined: 27 Jan 2013, 20:37
Has liked: 2 times
Been liked: 102 times
FAF User Name: Crotalus

Re: About Script instanciating...

Postby Redmoth » 13 Apr 2016, 11:01

Hi, thank you very much Crotalus for the clear answer :) I think this gives me a better Idea of how I want to proceed. I will actually probably have one main script loop that will do its stuff on all AI controlled armies, or have one start whenever an AI OnCreateArmyBrain() is called, depending on which I find more simple and visually clearer. Basically I wont go as far as to use the magic you are speaking about lol. It will be more a state machine than some complex system with out of the box scripts, getting info fed to it by the aiBrains and determining if it should take an action (in short). Luckily most functions I need are already in the game's lua files, such as stuff for monnitoring eco drain/production, issuing build orders and etc.

Thank you again, I had a very hard time grasping what was going on with the aiBrain :)
Redmoth
 
Posts: 8
Joined: 13 Apr 2016, 04:49
Has liked: 4 times
Been liked: 0 time
FAF User Name: Redmoth


Return to Mods & Tools

Who is online

Users browsing this forum: No registered users and 1 guest