Jump to content

[Solved] Help with making code more efficient


Recommended Posts

Hello, I need some help making some annoying wall of code smaller (if possible), I'd really appreciate any help since scrolling though this's a real pain..

So, here's the code...

Spoiler

local function Update_Stats(inst)

	if inst.components.health:IsDead() then
		return
	end
	
	local sane = inst.components.sanity:IsSane() -- Sane
	local insane = not inst.components.sanity:IsSane() -- Insane

	local gelid_nil = inst.gelid_mode == nil -- Gelid
	local gelid_true = inst.gelid_mode == true -- Gelid

	local wathgrithr_nil = inst.wathgrithr_mode == nil -- Valkyrie
	local wathgrithr_true = inst.wathgrithr_mode == true -- Valkyrie

	if inst.components.hunger.current >= 250 then -- Above 250 hunger.
		inst.components.locomotor.runspeed = 1.4 * TUNING.WILSON_RUN_SPEED

		if sane then
			if gelid_nil and wathgrithr_nil then -- Sane
				inst.components.health.absorb = -1
				inst.components.hunger.hungerrate = 3.5 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_true and wathgrithr_nil then  -- Sane & Gelid
				inst.components.health.absorb = -.5
				inst.components.hunger.hungerrate = 3 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_nil and wathgrithr_true then -- Sane & Valkyrie
				inst.components.health.absorb = -.7
				inst.components.hunger.hungerrate = 3.8 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_true and wathgrithr_true then -- Sane & Gelid & Valkyrie
				inst.components.health.absorb = -.2
				inst.components.hunger.hungerrate = 3.3 * TUNING.WILSON_HUNGER_RATE
			end
		elseif insane then
			if gelid_nil and wathgrithr_nil then -- Insane
				inst.components.health.absorb = -.5
				inst.components.hunger.hungerrate = 5 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_true and wathgrithr_nil then -- Insane & Gelid
				inst.components.health.absorb = 0
				inst.components.hunger.hungerrate = 4.5 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_nil and wathgrithr_true then -- Insane & Valkyrie
				inst.components.health.absorb = -.2
				inst.components.hunger.hungerrate = 5.3 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_true and wathgrithr_true then -- Insane & Gelid & Valkyrie
				inst.components.health.absorb = .3
				inst.components.hunger.hungerrate = 4.8 * TUNING.WILSON_HUNGER_RATE
			end
		end

	elseif inst.components.hunger.current >= 200 then -- Above 200 hunger.
		inst.components.locomotor.runspeed = 1.3 * TUNING.WILSON_RUN_SPEED
		
		if sane then
			if gelid_nil and wathgrithr_nil then -- Sane
				inst.components.health.absorb = -1.2
				inst.components.hunger.hungerrate = 3 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_true and wathgrithr_nil then  -- Sane & Gelid
				inst.components.health.absorb = -.7
				inst.components.hunger.hungerrate = 2.5 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_nil and wathgrithr_true then -- Sane & Valkyrie
				inst.components.health.absorb = -.9
				inst.components.hunger.hungerrate = 3.3 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_true and wathgrithr_true then -- Sane & Gelid & Valkyrie
				inst.components.health.absorb = -.4
				inst.components.hunger.hungerrate = 2.8 * TUNING.WILSON_HUNGER_RATE
			end
		elseif insane then
			if gelid_nil and wathgrithr_nil then -- Insane
				inst.components.health.absorb = -.7
				inst.components.hunger.hungerrate = 4.5 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_true and wathgrithr_nil then -- Insane & Gelid
				inst.components.health.absorb = -.2
				inst.components.hunger.hungerrate = 4 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_nil and wathgrithr_true then -- Insane & Valkyrie
				inst.components.health.absorb = -.4
				inst.components.hunger.hungerrate = 4.8 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_true and wathgrithr_true then -- Insane & Gelid & Valkyrie
				inst.components.health.absorb = .1
				inst.components.hunger.hungerrate = 4.3 * TUNING.WILSON_HUNGER_RATE
			end
		end

	elseif inst.components.hunger.current >= 150 then -- Above 150 hunger.
		inst.components.locomotor.runspeed = 1.2 * TUNING.WILSON_RUN_SPEED
      
		if sane then
			if gelid_nil and wathgrithr_nil then -- Sane
				inst.components.health.absorb = -1.4
				inst.components.hunger.hungerrate = 2.5 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_true and wathgrithr_nil then  -- Sane & Gelid
				inst.components.health.absorb = -.9
				inst.components.hunger.hungerrate = 2 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_nil and wathgrithr_true then -- Sane & Valkyrie
				inst.components.health.absorb = -1.1
				inst.components.hunger.hungerrate = 2.8 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_true and wathgrithr_true then -- Sane & Gelid & Valkyrie
				inst.components.health.absorb = -.6
				inst.components.hunger.hungerrate = 2.3 * TUNING.WILSON_HUNGER_RATE
			end
		elseif insane then
			if gelid_nil and wathgrithr_nil then -- Insane
				inst.components.health.absorb = -.9
				inst.components.hunger.hungerrate = 4 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_true and wathgrithr_nil then -- Insane & Gelid
				inst.components.health.absorb = -.4
				inst.components.hunger.hungerrate = 3.5 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_nil and wathgrithr_true then -- Insane & Valkyrie
				inst.components.health.absorb = -.6
				inst.components.hunger.hungerrate = 4.3 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_true and wathgrithr_true then -- Insane & Gelid & Valkyrie
				inst.components.health.absorb = -.1
				inst.components.hunger.hungerrate = 3.8 * TUNING.WILSON_HUNGER_RATE
			end
		end
   
	elseif inst.components.hunger.current >= 100 then -- Above 100 hunger.
		inst.components.locomotor.runspeed = 1.1 * TUNING.WILSON_RUN_SPEED
      
		if sane then
			if gelid_nil and wathgrithr_nil then -- Sane
				inst.components.health.absorb = -1.6
				inst.components.hunger.hungerrate = 2 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_true and wathgrithr_nil then  -- Sane & Gelid
				inst.components.health.absorb = -1.1
				inst.components.hunger.hungerrate = 1.5 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_nil and wathgrithr_true then -- Sane & Valkyrie
				inst.components.health.absorb = -1.3
				inst.components.hunger.hungerrate = 2.3 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_true and wathgrithr_true then -- Sane & Gelid & Valkyrie
				inst.components.health.absorb = -.8
				inst.components.hunger.hungerrate = 1.8 * TUNING.WILSON_HUNGER_RATE
			end
		elseif insane then
			if gelid_nil and wathgrithr_nil then -- Insane
				inst.components.health.absorb = -1.1
				inst.components.hunger.hungerrate = 3.5 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_true and wathgrithr_nil then -- Insane & Gelid
				inst.components.health.absorb = -.6
				inst.components.hunger.hungerrate = 3 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_nil and wathgrithr_true then -- Insane & Valkyrie
				inst.components.health.absorb = -.8
				inst.components.hunger.hungerrate = 3.8 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_true and wathgrithr_true then -- Insane & Gelid & Valkyrie
				inst.components.health.absorb = -.3
				inst.components.hunger.hungerrate = 3.3 * TUNING.WILSON_HUNGER_RATE
			end
		end

	elseif inst.components.hunger.current >= 50 then -- Above 50 hunger.
		inst.components.locomotor.runspeed = 1 * TUNING.WILSON_RUN_SPEED
		
		if sane then
			if gelid_nil and wathgrithr_nil then -- Sane
				inst.components.health.absorb = -1.8
				inst.components.hunger.hungerrate = 1.5 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_true and wathgrithr_nil then  -- Sane & Gelid
				inst.components.health.absorb = -1.3
				inst.components.hunger.hungerrate = 1 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_nil and wathgrithr_true then -- Sane & Valkyrie
				inst.components.health.absorb = -1.5
				inst.components.hunger.hungerrate = 1.8 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_true and wathgrithr_true then -- Sane & Gelid & Valkyrie
				inst.components.health.absorb = -1
				inst.components.hunger.hungerrate = 1.3 * TUNING.WILSON_HUNGER_RATE
			end
		elseif insane then
			if gelid_nil and wathgrithr_nil then -- Insane
				inst.components.health.absorb = -1.3
				inst.components.hunger.hungerrate = 3 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_true and wathgrithr_nil then -- Insane & Gelid
				inst.components.health.absorb = -.8
				inst.components.hunger.hungerrate = 2.5 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_nil and wathgrithr_true then -- Insane & Valkyrie
				inst.components.health.absorb = -1
				inst.components.hunger.hungerrate = 3.3 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_true and wathgrithr_true then -- Insane & Gelid & Valkyrie
				inst.components.health.absorb = -.5
				inst.components.hunger.hungerrate = 2.8 * TUNING.WILSON_HUNGER_RATE
			end
		end

	elseif inst.components.hunger.current <= 50 then -- Below 50 hunger.
		inst.components.locomotor.runspeed = .75 * TUNING.WILSON_RUN_SPEED

		if sane then
			if gelid_nil and wathgrithr_nil then -- Sane
				inst.components.health.absorb = -2
				inst.components.hunger.hungerrate = 1 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_true and wathgrithr_nil then  -- Sane & Gelid
				inst.components.health.absorb = -1.5
				inst.components.hunger.hungerrate = .5 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_nil and wathgrithr_true then -- Sane & Valkyrie
				inst.components.health.absorb = -1.7
				inst.components.hunger.hungerrate = 1.3 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_true and wathgrithr_true then -- Sane & Gelid & Valkyrie
				inst.components.health.absorb = -1.2
				inst.components.hunger.hungerrate = .8 * TUNING.WILSON_HUNGER_RATE
			end
		elseif insane then
			if gelid_nil and wathgrithr_nil then -- Insane
				inst.components.health.absorb = -1.5
				inst.components.hunger.hungerrate = 2.5 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_true and wathgrithr_nil then -- Insane & Gelid
				inst.components.health.absorb = -1
				inst.components.hunger.hungerrate = 2 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_nil and wathgrithr_true then -- Insane & Valkyrie
				inst.components.health.absorb = -1.2
				inst.components.hunger.hungerrate = 2.8 * TUNING.WILSON_HUNGER_RATE
			
			elseif gelid_true and wathgrithr_true then -- Insane & Gelid & Valkyrie
				inst.components.health.absorb = -.7
				inst.components.hunger.hungerrate = 2.3 * TUNING.WILSON_HUNGER_RATE
			end
		end

   end
