SenL Posted June 14, 2022 Share Posted June 14, 2022 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 More sharing options...
Leonidas IV Posted June 14, 2022 Share Posted June 14, 2022 DebugMenuScreen.lua it's quite simple. But screens and widgets are really complex. Link to comment Share on other sites More sharing options...
SenL Posted June 15, 2022 Author Share Posted June 15, 2022 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 More sharing options...
Leonidas IV Posted June 16, 2022 Share Posted June 16, 2022 21 hours ago, SenL said: Now how do I move this widget... SetPosition Link to comment Share on other sites More sharing options...
SenL Posted June 16, 2022 Author Share Posted June 16, 2022 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 More sharing options...
Leonidas IV Posted June 17, 2022 Share Posted June 17, 2022 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. 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 More sharing options...
SenL Posted June 18, 2022 Author Share Posted June 18, 2022 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 More sharing options...
Leonidas IV Posted June 18, 2022 Share Posted June 18, 2022 13 hours ago, SenL said: I'm not sure how. Do I still do this? Yeah, I think. If the statusdisplays widget already has the UpdateState() method, you just add your part using a hook (As I taught you in the trap's push event) Link to comment Share on other sites More sharing options...
Recommended Posts
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.