Forged Alliance Forever Forged Alliance Forever Forums 2020-01-24T22:10:45+02:00 /feed.php?f=41&t=18680 2020-01-24T22:10:45+02:00 2020-01-24T22:10:45+02:00 /viewtopic.php?t=18680&p=181494#p181494 <![CDATA[Re: Replacing a function]]>
I realized that the problem is with all classes that do NOT override the specific function on their own.
So when you hooked a function like this
Code:
local oldStructureUnit = StructureUnit
StructureUnit = Class(oldStructureUnit) {
    OnCreate = function(self)
        oldStructureUnit.OnCreate(self)
        LOG('*** we hooked sucessfully')
    end,
}

you have to make all subclasses, that do not override that function anyway, use the new function like this:
Code:
local oldRadarUnit = RadarUnit
RadarUnit = Class(oldRadarUnit) {
    OnCreate = function(self)
        StructureUnit.OnCreate(self)
    end,
}

You do this for all subclasses that inherit from the original class and do not override the function. These can be a lot, but at least you don't have to paste in your function every time.
This is still not perfect, because when in a later game patch modifies the function in one of the subclasses you override this change right away.
But at least it is working now \o/

Statistics: Posted by BlackYps — 24 Jan 2020, 22:10


]]>
2020-01-24T20:57:53+02:00 2020-01-24T20:57:53+02:00 /viewtopic.php?t=18680&p=181491#p181491 <![CDATA[Re: Replacing a function]]> Statistics: Posted by Uveso — 24 Jan 2020, 20:57


]]>
2020-01-24T20:38:11+02:00 2020-01-24T20:38:11+02:00 /viewtopic.php?t=18680&p=181489#p181489 <![CDATA[Re: Replacing a function]]> I was hoping to avoid that, but if there is no other way, that will do.

Still, thank you very much for your help!

Statistics: Posted by BlackYps — 24 Jan 2020, 20:38


]]>
2020-01-24T20:16:49+02:00 2020-01-24T20:16:49+02:00 /viewtopic.php?t=18680&p=181488#p181488 <![CDATA[Re: Replacing a function]]> Most classes that uses the StructureUnit as base have already a hook for the OnDestroy function. So they are overwriting a hook that was made inside the Structure class.
Also the hooked OnDestroy function from all subclasses of StructureUnit should call the StructureUnit.OnDestroy on the end, but they call Unit.OnDestroy instead.

So if you want to hook the OnDestroy inside Structures you need to make a hook in the parentclass of Structureunits, and this is the unitclass.
Make a hook inside unit.lua for the OnDestroy function. This will be called from any mobile and structure units.
This way the original OnDestroy function will be called and your hook will be executed after the original StructureUnit.OnDestroy.

All other changes like not executing the OnDestroy function in subclasses, you need to hook every single OnDestroy function you want to change.

Statistics: Posted by Uveso — 24 Jan 2020, 20:16


]]>
2020-01-24T19:02:45+02:00 2020-01-24T19:02:45+02:00 /viewtopic.php?t=18680&p=181486#p181486 <![CDATA[Re: Replacing a function]]> Is there a way to have all structures use a hooked function without copying it in every subclass specifically, though?
I hooked the OnCreate function of the StructureUnit class (in defaultunits.lua) and noticed that it works for point defenses, all factories, missile launchers and defense and artillery. This is for all factions.
It seems that in the hooking process not all classes get regenerated with my newly hooked function. However I can't see any systematics behind this. Do you know what is going on?

Statistics: Posted by BlackYps — 24 Jan 2020, 19:02


]]>
2020-01-22T20:44:30+02:00 2020-01-22T20:44:30+02:00 /viewtopic.php?t=18680&p=181464#p181464 <![CDATA[Re: Replacing a function]]> Structures have a class for every faction.

Create a new file:
Code:
\Mods\oil_slicks\hook\lua\cybranunits.lua