end

 

The code's supposed to update my character's defense (health.absorb) & hunger rate, my character has 300 hunger & every time it goes down by 50 points he's supposed to become weaker, plus he has 2 different modes that also change these stats so that adds even more "elseif"..making the code very bloated!

Does anyone know a way I can shorten this code?? Because soon I'm going to add several more modes & I don't want this code to go over 1,000 lines!! 

Any help/advice would be greatly appreciated, thanks for your time & have a great day/night :D!!!

Edited by SuperDavid
Link to comment
Share on other sites

Max hunger's 300 & I don't really want it to be linear, but if that's the only way to shorten it then I guess I have no other option!

edit: by linear you mean like Wolfgang where his stats all get lower for each point that goes down, right?

Edited by SuperDavid
Link to comment
Share on other sites

You can convert the set of true/false into binary indexes and access the values via an array.

It might be easier to have each mode affect the stat by some factor and this also makes it easier for people to know what each mode means.

...

  local statIndex = inst.gelid_mode and 0 or 1
  statIndex += inst.wathgrithr_mode and 0 or 2
  statIndex += inst.components.sanity:IsSane() and 0 or 4

  local healthStat = {
    -- >=250 hunger
    { -1, -.5, -.7, -.2, -- sane
    -.5, 0, -.2, .3 }, --insane
  }
  local hungerMultiplierStat = {
    -- >=250 hunger
    { 3.5, 3, 3.8, 3.3, -- sane
    5, 4.5, 5.3, 4.8 }, -- insane
  }

	if inst.components.hunger.current >= 250 then -- Above 250 hunger.
		inst.components.locomotor.runspeed = 1.4 * TUNING.WILSON_RUN_SPEED
		inst.components.health.absorb = healthStat[0][statIndex]
		inst.components.hunger.hungerrate = hungerMultiplierStat[0][statIndex] * TUNING.WILSON_HUNGER_RATE
