Running code when a unit is created

Interesting mapping tools and mapping help.

Moderator: Morax

Running code when a unit is created

Postby EntropyWins » 18 Aug 2018, 08:16

I'd like a way to run lua code when a unit gets created. In a map script. Something like this function:

oncreated.png
oncreated.png (2.65 KiB) Viewed 3091 times


I've been looking through quite a lot of the FA/FAF lua code and have not found anything providing such functionality. Is there really no way of doing this?

Of course there is one way... which is what I did here https://pastebin.com/KBM72wBv . Having to loop through all units and seeing if there is a new one every tick is quite stupid though, especially if the game already provides a more sane way of reacting to the unit creation event.
Last edited by EntropyWins on 18 Aug 2018, 12:36, edited 1 time in total.
EntropyWins
Avatar-of-War
 
Posts: 113
Joined: 02 Oct 2016, 20:26
Has liked: 18 times
Been liked: 4 times
FAF User Name: EntropyWins

Re: Running code when a unit is created

Postby speed2 » 18 Aug 2018, 09:06

User avatar
speed2
Contributor
 
Posts: 3189
Joined: 05 Jan 2013, 15:11
Has liked: 636 times
Been liked: 1119 times
FAF User Name: speed2

Re: Running code when a unit is created

Postby EntropyWins » 21 Aug 2018, 17:17

Hooking that function is not possible in a map script. So I am still looking for a way to do this.

The lua script I linked in my post also has a number of issues, with one of them maybe not fixable. Hence all the more reason to have a sane existing way to react to unit creation.
EntropyWins
Avatar-of-War
 
Posts: 113
Joined: 02 Oct 2016, 20:26
Has liked: 18 times
Been liked: 4 times
FAF User Name: EntropyWins

Re: Running code when a unit is created

Postby speed2 » 21 Aug 2018, 17:40

Cause it's not something that the map script should be able to do.
User avatar
speed2
Contributor
 
Posts: 3189
Joined: 05 Jan 2013, 15:11
Has liked: 636 times
Been liked: 1119 times
FAF User Name: speed2

Re: Running code when a unit is created

Postby EntropyWins » 21 Aug 2018, 17:49

I'm going to assume you are saying that hooking that function is not something a map script should be able to do. If on the other hand you are saying that a map script should not be able to react to an "unit created" even, then my reaction is WHY THE FIRGATE NOT?!!! Since this is super useful for many things. For instance to apply health buffs, to bind expiry timers, to remove wreckages, etc.
EntropyWins
Avatar-of-War
 
Posts: 113
Joined: 02 Oct 2016, 20:26
Has liked: 18 times
Been liked: 4 times
FAF User Name: EntropyWins

Re: Running code when a unit is created

Postby Uveso » 21 Aug 2018, 18:27

its no question if you should, you can't.

Unitblueprints are initalized inside blueprint.lua and will be registered inside the c-engine there.

if you now change the blueprint in your mapscript, it will not be registered inside the c-engine.

And woops desync...

[Edit]
(all c-engine commands will get the old unit data, but if you read the blueprint itself you will get the new data.
And this is only working local, not in multiplayer)
User avatar
Uveso
Supreme Commander
 
Posts: 1788
Joined: 11 Dec 2015, 20:56
Location: Germany
Has liked: 70 times
Been liked: 291 times
FAF User Name: Uveso

Re: Running code when a unit is created

Postby EntropyWins » 21 Aug 2018, 18:43

Yeah sure, so forget about hooking the that function. That is not what I am asking about. I'm asking if there is a way to run code when a unit gets created. Is there seriously no event of sorts for that?
EntropyWins
Avatar-of-War
 
Posts: 113
Joined: 02 Oct 2016, 20:26
Has liked: 18 times
Been liked: 4 times
FAF User Name: EntropyWins

Re: Running code when a unit is created

Postby Uveso » 21 Aug 2018, 23:38

No, you must hook that function. This function is the event.
User avatar
Uveso
Supreme Commander
 
Posts: 1788
Joined: 11 Dec 2015, 20:56
Location: Germany
Has liked: 70 times
Been liked: 291 times
FAF User Name: Uveso

Re: Running code when a unit is created

Postby DDDX » 03 Sep 2018, 16:01

I use this for my "is Paragon built" script:

Code: Select all
for x, army in ListArmies() do                               
                 if (army == "ARMY_1" or army == "ARMY_2" or army == "ARMY_3" or army == "ARMY_4" or army == "ARMY_5" or army == "ARMY_6") then
                     List = GetArmyBrain(army):GetListOfUnits(categories.xab1401, false, true)
                         for k, unit in List do      
                           if (unit:GetUnitId() == 'xab1401') then
                               if not (unit:GetFractionComplete() ~= 1) then


the last segment is to let the map know the unit is completed and not merely being built ;)
Check out my 2 maps: "Survival_Mayhem&BO_3d_v1" "Survival_Mayhem&BO_3d_RPG"
as well as my mod: "Survival Mayhem&BO balance"
-- let me know of any bugs or issues regarding those 3.
DDDX
Avatar-of-War
 
Posts: 170
Joined: 21 Mar 2016, 16:13
Has liked: 18 times
Been liked: 16 times
FAF User Name: DDDX

Re: Running code when a unit is created

Postby EntropyWins » 15 Sep 2018, 13:33

Thanks for sharing DDDX! I did not know GetListOfUnits and have some good uses for it.

Looks like you have a double negation in your inner if predicate. And is the one above really needed given you already filter on categories.xab1401? Finally, if you do not use the key in a for loop, I believe it is convention to name it _.
EntropyWins
Avatar-of-War
 
Posts: 113
Joined: 02 Oct 2016, 20:26
Has liked: 18 times
Been liked: 4 times
FAF User Name: EntropyWins

Next

Return to Mapping

Who is online

Users browsing this forum: No registered users and 1 guest