Jump to content

[Help] With Coding


Recommended Posts

I have been trying for days trying to make it so that when an umbrella is equipped, that the character does not lose sanity in the rain. The end result of this was so that at night, when raining and wearing an umbrella, that the character wouldn't lose sanity at all. I already have a beautiful code working that does cause a massive amount of sanity loss at night [I did this on purpose for the character]. I have something coded finally that doesn't crash the game or give errors, but it doesn't work per say either. Any ideas how to go about this? Because I am lost. I have also tried to go about this with using sanity tags and all I got was 'umbrella not defined'. 

Any suggestions would be wonderful.

 

Quote

local function umbrella(user) local delta = 0
  if TheWorld.state.israining and umbrella_equipped then delta = 0
  end
  end

Edit: Added code. [I also have the umbrella assets at the very top of the .lua as well.]

Edited by TooMchONI
Link to comment
Share on other sites

6 minutes ago, ZupaleX said:

To be able to help we will need to see that code :)

I edited my OP and added the code I had come up with that doesn't crash the game but doesn't work either. For now I am just testing the code for day/dusk/night.

Link to comment
Share on other sites

Alright, so you were on a good trail with the inst.components.sanity.custom_rate_fn

However what you are doing in there is probably not doing what you expect.

Let's take a look at the component sanity, especially the function handling the sanity loss or gain:

Spoiler

function Sanity:Recalc(dt)
    local total_dapperness = self.dapperness
    for k, v in pairs(self.inst.components.inventory.equipslots) do
        if v.components.equippable ~= nil then
            total_dapperness = total_dapperness + v.components.equippable:GetDapperness(self.inst)
        end
    end

    total_dapperness = total_dapperness * self.dapperness_mult

    local dapper_delta = total_dapperness * TUNING.SANITY_DAPPERNESS

    local moisture_delta = easing.inSine(self.inst.components.moisture:GetMoisture(), 0, TUNING.MOISTURE_SANITY_PENALTY_MAX, self.inst.components.moisture:GetMaxMoisture())

    local light_delta
    if TheWorld.state.isday and not TheWorld:HasTag("cave") then
        light_delta = TUNING.SANITY_DAY_GAIN
    else
        local lightval = CanEntitySeeInDark(self.inst) and .9 or self.inst.LightWatcher:GetLightValue()
        light_delta =
            (   (lightval > TUNING.SANITY_HIGH_LIGHT and TUNING.SANITY_NIGHT_LIGHT) or
                (lightval < TUNING.SANITY_LOW_LIGHT and TUNING.SANITY_NIGHT_DARK) or
                TUNING.SANITY_NIGHT_MID
            ) * self.night_drain_mult
    end

    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 (self.neg_aura_absorb > 0 and self.neg_aura_absorb * -aura_val or aura_val) * self.neg_aura_mult or aura_val)
        end
    end

    local mount = self.inst.components.rider:IsRiding() and self.inst.components.rider:GetMount() or nil
    if mount ~= nil and mount.components.sanityaura ~= nil then
        local aura_val = mount.components.sanityaura:GetAura(self.inst)
        aura_delta = aura_delta + (aura_val < 0 and (self.neg_aura_absorb > 0 and self.neg_aura_absorb * -aura_val or aura_val) * self.neg_aura_mult or aura_val)
    end

    self:RecalcGhostDrain()
    local ghost_delta = TUNING.SANITY_GHOST_PLAYER_DRAIN * self.ghost_drain_mult

    self.rate = dapper_delta + moisture_delta + light_delta + aura_delta + ghost_delta + self.externalmodifiers:Get()

    if self.custom_rate_fn ~= nil then
        self.rate = self.rate + self.custom_rate_fn(self.inst)
    end

    self.rate = self.rate * self.rate_modifier
    self.ratescale =
        (self.rate > .2 and RATE_SCALE.INCREASE_HIGH) or
        (self.rate > .1 and RATE_SCALE.INCREASE_MED) or
        (self.rate > .01 and RATE_SCALE.INCREASE_LOW) or
        (self.rate < -.3 and RATE_SCALE.DECREASE_HIGH) or
        (self.rate < -.1 and RATE_SCALE.DECREASE_MED) or
        (self.rate < -.02 and RATE_SCALE.DECREASE_LOW) or
        RATE_SCALE.NEUTRAL

    --print (string.format("dapper: %2.2f light: %2.2f TOTAL: %2.2f", dapper_delta, light_delta, self.rate*dt))
    self:DoDelta(self.rate * dt, true)
end

 

You will notive that the custom_rate_fn is called almost at the end. And you will also notice that it doesn't override the previously calculated rate, it just adds to it.

So doing what you did

local function sanityfn(inst)
	local delta = 0

	if TheWorld.state.isday and not TheWorld.state.israining then 
		delta = 0
	elseif TheWorld.state.isday and TheWorld.state.israining then 
		delta = - 0.25
	elseif TheWorld.state.isdusk and not TheWorld.state.israining then 
		delta = 0.25
  	elseif TheWorld.state.isdusk and TheWorld.state.israining then 
		delta = -0.25
	elseif TheWorld.state.isnight and not TheWorld.state.israining then 
		delta = 0.15
  	elseif TheWorld.state.isnight and TheWorld.state.israining then 
		delta = - 1
    elseif TheWorld.state.isnight and TheWorld.state.israining then 
		delta = 0
	end 

	return delta