insert this to the empty file:
Code:
local oldCLandFactoryUnit = CLandFactoryUnit
CLandFactoryUnit = Class(oldCLandFactoryUnit) {

    OnCreate = function(self)
        WARN('***---*** CLandFactoryUnit OnCreate HOOK')
        oldCLandFactoryUnit.OnCreate(self)
    end,

    OnKilled = function(self, instigator, type, overkillRatio)
        WARN('***---*** CLandFactoryUnit OnKilled HOOK')
        oldCLandFactoryUnit.OnKilled(self, instigator, type, overkillRatio)
    end,

    OnDestroy = function(self)
        WARN('***---*** CLandFactoryUnit OnDestroy HOOK')
        oldCLandFactoryUnit.OnDestroy(self)
    end,
}


Now create and destroy a CYBRAN LAND Factory.

Statistics: Posted by Uveso — 22 Jan 2020, 20:44


]]>
2020-01-21T17:15:58+02:00 2020-01-21T17:15:58+02:00 /viewtopic.php?t=18680&p=181426#p181426 <![CDATA[Re: Replacing a function]]> The final defaultunits.lua loaded in the game looks like this I assume:

Code:
StructureUnit = Class(Unit) {
--a ton of functions
}

FactoryUnit = Class(StructureUnit ) {
--more functions
}

--now comes the hooked part from the mod

local oldStructureUnit = StructureUnit
StructureUnit = Class(oldStructureUnit) {
    OnKilled = function(self)
        LOG('***---*** StructureUnit OnKilled HOOK')
        oldStructureUnit.OnDestroy(self)
        local orient = self.TarmacBag.Orientation
        local currentBP = self.TarmacBag.CurrentBP
        self:DestroyTarmac()
        self:CreateTarmac(true, true, true, orient, currentBP, currentBP.DeathLifetime or 300)
    end,
   
    OnDestroy = function(self)
        LOG('***---*** StructureUnit OnDestroy HOOK')
        Unit.OnDestroy(self)
    end,
}


So a part of the class definition comes after the subclasses are created. The changes from the mod seem to not always work.
I attached the mod so you can have a look.
Note that the Aircraftcarrier automagically uses the new function defined in seaunit.
However the changes in Structureunit seem to not be used by the child classes.
This doesn't make any sense to me. Like, at all!

I really appreciate your help!

Statistics: Posted by BlackYps — 21 Jan 2020, 17:15


]]>
2020-01-21T01:21:19+02:00 2020-01-21T01:21:19+02:00 /viewtopic.php?t=18680&p=181408#p181408 <![CDATA[Re: Replacing a function]]>
Code:
Unit = Class(moho.unit_methods)

moho.unit_methods is a c-class imported to LUA, the root file with all basics for the unitclass can be found in:
Code:
Supreme Commander\Gamedata\mohodata.SCD\lua\sim\Unit.lua

Now we create the StructureUnit. We are using the Unitclass as base.
That means everything included in the unitclass ist also part of the StructureUnit (if we not overwrite something)
Code:
StructureUnit = Class(Unit) {}

Finaly we creating the FactoryUnitclass.
Code:
FactoryUnit = Class(StructureUnit) {}

At this point the FactoryUnit has all function from the unitclass and all added functions from the StructureUnitclass.

If you now want to hook the FactoryUnit, first copy the original FactoryUnit:
Code:
local oldFactoryUnit = FactoryUnit


Now we are creating our own FactoryUnitclass:
Code:
FactoryUnit = Class(oldFactoryUnit)


We don't need to add the StructureUnitclass because (old)FactoryUnit is a child from StructureUnitclass that is also a child from unitclass.

Statistics: Posted by Uveso — 21 Jan 2020, 01:21


]]>
2020-01-21T00:27:55+02:00 2020-01-21T00:27:55+02:00 /viewtopic.php?t=18680&p=181407#p181407 <![CDATA[Re: Replacing a function]]> Hooking the structureunit class works well. The problem occurs when I try to propagate the change down to the subclasses.

