Jump to content

[Solved] Widget/badge value drops on load.


Recommended Posts

Hi, so I've been working on the widget for quite some time now and I never really understood why the value keeps dropping to 0 as soon as the world is loaded.

Example:

DTHaKdi.gif.d01e4d0639309ebe4be9999a5b47fa06.gif

I already talked about widget making in an other post but that was before I started actually codding it. Now it's a scripting issue more than a "how to" issue.


I have some files attached for anyone brave enough to surf through them but otherwise I'm open to suggestions as to how to fix this issue. So madness.lua is a component, it's got it's replica too. madplayer_classified.lua is a prefab that  I'm using to declare net vars and to add the badge to all characters and then madnessbadge.lua which is a widget. All of these are their respective folders. (components,prefabs,widgets in that order)

Thanks in advance for anyone who helps!

madness.lua madness_replica.lua madplayer_classified.lua madnessbadge.lua

Edited by AmanitaDST
Link to comment
Share on other sites

I can't look into it deeper right now, I should be able to later today though. But first thing I noticed is that you're adding the components "reader" and "madness" to the client too. You should only add them server side. Also, just use AddPlayerPostInit to add something to players, you don't need to go through the entire list of character prefabs.

  • Thanks 1
Link to comment
Share on other sites

Found the issue! The reason the meter was depleting like that was only a visual thing. The way these meters work is that they have a long animation of them depleting, but they're set to a certain frame based on how full the stat is. That's why it was depleting, it wasn't being set to any specific frame.

In your "madplayer_classified" file, you were missing a bunch of code in your StatusDisplays _ctor hook to listen for certain events and such, but I made it for you (since I was testing around). I also made sure that the badge gets hidden if you're a ghost.

local function OnSetPlayerMode(inst, self)
	self.madness_modetask = nil
	
	if self.owner.replica.madness ~= nil then
		if self.onmadnessdelta == nil then
			self.onmadnessdelta = function(owner, data) self:MadnessDelta(data) end
			self.inst:ListenForEvent("madnessdelta", self.onmadnessdelta, self.owner)
			self:SetMadnessPercent(self.owner.replica.madness:GetPercent())
		end
	end
end

local function OnSetGhostMode(inst, self)
	self.madness_modetask = nil
	
	if self.owner.replica.madness ~= nil then
		if self.onmadnessdelta ~= nil then
			self.inst:RemoveEventCallback("madnessdelta", self.onmadnessdelta, self.owner)
			self.onmadnessdelta = nil
		end
	end
end

function self:SetMadnessPercent(pct)
	self.madbrain:SetPercent(pct, self.owner.replica.madness:Max())
	
	if pct <= 0 then
		self.madbrain:StartWarning()
	else
		self.madbrain:StopWarning()
	end
end

function self:MadnessDelta(data)
	self:SetMadnessPercent(data.newpercent)
	if self.madbrain ~= nil and self.madbrain.MadnessDelta then
		self.madbrain:MadnessDelta(data)
	else
		if not data.overtime then
			if data.newpercent > data.oldpercent then
				self.madbrain:PulseGreen()
				GLOBAL.TheFrontEnd:GetSound():PlaySound("dontstarve/HUD/sanity_up")
			elseif data.newpercent < data.oldpercent then
				self.madbrain:PulseRed()
				GLOBAL.TheFrontEnd:GetSound():PlaySound("dontstarve/HUD/sanity_down")
			end
		end
	end
end

local _setghostmode_old = self.SetGhostMode
function self:SetGhostMode(ghostmode, ...)
	_setghostmode_old(self, ghostmode, ...)
	
	if ghostmode then
		if self.madbrain ~= nil then
			self.madbrain:Hide()
			self.madbrain:StopWarning()
		end
	else
		if self.madbrain ~= nil then
			self.madbrain:Show()
		end
	end
	if self.madness_modetask ~= nil then
		self.madness_modetask:Cancel()
	end
	self.madness_modetask = self.inst:DoStaticTaskInTime(0, ghostmode and OnSetGhostMode or OnSetPlayerMode, self)		
end

I'm not sure if this is everything you need, I've coded badges before and they can be a pain. But hopefully this will help you keep going!

 

ALSO, unrelated thing I noticed while looking around your files. The way you're adding RECIPE_DESC is overriding the entire table, so every other recipe in the game has no description. Unfortunately you have to add each line manually, like you did for NAMES.

Edited by ariadnesGambit
  • Big Ups 1
Link to comment
Share on other sites

Thanks for your input!

56 minutes ago, ariadnesGambit said:

Found the issue! The reason the meter was depleting like that was only a visual thing. The way these meters work is that they have a long animation of them depleting, but they're set to a certain frame based on how full the stat is. That's why it was depleting, it wasn't being set to any specific frame.