...

 

Edited by SlowMaybe
lua
Link to comment
Share on other sites

@SuperDavid Alright, I figured out that you didn't just put numbers randomly. Turns out you have some formula for them. Here's the code. I've not tested yet.

local function Update_Stats(inst)
	local ic = inst.components
	local ich = ic.health
	local icu = ic.hunger
	local rspeed = TUNING.WILSON_RUN_SPEED
	local hrate = TUNING.WILSON_HUNGER_RATE
	
	if ich:IsDead() then
		return
	end
	
	local sane = ic.sanity:IsSane() -- Sane
	local insane = not ic.sanity:IsSane() -- Insane
	
	local gelid_nil = inst.gelid_mode == nil -- Gelid
	local gelid_true = inst.gelid_mode == true -- Gelid
	
	local wathgrithr_nil = inst.wathgrithr_mode == nil -- Valkyrie
	local wathgrithr_true = inst.wathgrithr_mode == true -- Valkyrie
	
	local form = math.min(5,math.floor(icu.current/(icu.max/6)))
	
	ic.locomotor:SetExternalSpeedMultiplier(inst, "SuperDavid_SpeedMult", (form==0 and 0.75) or ( 1 + 0.1 * (form-1) ) )
	
	ich.absorb = ( ( (sane and -2) or -1.5 ) + 0.2 * form ) + ( ( (gelid_nil and wathgrithr_nil) and 0 ) or ( (gelid_true and wathgrithr_nil) and 0.5 ) or ( (gelid_nil and wathgrithr_true) and 0.3 ) or 0.8)
	icu.hungerrate = ( ( ( (sane and 1) or 2.5 ) + 0.5 * form ) + ( ( (gelid_nil and wathgrithr_nil) and 0 ) or ( (gelid_true and wathgrithr_nil) and -0.5 ) or ( (gelid_nil and wathgrithr_true) and 0.3 ) or -0.2) ) * hrate
	
	print("Absorption = "..ich.absorb) --'Remove when testing is done'
	print("Hungerate = "..ich.hungerrate) --'Remove when testing is done'
