Jump to content

[Help Please?] Adding A New Component


Recommended Posts

I'm brand new to coding and I really want to wrap my head around this! I would really love a teacher or a peer to talk code with on a daily basis to start really getting into this. *_* I want to make full mod expansions once I get the hang of things.

 

 

But here's my immediate problem!

 

I'm trying to make a structure with a sanity boost that has a flat rate over a set radius. So no matter how close or far you are from it you will get the same boost, so long as you are in range.

 

I've achieved a functional custom structure with its own craft tab and a regular, distance-scaling sanity aura.

 

I see the code that scales sanity aura in the original components/sanity.lua file

    local aura_delta = 0    local x, y, z = self.inst.Transform:GetWorldPosition()    local ents = TheSim:FindEntities(x, y, z, TUNING.SANITY_EFFECT_RANGE, nil, { "FX", "NOCLICK", "DECOR","INLIMBO" })    for i, v in ipairs(ents) do         if v.components.sanityaura ~= nil and v ~= self.inst then            local aura_val = v.components.sanityaura:GetAura(self.inst) / math.max(1, self.inst:GetDistanceSqToInst(v))            aura_delta = aura_delta + (aura_val < 0 and aura_val * self.neg_aura_mult or aura_val)        end    end
    self.rate = dapper_delta + moisture_delta + light_delta + aura_delta + ghost_delta

and then everything in components/sanityaura.lua and components/aura.lua.

And I see the values in the tuning table that state the values of each aura from tiny - huge, and SANITY_EFFECT_RANGE = 10.

 

But the code I included is the meat of what's adding that distance scaling.

So I was thinking I would create a nearly identical component and just call it "pureaura_delta", copy the lines for noticing objects near the player with auras, remove "/math.max..." from the value computation of aura_val (which will be "pureaura_val"), create a new tuning value for "pureaura" for my structure, and just tack "pureaura_delta" onto the things to be added to self.rate.

But I don't know where to put those components! Do I add them into the modmain file? And do I need to call some global functions to correctly implement that stuff?

Also if I write self.rate == self.rate + pureaura_delta, will that overwrite and ruin the original equation for self.rate?

 

Any help or suggestions are deeply appreciated! ^ ^

Also if you know of any mods with component modifications/additions, please let me know! Perhaps I can use them as a guide.

Link to comment
Share on other sites

I'm not sure I completly get what you want to do.

 

You want the players to gain/loose sanity when standing near your structure, but loose the same amount if they are at 1cm or at the limit of the aura?

 

If that's the case adding a component for that is not useful and would even be counter productive. Like using an assault rifle to chase a mosquito (even though mosquitoes can be very annoying).

 

What I would do is add the sanityaura component to your structure, and instead of setting

mystructure.components.sanityaura.aura = something

I would set as well aurafn. You see that the aurafn is taking as arguments inst and observer. Then in this aurafn, calculate the distance between the observer (the guy who should gain/loose sanity) and your structure and then

mystructure.components.sanityaura.aurafn = function(inst, observer)   local obsStructDist = math.max(1, inst:GetDistanceSqToInst(observer))      return self.aura * obsStructDistend

This way, when the sanity component call

local aura_val = v.components.sanityaura:GetAura(self.inst) / math.max(1, self.inst:GetDistanceSqToInst(v))

it actually calls

local aura_val = v.components.sanityaura.aura * math.max(1, self.inst:GetDistanceSqToInst(v)) / math.max(1, self.inst:GetDistanceSqToInst(v))

so the distance thingy get cancelled and you end up with a flat aura

 

 

Take what i send carefully as I wrote it down here in 1min and a half and did not test it in game, but that's the spirit.

Link to comment
Share on other sites

Well I've been fiddling around with it quite a bit and after many crashes and iterations this is the only line I've gotten to pump out any aura at all:

inst.components.sanityaura.aura = TUNING.SANITYAURA_TINY / 200 * math.max(1, inst:GetDistanceSqToInst(ThePlayer))

(I resorted to this because after trying many different ways of using (observer) depending on the general order of my statements I would get 'observer' undefined or 'sanity.lua trying to perform arithmetic on a nil value')

 

Now the aura is super strong, as can be expected from trying to cancel out the distance scale, thus the / 200.

However it still scales with distance! I'm assuming that must mean somehow GetDistanceSqToInst(Player) isn't coming out the same to the GetDistanceSqToInst(v) in the sanity.lua file?? Either that or I'm failing to understand some other part of the equation.

Link to comment
Share on other sites

You cannot do that because ThePlayer will be

1- if it's player hosted, it will be the distance between the host and the machine which will be used, even if it's a client which is using the machine

2- nothing and will probably crash if it's a dedicated server because in this case ThePlayer is nothing.

 

So I think you have to use that aurafn, that's the only decent way. I can think of something else but it would be horrible and I don't want to explain it. If you have issue to use it, try posting the error logs you get with the aurafn and we can try to sort it out.

Edited by ZupaleX
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...