Apparently this is not the way to go:
Code:
local oldFactoryUnit = FactoryUnit
FactoryUnit = Class(StructureUnit, oldFactoryUnit) {}

Statistics: Posted by BlackYps — 21 Jan 2020, 00:27


]]>
2020-01-21T00:08:46+02:00 2020-01-21T00:08:46+02:00 /viewtopic.php?t=18680&p=181406#p181406 <![CDATA[Re: Replacing a function]]> Statistics: Posted by Uveso — 21 Jan 2020, 00:08


]]>
2020-01-21T00:07:04+02:00 2020-01-21T00:07:04+02:00 /viewtopic.php?t=18680&p=181405#p181405 <![CDATA[Re: Replacing a function]]>
There must be a reason why it doesn't work in the other mod...
I will try to find it. But I don't really have an idea what the reason could be.

Statistics: Posted by BlackYps — 21 Jan 2020, 00:07


]]>
2020-01-20T23:41:58+02:00 2020-01-20T23:41:58+02:00 /viewtopic.php?t=18680&p=181404#p181404 <![CDATA[Re: Replacing a function]]> Just to be sure;
create a file named:
Code:
\Mods\YOURMODNAME\hook\lua\defaultunits.lua

and put this into the empty file:
Code:
local OLDStructureUnit = StructureUnit
StructureUnit = Class(OLDStructureUnit) {
    OnDestroy = function(self)
        LOG('***---*** StructureUnit OnDestroy HOOK')
        Unit.OnDestroy(self)
        local orient = self.TarmacBag.Orientation
        local currentBP = self.TarmacBag.CurrentBP
        self:DestroyTarmac()
        self:CreateTarmac(true, true, true, orient, currentBP, currentBP.DeathLifetime or 300)
    end,
}


Does this work on your side ?

Statistics: Posted by Uveso — 20 Jan 2020, 23:41


]]>
2020-01-20T23:42:17+02:00 2020-01-20T23:12:09+02:00 /viewtopic.php?t=18680&p=181403#p181403 <![CDATA[Re: Replacing a function]]>
Code:
local oldStructureUnit = StructureUnit
StructureUnit = Class(oldStructureUnit) {
   OnDestroy = function(self)
      Unit.OnDestroy(self)
   end,
}


Looks good so far; you copied the original function to "oldStructureUnit".
But now you try to access "oldStructureUnit" only with "unit" to get the OnDestroy function.
You need to use the copy of the original:
Code:
oldStructureUnit.OnDestroy(self)


so the function should look like this:
Code:
local oldStructureUnit = StructureUnit
StructureUnit = Class(oldStructureUnit) {
   OnDestroy = function(self)
      oldStructureUnit.OnDestroy(self)
   end,
}

Statistics: Posted by Uveso — 20 Jan 2020, 23:12


]]>
2020-01-20T22:14:58+02:00 2020-01-20T22:14:58+02:00 /viewtopic.php?t=18680&p=181396#p181396 <![CDATA[Re: Replacing a function]]> I want to get rid of the additions that the StructureUnit class makes to OnDestroy

When using your method and writing
Code:
local oldStructureUnit = StructureUnit
StructureUnit = Class(oldStructureUnit) {
   OnDestroy = function(self)
      Unit.OnDestroy(self)
   end,
}

I get the error:
Code:
field 'OnDestroy' is ambiguous in class definition

It seems that it doesn't overwrite the new one with the old one but uses both and then the game is confused abot which one to take.
What am I doing wrong?

Statistics: Posted by BlackYps — 20 Jan 2020, 22:14


]]>
2020-01-20T18:54:11+02:00 2020-01-20T18:54:11+02:00 /viewtopic.php?t=18680&p=181379#p181379 <![CDATA[Re: Replacing a function]]> Statistics: Posted by Uveso — 20 Jan 2020, 18:54


]]>