end

 

Edited by JohnWatson
Forgot a Tab
Link to comment
Share on other sites

@JohnWatson Thank you very much!! Your code's much smaller than that crazy mess I made :D!!

The only problem is I don't fully understand it..so can I maybe ask a couple questions about some stuff from you then I can try to understand better:wilson_smile:?

So, first of all I edited the code slightly then I can understand a little better & here is how it looks now if you need to know to help me :).

Spoiler

local function Stats_Update(inst)
	local health = inst.components.health
	local hunger = inst.components.hunger
	local speed = TUNING.WILSON_RUN_SPEED
	local hunger_rate = TUNING.WILSON_HUNGER_RATE
	
	if inst.components.health:IsDead() then
		return
	end
	
	local sane = inst.components.sanity:IsSane() -- Sane
	local insane = not inst.components.sanity:IsSane() -- Insane
	
	local gelid_nil = inst.gelid_mode == nil -- Gelid
	local gelid_true = inst.gelid_mode == true -- Gelid
	
	local wathgrithr_nil = inst.wathgrithr_mode == nil -- Valkyrie
	local wathgrithr_true = inst.wathgrithr_mode == true -- Valkyrie
	
	local form = math.min(5,math.floor(hunger.current/(hunger.max/6)))
	
	inst.components.locomotor:SetExternalSpeedMultiplier(inst, "hunger_speed", (form==0 and 0.75) or ( 1 + 0.1 * (form-1) ) )
	
	health.absorb = ( ( (sane and -2) or -1.5 ) + 0.2 * form )
	+ ( ( (gelid_nil and wathgrithr_nil) and 0 )
	or ( (gelid_true and wathgrithr_nil) and 0.5 )
	or ( (gelid_nil and wathgrithr_true) and 0.3 )
	or 0.8)
	
	
	hunger.hungerrate = ( ( ( (sane and 1) or 2.5 ) + 0.5 * form )
	+ ( ( (gelid_nil and wathgrithr_nil) and 0 )
	or ( (gelid_true and wathgrithr_nil) and -0.5 )
	or ( (gelid_nil and wathgrithr_true) and 0.3 )
	or -0.2) ) * hunger_rate