end

Means that when it is day and it is raining, you return 0 for custom_rate_fn, so have an effective sanity rate of

self.rate + 0 = self.rate in the function I linked above. The result of that is a sanity loss/gain unmodified.

Then maybe that was the purpose of this piece of code, I don't know but as far as I understood what you want to do, I don't think so.

Anyway, you will notice that custom_rate_fn is a function which takes the instance affected by the sanity business.

You could write inside that function something like

local function sanityfn(inst)
	local delta = 0

	if TheWorld.state.isday and inst.components and inst.components.inventory then
		local equipped = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
		if equipped and equipped.prefab == "umbrella" then
			delta = [whatever you want it to be if the player has umbrella equipped]
		else
			delta = [whatever you want it to be when the player has something else equipped]
		end
	elseif [some other condition] then
		[...]
	end

	return delta
end

Keep in mind that what you return will be added to the current rate and not override it. So if you want to cancel the sanity loss, saying delta = 0 in your custom sanity rate function won't work.

Link to comment
Share on other sites

So I ran this code you gave me. The very last part and after sifting through the "you forgot this" and "That doesn't go here". I finally came to an error that said that it was trying to calculate an nil. I changed numbers to positives and negatives and still got the same issue. I feel like I am really close to having this solved. I want to thank you for the code. I am going to keep playing with it. Maybe it's my stupidity getting in the way.

Link to comment
Share on other sites

Don't hesitate to post your attempts so we can try to debug it. What I sent is not 100% failsafe. It's written in a web browser without any in-game testing. That's just a skeleton of code to get started.

Link to comment
Share on other sites

Alright so... I decided to only work with this part of the code you gave me. I figured I would some how over complicate it if tried to do coding for all the days cycles right now.

Quote

local function sanityfn(inst)
	local delta = 0

	if TheWorld.state.isday and inst.components and inst.components.inventory then
		local equipped = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
		if equipped and equipped.prefab == "umbrella" then
			delta = [whatever you want it to be if the player has umbrella equipped]
		else
			delta = [whatever you want it to be when the player has something else equipped]
		end

So I played around and plugged in numbers and this is the code I have now:

Quote

local function sanityfn(inst)
    if TheWorld.state.isday and inst.components and inst.componetns.inventory then
       local equipped = inst.components.inventoru:GetEquippedItem(EQUIPSLOTS) 
        if equipped and equipped.prefab == "umbrella" then
        delta = 1.5 
     else
       delta = -1
       end
      end
     end
     
        
     
end

Master_posinit

Quote

inst.components.sanity.custom_rate_fn = sanityfn

I actually don't know why I needed that extra end way down at the bottom ... but I couldn't get 'if' at the top to end without it. I even rewrote the entire code again exactly how you gave it to me thinking I goofed some place... eh...  Anyways this code threw this error:

2wdzu5e.png

And I have no idea what that means at all. 

 

EDIT: 

Okay I figured out the <oef> thing was just referring to that extra End I was talking about. I removed it and now I get this error... this is back to the Nil thing I was talking about last night.

91mr1g.png

I'm also including a new character.lua file with everything I have currently.

Character.lua

Edited by TooMchONI
Added new Character lua
Link to comment
Share on other sites

OMG! That's what I get for coding when I just wake up! LOL Okay so I actually found two typos. Components was misspelled and inventory was misspelled.  But the Code doesn't throw errors now. I just need to figure out what I need to do to actually make it work. 

Quote

local function sanityfn(inst)
    if TheWorld.state.isday and inst.components and inst.componets.inventory then
       local equipped = inst.components.inventory:GetEquippedItem(EQUIPSLOTS) 
        if equipped and equipped.prefab == "umbrella" then
        delta = 1.5 
     else
       delta = -1
       end
      end
    end

This is the code as of right now. And this is what doesn't show errors. However, it isn't working either. Am I forgetting something? I'm going to sift back through the sanity.lua and see see if I am missing anything.

Link to comment
Share on other sites

You should try to a print inside that function to see if it is actually called when it is day and you have an umbrella like

local function sanityfn(inst)
	print("Calling custom sanity fn")

	if TheWorld.state.isday and inst.components and inst.components.inventory then
		print("TheWorld.state is day")

		local equipped = inst.components.inventory:GetEquippedItem(EQUIPSLOTS) 
		print("Equipped item: " .. tostring(equipped))

		if equipped and equipped.prefab == "umbrella" then
			print("Applying custom satiny rate with umbrella")
			delta = 1.5 
		else
			print("Applying custom satiny rate without an umbrella")
			delta = -1
		end
	end
end

Then equip an umbrella or not and open the console to see if these get printed out.

Edited by ZupaleX
Link to comment
Share on other sites

After sitting and thinking about this, I think there is nothing wrong with the code at all. I think I need to be more precise on which equipped item I want it to use. I used the term Umbrella... but cause I thought that was a broad term for Eyebrella, Umbrella and the Parasol... I must be wrong. So I am scratching this code and starting over. Thanks for the help. :)

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...