In your "madplayer_classified" file, you were missing a bunch of code in your StatusDisplays _ctor hook to listen for certain events and such, but I made it for you (since I was testing around). I also made sure that the badge gets hidden if you're a ghost.

local function OnSetPlayerMode(inst, self)
	self.madness_modetask = nil
	
	if self.owner.replica.madness ~= nil then
		if self.onmadnessdelta == nil then
			self.onmadnessdelta = function(owner, data) self:MadnessDelta(data) end
			self.inst:ListenForEvent("madnessdelta", self.onmadnessdelta, self.owner)
			self:SetMadnessPercent(self.owner.replica.madness:GetPercent())
		end
	end
end

local function OnSetGhostMode(inst, self)
	self.madness_modetask = nil
	
	if self.owner.replica.madness ~= nil then
		if self.onmadnessdelta ~= nil then
			self.inst:RemoveEventCallback("madnessdelta", self.onmadnessdelta, self.owner)
			self.onmadnessdelta = nil
		end
	end
end

function self:SetMadnessPercent(pct)
	self.madbrain:SetPercent(pct, self.owner.replica.madness:Max())
	
	if pct <= 0 then
		self.madbrain:StartWarning()
	else
		self.madbrain:StopWarning()
	end
end

function self:MadnessDelta(data)
	self:SetMadnessPercent(data.newpercent)
	if self.madbrain ~= nil and self.madbrain.MadnessDelta then
		self.madbrain:MadnessDelta(data)
	else
		if not data.overtime then
			if data.newpercent > data.oldpercent then
				self.madbrain:PulseGreen()
				GLOBAL.TheFrontEnd:GetSound():PlaySound("dontstarve/HUD/sanity_up")
			elseif data.newpercent < data.oldpercent then
				self.madbrain:PulseRed()
				GLOBAL.TheFrontEnd:GetSound():PlaySound("dontstarve/HUD/sanity_down")
			end
		end
	end
end

local _setghostmode_old = self.SetGhostMode
function self:SetGhostMode(ghostmode, ...)
	_setghostmode_old(self, ghostmode, ...)
	
	if ghostmode then
		if self.madbrain ~= nil then
			self.madbrain:Hide()
			self.madbrain:StopWarning()
		end
	else
		if self.madbrain ~= nil then
			self.madbrain:Show()
		end
	end
	if self.madness_modetask ~= nil then
		self.madness_modetask:Cancel()
	end
	self.madness_modetask = self.inst:DoStaticTaskInTime(0, ghostmode and OnSetGhostMode or OnSetPlayerMode, self)		
end

I'll give this a try and see what happens
 

 

  • GL Happy 1
Link to comment
Share on other sites

I have some development, 

3 hours ago, ariadnesGambit said:

Found the issue! The reason the meter was depleting like that was only a visual thing. The way these meters work is that they have a long animation of them depleting, but they're set to a certain frame based on how full the stat is. That's why it was depleting, it wasn't being set to any specific frame.

In your "madplayer_classified" file, you were missing a bunch of code in your StatusDisplays _ctor hook to listen for certain events and such, but I made it for you (since I was testing around). I also made sure that the badge gets hidden if you're a ghost.

local function OnSetPlayerMode(inst, self)
	self.madness_modetask = nil
	
	if self.owner.replica.madness ~= nil then
		if self.onmadnessdelta == nil then
			self.onmadnessdelta = function(owner, data) self:MadnessDelta(data) end
			self.inst:ListenForEvent("madnessdelta", self.onmadnessdelta, self.owner)
			self:SetMadnessPercent(self.owner.replica.madness:GetPercent())
		end
	end
end

local function OnSetGhostMode(inst, self)
	self.madness_modetask = nil
	
	if self.owner.replica.madness ~= nil then
		if self.onmadnessdelta ~= nil then
			self.inst:RemoveEventCallback("madnessdelta", self.onmadnessdelta, self.owner)
			self.onmadnessdelta = nil
		end
	end
end

function self:SetMadnessPercent(pct)
	self.madbrain:SetPercent(pct, self.owner.replica.madness:Max())
	
	if pct <= 0 then
		self.madbrain:StartWarning()
	else
		self.madbrain:StopWarning()
	end
end

function self:MadnessDelta(data)
	self:SetMadnessPercent(data.newpercent)
	if self.madbrain ~= nil and self.madbrain.MadnessDelta then
		self.madbrain:MadnessDelta(data)
	else
		if not data.overtime then
			if data.newpercent > data.oldpercent then
				self.madbrain:PulseGreen()
				GLOBAL.TheFrontEnd:GetSound():PlaySound("dontstarve/HUD/sanity_up")
			elseif data.newpercent < data.oldpercent then
				self.madbrain:PulseRed()
				GLOBAL.TheFrontEnd:GetSound():PlaySound("dontstarve/HUD/sanity_down")
			end
		end
	end