end

 

For my second thing I'd like to ask is what does this code mean?

local form = math.min(5,math.floor(hunger.current/(hunger.max/6)))

 

My third question's say if I want to edit my character's "health.absorb" or "hunger.hungerrate" how would I do that? Like for example if I want to increase the hungerrate by 50% for when my character's insane & has no mode which one of the numbers would I change here cuz I'm kinda confused what all this means now :oops:..

Spoiler

hunger.hungerrate = ( ( ( (sane and 1) or 2.5 ) + 0.5 * form )
+ ( ( (gelid_nil and wathgrithr_nil) and 0 )
or ( (gelid_true and wathgrithr_nil) and -0.5 )
or ( (gelid_nil and wathgrithr_true) and 0.3 )
or -0.2) ) * hunger_rate

 

And for my last question what's all the numbers in this ExternalSpeedMultiplier mean? Then if I want to increase/lower values in future.

inst.components.locomotor:SetExternalSpeedMultiplier(inst, "SuperDavid_SpeedMult", (form==0 and 0.75) or ( 1 + 0.1 * (form-1) ) )

Also, does ExternalSpeedMultiplier give the same value as multiplying the runspeed? Which goes down by 10% when under 200, 150, 100, & -25% speed of Wilson when under 50 hunger.

 

Sorry, for annoying you & thanks a lot for making the code MUCH smaller :D!

Edited by SuperDavid
Link to comment
Share on other sites

7 hours ago, SuperDavid said:

 

  Hide contents


local function Stats_Update(inst)
	local health = inst.components.health
	local hunger = inst.components.hunger
	local speed = TUNING.WILSON_RUN_SPEED
	local hunger_rate = TUNING.WILSON_HUNGER_RATE
	
	if inst.components.health:IsDead() then
		return
	end
	
	local sane = inst.components.sanity:IsSane() -- Sane
	local insane = not inst.components.sanity:IsSane() -- Insane
	
	local gelid_nil = inst.gelid_mode == nil -- Gelid
	local gelid_true = inst.gelid_mode == true -- Gelid
	
	local wathgrithr_nil = inst.wathgrithr_mode == nil -- Valkyrie
	local wathgrithr_true = inst.wathgrithr_mode == true -- Valkyrie
	
	local form = math.min(5,math.floor(hunger.current/(hunger.max/6)))
	
	inst.components.locomotor:SetExternalSpeedMultiplier(inst, "hunger_speed", (form==0 and 0.75) or ( 1 + 0.1 * (form-1) ) )
	
	health.absorb = ( ( (sane and -2) or -1.5 ) + 0.2 * form )
	+ ( ( (gelid_nil and wathgrithr_nil) and 0 )
	or ( (gelid_true and wathgrithr_nil) and 0.5 )
	or ( (gelid_nil and wathgrithr_true) and 0.3 )
	or 0.8)
	
	
	hunger.hungerrate = ( ( ( (sane and 1) or 2.5 ) + 0.5 * form )
	+ ( ( (gelid_nil and wathgrithr_nil) and 0 )
	or ( (gelid_true and wathgrithr_nil) and -0.5 )
	or ( (gelid_nil and wathgrithr_true) and 0.3 )
	or -0.2) ) * hunger_rate
end

 

Looks like you've only changed some minor things, not much difference.

7 hours ago, SuperDavid said:

For my second thing I'd like to ask is what does this code mean?


local form = math.min(5,math.floor(hunger.current/(hunger.max/6)))

