Jump to content

Widget doesn't pop up

Recommended Posts

Hello fellow modders!



So I want a widget to pop up and stay when a specific condition is fulfilled. My code in modmain is:

function GLOBAL.shadowcounter(inst)	inst.SpawnChance = math.random()		if inst.SpawnChance < 0.5 then			local sw = GLOBAL.require "widgets/shadowwidget"			AddClassPostConstruct("widgets/statusdisplays", function(self)			self.shadow = self:AddChild(sw(self.owner))			self.shadow:SetPosition(0, -200, 0)			end)		else 			inst.components.health:DoDelta(50)		endend

with this in prefabs/character.lua

	inst:ListenForEvent("attacked", shadowcounter)

So basically I want the widget to pop up when I get attacked. (the math.random is just there to generate a decision-making-scenario) My game does not crash when testing this. It even calls the right function. But somehow the widget does not pop up. The adding of the 50 health works fine. Does anyone know where my mistake is?

Link to comment
Share on other sites

AddClassPostConstruct sets up a function to run and append code after statusdisplays was constructed.


If you execute the function shadowcounter during the game, statusdisplays was already constructed and displayed, so, you aren't going to have your own counter displayed.


You have to use AddClassPostConstruct when the modmain loads.


Then you are going to have to set up a net_bool for the players to tell them to pop up their shadow counter. It will be 0 on the net for them when told not to pop it up, 1 when told.


But you have values to display, right? Then the clients need to know these values, and you will have to send them via a netvar.


So, what does shadowcounter do? What does it show?

Link to comment
Share on other sites

Thank you for the response! Okay for better understanding here is my idea: I want a widget that shows the player through the grafic (so without showing the values) how high the value of a certain variable is. Basically it should be a custom bar that fills up when certain events happen (I haven't thought of those events yet). I want a structure like this:

if value < 2then show widget1else if value 2:4then show widget2else if value 4:6then show widget3...

With widget1, widget2, widget3 etc being parts of the full bar.


Edit: Since my character is called Zoey the possessed I want a possession system that triggers a transformation after a certain value. The widgets shall show the player how far in the possession the character is.


Edit2: So after what I understand I can't use AddClassPostConstruct. Is there another way to add the widget after the statusdisplays got constructed?

Edited by LesSV
Link to comment
Share on other sites

Here I add the widget if the owner is a certain character.

The widget updates with a certain value when it receives a certain event.


That event gets passed when the netvar _scount gets its value changed.

When you get attacked, I change it, then the widget updates by using the new netvar value.


Apply the same logic for images.

Also, before changing images, you can check if the value is 0 or under 0, and then make self:Hide(), to hide a widget, and self:Show(), to show a widget.


Link to comment
Share on other sites


Thanks for your help! Your code worked like a charm. Then I tried to modify it so that it hides and shows widget as you suggested. But I can't get it to work. My idea was to take the whole Hide/Show stuff to the Update function of the ShadowWidget:

local Widget = require "widgets/widget"local Text = require "widgets/text"ShadowWidget = Class(Widget, function(self, owner)    	Widget._ctor(self, "ShadowWidget")    	self.owner = owner    	self.base_scale = 0.5    	self:SetScale(self.base_scale, self.base_scale, self.base_scale)    	self:SetPosition(200, 200, 200)    	self.image0 = self:AddChild(Image("images/shadowhorde.xml", "shadowhorde.tex"))	self.image1 = self:AddChild(Image("images/sh1.xml", "sh1.tex"))	self.image2 = self:AddChild(Image("images/sh2.xml", "sh2.tex"))	self.image3 = self:AddChild(Image("images/sh3.xml", "sh3.tex"))	self.text = self:AddChild(Text(NUMBERFONT, 40 / self.base_scale))		self.image0:Hide()	self.image1:Hide()	self.image2:Hide()	self.image3:Hide()end)function ShadowWidget:Update(val)	self.text:SetString(val)	if val == "1" then		self.image0:Hide()		self.image1:Show()		self.image2:Hide()		self.image3:Hide()	elseif val == "2" then		self.image0:Hide()		self.image1:Hide()		self.image2:Show()		self.image3:Hide()	elseif val == "3" then		self.image0:Hide()		self.image1:Hide()		self.image2:Hide()		self.image3:Show()	else		self.image0:Show()		self.image1:Hide()		self.image2:Hide()		self.image3:Hide()	end	endreturn ShadowWidget

But with this I always get the self.text and the self.image0. So I thought it has something to do with the syntax of the if-structure. But I just can't figure it out. 


Link to comment
Share on other sites

The text is always shown, and for values 0, 4, 5 , 6, ...image0 shows, as the if/elseifs only covers 1, 2, 3.


But the Show(), Hide() should work, I tested it with texts.


Try using


instead of show and hide?

Edited by DarkXero
Link to comment
Share on other sites

I decided to try something a bit different.


Some comments to have while checking the mod:

1) Removed AddClassPostConstruct, moved widget generation into shadowness replica2) Created shadowness to handle shadow values 0, 1, 2, 33) Created shadowness replica to display shadow values for clients4) ShadowWidget now has only one image member, self.image5) self.image is a widget child of self.display6) self.image gets modified with SetTexture to get different images7) self.image gets modified when the shadowdirty event gets pushed8) This occurs when the shadowness replica has its netvariable, holding the shadow values, changed9) The trigger for the replica change comes from editing the component server side10) The shadowwidget gets attached to the player hud via the replica


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

  • Create New...