end

local _setghostmode_old = self.SetGhostMode
function self:SetGhostMode(ghostmode, ...)
	_setghostmode_old(self, ghostmode, ...)
	
	if ghostmode then
		if self.madbrain ~= nil then
			self.madbrain:Hide()
			self.madbrain:StopWarning()
		end
	else
		if self.madbrain ~= nil then
			self.madbrain:Show()
		end
	end
	if self.madness_modetask ~= nil then
		self.madness_modetask:Cancel()
	end
	self.madness_modetask = self.inst:DoStaticTaskInTime(0, ghostmode and OnSetGhostMode or OnSetPlayerMode, self)		
end

I'm not sure if this is everything you need, I've coded badges before and they can be a pain. But hopefully this will help you keep going!

 

ALSO, unrelated thing I noticed while looking around your files. The way you're adding RECIPE_DESC is overriding the entire table, so every other recipe in the game has no description. Unfortunately you have to add each line manually, like you did for NAMES.

With your help and Lukas' help, I've managed to make it work. I got some adjustments to make in regards to making the badge reset it's value when the player respawns. I tried some things but it I could not figure out how to make the madness go back to max upon revival of the player. Although the rest seems to work perfectly fine. Here's the updated madplayer_classified.lua

madplayer_classified.lua

Edited by AmanitaDST
Link to comment
Share on other sites

26 minutes ago, AmanitaDST said:

I tried some things but it I could not figure out how to make the madness go back to max upon revival of the player.

Put this in your AddPlayerPostInit:

inst:ListenForEvent("makeplayerghost",function(inst, data)
	if inst.components.madness ~= nil then
		inst.components.madness:SetPercent(1, true)
		isnt.components.madness:Pause()
	end
end)

inst:ListenForEvent("respawnfromghost", function(inst)
	if inst.components.madness ~= nil then
		inst.components.madness:Resume()
	end
end)

 

  • Health 1
Link to comment
Share on other sites

8 minutes ago, ariadnesGambit said:

Put this in your AddPlayerPostInit:

inst:ListenForEvent("makeplayerghost",function(inst, data)
	if inst.components.madness ~= nil then
		inst.components.madness:SetPercent(1, true)
		isnt.components.madness:Pause()
	end
end)

inst:ListenForEvent("respawnfromghost", function(inst)
	if inst.components.madness ~= nil then
		inst.components.madness:Resume()
	end
end)

 

Thank you so much! That does make perfect sense. I'm slowly learning the syntax of lua/dst. Thanks a lot again!

  • GL Happy 1
Link to comment
Share on other sites

4 minutes ago, AmanitaDST said:

Thank you so much! That does make perfect sense. I'm slowly learning the syntax of lua/dst. Thanks a lot again!

No problem! Since you're making something really similar to an existing feature, it really helps to look at what Klei does, though it's not always easy to track it down. You did a pretty good job with your original code, now you're just missing some pieces here and there. I went through the same process when making my own badge and it took me days of work, so I'm happy to make someone else's job easier!

  • Big Ups 1
Link to comment
Share on other sites

21 minutes ago, ariadnesGambit said:

No problem! Since you're making something really similar to an existing feature, it really helps to look at what Klei does, though it's not always easy to track it down. You did a pretty good job with your original code, now you're just missing some pieces here and there. I went through the same process when making my own badge and it took me days of work, so I'm happy to make someone else's job easier!

I unfortunately ran into and error adding it in, I think I might've done smt wrong but here it is. This is inside modmain:
 

AddPlayerPostInit(function(inst)
	if not GLOBAL.TheWorld.ismastersim then
	  return
	end
  
	if inst.components.reader == nil then
		inst:AddComponent("reader")
	end

	inst:AddComponent("madness")
	
	if inst.components.itemaffinity == nil then
		inst:AddComponent("itemaffinity")
	end

	inst.components.itemaffinity:AddAffinity("cursedstone", nil, -TUNING.DAPPERNESS_TINY, 1)
	inst.components.itemaffinity:AddAffinity("cursedcrystal", nil, -TUNING.DAPPERNESS_SMALL, 1)
	inst.components.itemaffinity:AddAffinity("cursedgem", nil, -TUNING.DAPPERNESS_SMALL, 1)
	inst.components.itemaffinity:AddAffinity("cursedorb", nil, -TUNING.DAPPERNESS_HUGE, 1)

	inst:ListenForEvent("makeplayerghost",function(inst, data)
		if inst.components.madness ~= nil then
			inst.components.madness:SetPercent(1, true)
			isnt.components.madness:Pause()
		end
	end)
	
	inst:ListenForEvent("respawnfromghost", function(inst)
		if inst.components.madness ~= nil then
			inst.components.madness:Resume()
		end
	end)
end)

image.png.45825d6a05fa021a9dd34859dc99be5f.png

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