It gets the 'form' level of your character. (hunger.max/6) is 300/6 which is 50. Your character's current hunger is divided by 50, then rounded down, then gets set to 5 if it's greater than 5.

This means your character has 6 forms:

0 - 49 hunger = Form 0
50 - 99 hunger = Form 1
100 - 149 hunger = Form 2
150 - 199 hunger = Form 3
200 - 249 hunger = Form 4
250 - 300 hunger = Form 5

7 hours ago, SuperDavid said:

My third question's say if I want to edit my character's "health.absorb" or "hunger.hungerrate" how would I do that? Like for example if I want to increase the hungerrate by 50% for when my character's insane & has no mode which one of the numbers would I change here cuz I'm kinda confused what all this means now :oops:..

  Hide contents


hunger.hungerrate = ( ( ( (sane and 1) or 2.5 ) + 0.5 * form )
+ ( ( (gelid_nil and wathgrithr_nil) and 0 )
or ( (gelid_true and wathgrithr_nil) and -0.5 )
or ( (gelid_nil and wathgrithr_true) and 0.3 )
or -0.2) ) * hunger_rate

In that piece of code, if you want to change certain values, change this...

Initial sane value: 1st line, change '1' in '(sane and 1)'
Initial insane value: 1st line, change '2.5' in '( (sane and 1) or 2.5 )'
Additional value every form level: 1st line, change '0.5' in '+ 0.5 * form'
Additional value in normal state: 2nd line, change '0'
Additional value in gelid state: 3rd line, change '-0.5'
Additional value in valkyrie state: 4th line, change '0.3'
Additional value in gelid+valkyrie state: 5th line, change '-0.2'

7 hours ago, SuperDavid said:

And for my last question what's all the numbers in this ExternalSpeedMultiplier mean? Then if I want to increase/lower values in future.


inst.components.locomotor:SetExternalSpeedMultiplier(inst, "SuperDavid_SpeedMult", (form==0 and 0.75) or ( 1 + 0.1 * (form-1) ) )

Also, does ExternalSpeedMultiplier give the same value as multiplying the runspeed? Which goes down by 10% when under 200, 150, 100, & -25% speed of Wilson when under 50 hunger.

That adds an external speed multiplier. Kind of the same effect as directly modifying the runspeed.

That piece of code makes your character have different speeds depending on their form level.

Form 0 = 75% speed
Form 1 = 100% speed
Form 2 = 110% speed
Form 3 = 120% speed
Form 4 = 130% speed
Form 5 = 140% speed

Link to comment
Share on other sites

@JohnWatson Sorry to bother you again, but if you can help me with this problem I'm having that'd really be great :D!

So, I wanted to make my character's attack stat update similar to the other stats so I tried this code-

Spoiler

local function Attack_Update(inst, data)
	local hunger = inst.components.hunger
	
	if inst.components.health:IsDead() then
		return
	end
	
	local sane = inst.components.sanity:IsSane() -- Sane
	local insane = not inst.components.sanity:IsSane() -- Insane
	
	local wathgrithr_nil = inst.wathgrithr_mode == nil -- Valkyrie
	local wathgrithr_true = inst.wathgrithr_mode == true -- Valkyrie
	
	local form = math.min(5,math.floor(hunger.current/(hunger.max/6)))
	
	inst.components.combat.damagemultiplier = ( ( (sane and 1.4) or 1.9 ) + (form==0 and 0.75) or ( 1 + 0.1 * (form-1) ) )
	+ ( ( (wathgrithr_nil) and 0 )
	or ( (wathgrithr_true) and 0.3 ) )

end

 

-trying to make the attack stat go down the same value the speed stat does & keeping the mode changes, but I got a crash saying...

Spoiler

crash.jpeg

I don't understand what it means, so maybe you can help me again, please :)?

 

Also, if I can ask one more question from you.. In this code-

Spoiler

