Jump to content

Need help with using AddComponentPostInit() to add to Class(function(...))


Nappy

Recommended Posts

So I have been working with understanding PostInit() because it seems like the most advantageous way to edit components so that on future patches the mod stays viable.  I have had success adding new component functions AND replacing old component functions using AddComponentPostInit().  However, now I am trying to add a variable to the Class of a component function and could use some help.  The example of what I have successfully done by editing a Component file was to add this to component/childspawner ...

local Childspawner = Class(function(self, inst)

... self.rarechild1 = nil
    self.rarechild2 = nil
    self.rarechild3 = nil
    self.rarechildchancex3 = 0.1  ...end)

 

function ChildSpawner:SetRareChildx3(childname1, childname2, childname3, chance)
    self.rarechild1 = childname1
    self.rarechild2 = childname2
    self.rarechild3 = childname3
    self.rarechildchancex3 = chance
end

 

function ChildSpawner:SpawnChild(target, prefab, radius)

...if self.rarechild1 and math.random() < self.rarechildchancex3 then
        local pickrarechild = math.random(1,3)
        if pickrarechild == 1 then
            childtospawn = self.rarechild1
        elseif pickrarechild == 2 then
            childtospawn = self.rarechild2
        elseif pickrarechild == 3 then
            childtospawn = self.rarechild3
        end
    end  ... end

Thus, using ComponentsPostInit() I have figured out how to add function ChildSpawner:SetRareChildx3 and edit already existing function ChildSpawn:SpawnChild, BUT I NEED HELP adding to the Class(Function(...)) the variables I am calling to save the 3 different rare children please.  THANKS!

 

Link to comment
Share on other sites

Solved: For those interested, I found my answer by looking at this post:

I did not use the hack, but it helped me understand AddClassPostConstruct() and I used...

AddClassPostConstruct("components/childspawner", function(self)
    self.rarechild1 = nil
    self.rarechild2 = nil
    self.rarechild3 = nil
    self.rarechildchancex3 = 0.1
end)

 

I do have another question for you other modders out there, do you know how to reduce the amount of text I am replacing with AddComponentPostInit()?  As it is, for components.childspawner:SpawnChild(), there are 90+ lines of code, but I only need to add 10 lines of code and I am not altering anything already written.  However, the way I am using AddComponentPostInit() I am replacing the whole function when really I just wanted to add these few lines of code to it.  Just hit me back if you know how to do this, thx!

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...