Jump to content

Can someone post a simplest text widget?


SenL

Recommended Posts

I have a mod that has "charges" (1 charge, 2 charges, etc) and I would like this # displayed on screen as a text (for now).

I tried studying rezecib's "Combined Status" mod but it's far too advanced for me.

Can someone post a simplest text widget?

Thanks.

Link to comment
Share on other sites

After 2 hours I got it to work.
Borrowing "minibadge" widget from receib's "Combined Status" mod, I patched together these.

modmain.lua:

Quote

local require = GLOBAL.require
local Widget = require('widgets/widget')
local Image = require('widgets/image')
local Text = require('widgets/text')
local Minibadge = require("widgets/minibadge")

...

local function StatusPostConstruct(self)
    local toDisplay = 0

    self.myModChargeCount = self:AddChild(Minibadge("myModChargeCount", self.owner))
    self.myModChargeCount:SetPosition(0,0)
    self.myModChargeCount.bg:SetScale(.55, .43, 1)
    self.myModChargeCount.num:SetString(toDisplay)
    self.prismaticamuletNovaChargeCount:Hide()

    local function UpdateCount(data)
        if data ~= nil then
            if data["count"] ~= nil then
                toDisplay = data["count"]
            else
                toDisplay = "-3"
                print ("data[\"count\"] is nil")
            end
        else
            toDisplay = "-2"
            print ("data is nil")
        end
        
        self.myModChargeCount.num:SetString(toDisplay)
    end

    self.inst:ListenForEvent("myModNewChargeCount", function(inst, data) UpdateCount(data) end, GLOBAL.GetWorld())
    self.inst:ListenForEvent("myModEquipped", function(inst) self.myModChargeCount:Show() end, GLOBAL.GetWorld())
    self.inst:ListenForEvent("myModUnequipped", function(inst) self.myModChargeCount:Hide() end, GLOBAL.GetWorld())
end
AddClassPostConstruct("widgets/statusdisplays", StatusPostConstruct)

 

Now for mymod.lua - it needs to push 3 events. 1) when charge changes, 2) when item is equipped and 3) when item is unequipped. However I'm just showing the first one.

Quote

GetWorld():PushEvent("myModNewChargeCount", {count = inst.ChargeCount})

 

Now how do I move this widget...

Link to comment
Share on other sites

Right. SetPosition. I got it to move to where I want.

However when I put SetPosition(0,0) how come it's not at the center of screen but more like upper right-ish... is it because I use "widgets/statusdisplays"?

Also is it correct to use this widget (statusDisplays)?

 

Edit: Found two issues. The findMyMod() crashes because of a bug. I had it fixed to return nil if it couldn't find the amulet.
Second issue is larger. When the game starts and findMyMod() is nil (the amulet hasn't been crafted or spawned yet) then the widget will be broken. Broken because of the listenForEvent is on the amulet and if the amulet is nil then it's not listening for events. How do I fix this...

Quote

    myMod:ListenForEvent("myModNewChargeCount", function(inst, data) UpdateCount(data) end, myMod)
    myMod:ListenForEvent("myModEquipped", function(inst) self.myModChargeCount:Show() end, myMod)
    myMod:ListenForEvent("myModUnequipped", function(inst) self.myModChargeCount:Hide() end, myMod)

It feels like the ListenForEvent should be on self (the widget) ... but how does the amulet send the PushEvent to this widget?

Edit: Fixed. Did this, is this correct?

Quote

    self.inst:ListenForEvent("myModNewChargeCount", function(inst, data) UpdateCount(data) end, GLOBAL.GetWorld())
    self:inst:ListenForEvent("myModEquipped", function(inst) self.myModChargeCount:Show() end, GLOBAL.GetWorld())
    self:inst:ListenForEvent("myModUnequipped", function(inst) self.myModChargeCount:Hide() end, GLOBAL.GetWorld())

And on the myMod.lua:

Quote

GetWorld():PushEvent("myModEquipped")
...etc

 

Link to comment
Share on other sites

On 6/16/2022 at 10:45 AM, SenL said:

Did this, is this correct?

If it works it's correct! Usually it's done via other method, see visor_over widged (wagstaff armor), it uses UpdateState(). 
Then you would only need to create the ListentoEvent "NewCharge" when the item is equipped and remove it when the item is unequipped.
 

On 6/16/2022 at 10:45 AM, SenL said:

However when I put SetPosition(0,0) how come it's not at the center of screen but more like upper right-ish

That's exactly how it works.

image.png.fc5f2172ac5441ff0e82d073a7873bfa.png

To get the screen's center, you can use:
 

local scr_w, scr_h = GLOBAL.TheSim:GetScreenSize()

-- And use SetPosition with scr_w and scr_h divided by 2 (half of the screen)
self.myModChargeCount:SetPosition(scr_w/2, scr_h/2)

 

Link to comment
Share on other sites

12 hours ago, Leonidas IV said:

Usually it's done via other method, see visor_over widged (wagstaff armor), it uses UpdateState(). 

I'm not sure how. Do I still do this?

Quote

AddClassPostConstruct("widgets/statusdisplays", StatusPostConstruct)

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...