local function Stats_Update(inst)
	local health = inst.components.health
	local hunger = inst.components.hunger
	local speed = TUNING.WILSON_RUN_SPEED -- I noticed that local speed isn't being used since doing ExternalSpeedMult so I can remove it, right?
	local hunger_rate = TUNING.WILSON_HUNGER_RATE
	
	if inst.components.health:IsDead() then
		return
	end
	
	local sane = inst.components.sanity:IsSane() -- Sane
	local insane = not inst.components.sanity:IsSane() -- Insane
	
	local gelid_nil = inst.gelid_mode == nil -- Gelid
	local gelid_true = inst.gelid_mode == true -- Gelid
	
	local wathgrithr_nil = inst.wathgrithr_mode == nil -- Valkyrie
	local wathgrithr_true = inst.wathgrithr_mode == true -- Valkyrie
	
	local form = math.min(5,math.floor(hunger.current/(hunger.max/6)))
	
	inst.components.locomotor:SetExternalSpeedMultiplier(inst, "hunger_speed", (form==0 and 0.75) or ( 1 + 0.1 * (form-1) ) )
	
	health.absorb = ( ( (sane and -2) or -1.5 ) + 0.2 * form )
	+ ( ( (gelid_nil and wathgrithr_nil) and 0 )
	or ( (gelid_true and wathgrithr_nil) and 0.5 )
	or ( (gelid_nil and wathgrithr_true) and 0.3 )
	or 0.8)
	
	
	hunger.hungerrate = ( ( ( (sane and 1) or 2.5 ) + 0.5 * form )
	+ ( ( (gelid_nil and wathgrithr_nil) and 0 )
	or ( (gelid_true and wathgrithr_nil) and -0.5 )
	or ( (gelid_nil and wathgrithr_true) and 0.3 )
	or -0.2) ) * hunger_rate
end

 

specifically, at this part the lines at the end you said is if my character has both modes active, yes?

Spoiler

health.absorb = ( ( (sane and -2) or -1.5 ) + 0.2 * form )
+ ( ( (gelid_nil and wathgrithr_nil) and 0 )
or ( (gelid_true and wathgrithr_nil) and 0.5 )
or ( (gelid_nil and wathgrithr_true) and 0.3 )
or 0.8)
	
	
hunger.hungerrate = ( ( ( (sane and 1) or 2.5 ) + 0.5 * form )
+ ( ( (gelid_nil and wathgrithr_nil) and 0 )
or ( (gelid_true and wathgrithr_nil) and -0.5 )
or ( (gelid_nil and wathgrithr_true) and 0.3 )
or -0.2) ) * hunger_rate

 

I was wondering if I could change it to something like this since my character gonna have more modes in the future & I like to see which value is to what modes specifically or would that not work?

Spoiler

health.absorb = ( ( (sane and -2) or (insane and -1.5) ) + 0.2 * form )
+ ( ( (gelid_nil and wathgrithr_nil) and 0 )
or ( (gelid_true and wathgrithr_nil) and 0.5 )
or ( (gelid_nil and wathgrithr_true) and 0.3 )
or ( (gelid_true and wathgrithr_true) and 0.8 ) )
	
	
hunger.hungerrate = ( ( ( (sane and 1) or (insane and 2.5) ) + 0.5 * form )
+ ( ( (gelid_nil and wathgrithr_nil) and 0 )
or ( (gelid_true and wathgrithr_nil) and -0.5 )
or ( (gelid_nil and wathgrithr_true) and 0.3 )
or ( (gelid_true and wathgrithr_true) and -0.2 ) ) ) * hunger_rate

 

Also, when I add more modes in the future will I have to add the "mode_nil" of all the modes into this part of the code, right?

+ ( ( (gelid_nil and wathgrithr_nil) and 0 )

 

Another tiny question I have is if for example, wathgrithr mode's supposed to give a 30% damage boost in this code I would put "0.3" instead of "1.3" like damagemultiplier, right? Sorry for asking sooo many questions &...

thank you very much for your help JohnWatson, thanks to you I got to make my mod hundreds of lines shorter!! For real thanks a lot :D!!

Edited by SuperDavid
Link to comment
Share on other sites

On 2017-6-10 at 0:59 PM, SuperDavid said:

So, I wanted to make my character's attack stat update similar to the other stats so I tried this code-

How are you trying to make it go down? What should be the damage multiplier for each state/form?

On 2017-6-10 at 0:59 PM, SuperDavid said:

I was wondering if I could change it to something like this since my character gonna have more modes in the future & I like to see which value is to what modes specifically or would that not work?

Adding more modes would be complicated without reworking the code I gave, especially since your character can simultaneously have multiple modes at the same time.

On 2017-6-10 at 0:59 PM, SuperDavid said:

Another tiny question I have is if for example, wathgrithr mode's supposed to give a 30% damage boost in this code I would put "0.3" instead of "1.3" like damagemultiplier, right? Sorry for asking sooo many questions &...

Damage multiplier modifies damage by multiplying the base damage with a number. So if you want to deal an extra 30% damage, you would need to change the damage multiplier into 1.3 instead of 0.3 because a 0.3 multiplier would make you deal 70% less damage.

Link to comment
Share on other sites

On 6/12/2017 at 0:23 AM, JohnWatson said:

How are you trying to make it go down? What should be the damage multiplier for each state/form?

@JohnWatson I'm trying to make by default my character has 40% attack boost, & it goes down by 10% every -50 from 300 hunger, & when below 50 hunger it's -25% negative attack just like speed.

When my character goes insane he gets 50% add-on attack boost, & when my character's in wathgrithr mode he gets 30% add-on attack boost, gelid mode doesn't effect attack so I guess no need to include it?

 

On 6/12/2017 at 0:23 AM, JohnWatson said:

Adding more modes would be complicated without reworking the code I gave, especially since your character can simultaneously have multiple modes at the same time.

My character can only be in 2 modes at a time while insane/sane, maybe that makes it less complicated?

E.g. while in gelid & wathgrithr mode my character can't get any more modes unless one of those's removed.

So, if it'd wouldn't be so complicated with 2 mode limit could you maybe teach me how to add a mode to this code whenever you have the time? Cause I have like a bunch of modes, & going back to a million elseif will kill my sanity :wilson_dorky:...

Also, thank you for your help JohnWatson :D!!

Edited by SuperDavid
Link to comment
Share on other sites

The problem is the number of possible combination, you have to write a case for every pair of those.

A way to avoid that is for each mode to stack onto each other independently. Something like below.

Pseudo code

atk_modifier = 1.0

if hunger less than 50
  atk_modifier = -0.25
else
  atk_modifier = atk_modifier + (0.4 - 0.1 * floor((300 - current_hunger)/50))

if insane
  atk_modifier = atk_modifier + 0.5

if wathgrithr
  atk_modifier = atk_modifier + 0.3
  
//assign atk_modifier to use it
Edited by SlowMaybe
Link to comment
Share on other sites

@SlowMaybe Thank you for your help, but I'm kinda having some difficulty with your code.. I don't really know what to do with it :oops:.

I tried doing something like this-

Spoiler

local function Attack_Update(inst, data)
	local hunger = inst.components.hunger
	
	if inst.components.health:IsDead() then
		return
	end
	
	local sane = inst.components.sanity:IsSane() -- Sane
	local insane = not inst.components.sanity:IsSane() -- Insane
	
	local wathgrithr_nil = inst.wathgrithr_mode == nil -- Valkyrie
	local wathgrithr_true = inst.wathgrithr_mode == true -- Valkyrie
	
	local atk_modifier = 1
	
	if hunger.current <= 50 then
		atk_modifier = 0
	else
		atk_modifier = atk_modifier + (0.4 - 0.1 * floor((300 - current_hunger)/50))
	end
	
	if insane then
		atk_modifier = atk_modifier + 0.5
	end

	if wathgrithr_true then
		atk_modifier = atk_modifier + 100
	end
	
	inst.components.combat.damagemultiplier = atk_modifier

end

-but I got a crash saying " 'floor' is not declared" or something like that. Maybe if you have time can you tell me what I'm doing wrong? Cause I'm really confused :(...

And thanks for your help man :D!!

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