Jump to content

Putting text on the UI


Recommended Posts

I'm trying to work out how to deal with custom UI elements. StatusPlus puts numbers and bars on the screen, I looked at how that worked and saw they used the Text function.

 

What I want to do is put XP numbers on the screen but the best I came up with just made the game crash. Any advice would be a big help, I don't know a lot about LUA.

 

-

 


require = GLOBAL.require

local Text = require "widgets/text"


 

expText = inst:AddChild(Text(GLOBAL.NUMBERFONT, 28, "1/5"))

expTex.Text:SetVAlign(5)

expTex.Text:SetHAlign(5)

Link to comment
Share on other sites

Well typos on the last two lines but the game exploding is a bigger issue :(

 

Could you post your log?

 

Also, here is the contents of the text widget. Take a look at it, it may help you.

 

local Widget = require "widgets/widget"local Text = Class(Widget, function(self, font, size, text)    Widget._ctor(self, "Text")       self.inst.entity:AddTextWidget()        self.inst.TextWidget:SetFont(font)    self.inst.TextWidget:SetSize(size)    if text then        self:SetString( text )    endend)function Text:__tostring()    return string.format("%s - %s", self.name, self.string or "")endfunction Text:SetColour(r,g,b,a)    if type(r) == "number" then        self.inst.TextWidget:SetColour(r, g, b, a)    else        self.inst.TextWidget:SetColour(r[1], r[2], r[3], r[4])    endendfunction Text:SetHorizontalSqueeze( squeeze )    self.inst.TextWidget:SetHorizontalSqueeze(squeeze)endfunction Text:SetAlpha(a)    self.inst.TextWidget:SetColour(1,1,1, a)endfunction Text:SetFont(font)    self.inst.TextWidget:SetFont(font)endfunction Text:SetSize(sz)    self.inst.TextWidget:SetSize(sz)endfunction Text:SetRegionSize(w,h)    self.inst.TextWidget:SetRegionSize(w,h)endfunction Text:GetRegionSize()    return self.inst.TextWidget:GetRegionSize()endfunction Text:SetString(str)    self.string = str    self.inst.TextWidget:SetString(str or "")endfunction Text:GetString()    --print("Text:GetString()", self.inst.TextWidget:GetString())    return self.inst.TextWidget:GetString() or ""endfunction Text:SetVAlign(anchor)    self.inst.TextWidget:SetVAnchor(anchor)endfunction Text:SetHAlign(anchor)    self.inst.TextWidget:SetHAnchor(anchor)endfunction Text:EnableWordWrap(enable)    self.inst.TextWidget:EnableWordWrap(enable)endreturn Text

Link to comment
Share on other sites

Found a few mistakes, so now it's:

 

local Text = require "widgets/text"

 

inst.expText = inst:AddChild(Text(NUMBERFONT, 28))
inst.expText:SetString("0123456")
inst.expText:SetHAlign(ANCHOR_MIDDLE)
inst.expText:SetPosition(1.0, 1.0, 0)
inst.expText:SetScale(1,1,1)
inst.expText:Show()
 
Still not happy. Attempt to index field entity.
Link to comment
Share on other sites

 

Found a few mistakes, so now it's:

 

local Text = require "widgets/text"

 

inst.expText = inst:AddChild(Text(NUMBERFONT, 28))
inst.expText:SetString("0123456")
inst.expText:SetHAlign(ANCHOR_MIDDLE)
inst.expText:SetPosition(1.0, 1.0, 0)
inst.expText:SetScale(1,1,1)
inst.expText:Show()
 
Still not happy. Attempt to index field entity.

 

 

I'm not sure without seeing the entire log, but I think NUMBERFONT and ANCHOR_MIDDLE need to be GLOBAL.NUMBERFONT and GLOBAL.ANCHOR_MIDDLE.

 

Link to comment
Share on other sites

I have been adding to the modmain.lua of the Puppy Princess mod if that context. Don't really understand inst or self and LUA is new to me, it feels like I am making some fundamental mistakes but I can't work out what :(

 

http://steamcommunity.com/sharedfiles/filedetails/?id=244600638

 

Current:

 

local function AddExpBar(inst)
mushaExpBar = inst:AddChild(Text(NUMBERFONT, 28))
mushaExpBar:SetText("12345")
mushaExpBar:SetHAnchor(1)
mushaExpBar:SetVAnchor(1)
mushaExpBar:SetScale(1,1,1)
mushaExpBar:Show()
end
 
AddPrefabPostInit("musha", AddExpBar)
 
----
 
scripts/mods.lua(17,1) error calling PrefabPostInit: musha in mod workshop-244600638 (Puppy Princess): 
...pps/common/dont_starve/data/scripts/widgets/text.lua:8: calling 'SetFont' on bad self (string expected, got nil)
LUA ERROR stack traceback:
        =[C] in function 'SetFont'
        D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/widgets/text.lua(8,1) in function '_ctor'
        D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/class.lua(98,1) in function 'Text'
        D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/../mods/workshop-244600638/modmain.lua(312,1)
        =(tail call) ?
        =[C] in function 'xpcall'
        D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/mods.lua(15,1) in function 'mod'
        D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/mainfunctions.lua(133,1)
        =[C] in function 'SpawnPrefab'
        D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/mainfunctions.lua(156,1) in function 'SpawnPrefab'
        D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/gamelogic.lua(418,1) in function 'PopulateWorld'
...
        =[C] in function 'SetPersistentString'
        D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/saveindex.lua(83,1) in function 'Save'
        D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/saveindex.lua(660,1)
        =[C] in function 'SetPersistentString'
        D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/saveindex.lua(663,1) in function 'OnGenerateNewWorld'
        D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/gamelogic.lua(988,1) in function 'cb'
        D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/screens/worldgenscreen.lua(168,1) in function 'cb'
        D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/frontend.lua(398,1) in function 'DoFadingUpdate'
        D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/frontend.lua(450,1) in function 'Update'
        D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/update.lua(46,1)
scripts/mods.lua(215,1) Disabling workshop-244600638 (Puppy Princess) because it had an error.
scripts/frontend.lua(712,1) SCRIPT ERROR! Showing error screen
scripts/gamelogic.lua(445,1) Loading Nav Grid
scripts/saveindex.lua(224,1) LoadSavedSeasonData CB
 
Link to comment
Share on other sites

Sorry debugman18, I should have tried your recommendation of adding global earlier. New crash with it.

scripts/mods.lua(17,1) error calling PrefabPostInit: musha in mod workshop-244600638 (Puppy Princess):

...pps/common/dont_starve/data/scripts/entityscript.lua:418: attempt to index field 'entity' (a nil value)

LUA ERROR stack traceback:

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/entityscript.lua(418,1) in function 'AddChild'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/../mods/workshop-244600638/modmain.lua(312,1)

=(tail call) ?

=[C] in function 'xpcall'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/mods.lua(15,1) in function 'mod'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/mainfunctions.lua(133,1)

=[C] in function 'SpawnPrefab'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/mainfunctions.lua(156,1) in function 'SpawnPrefab'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/gamelogic.lua(418,1) in function 'PopulateWorld'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/gamelogic.lua(788,1) in function 'DoInitGame'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/gamelogic.lua(979,1) in function 'cb'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/saveindex.lua(632,1)

=[C] in function 'SetPersistentString'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/saveindex.lua(83,1) in function 'Save'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/saveindex.lua(660,1)

=[C] in function 'SetPersistentString'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/saveindex.lua(663,1) in function 'OnGenerateNewWorld'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/gamelogic.lua(988,1) in function 'cb'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/screens/worldgenscreen.lua(168,1) in function 'cb'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/frontend.lua(398,1) in function 'DoFadingUpdate'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/frontend.lua(450,1) in function 'Update'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/update.lua(46,1)

scripts/mods.lua(215,1) Disabling workshop-244600638 (Puppy Princess) because it had an error.

scripts/frontend.lua(712,1) SCRIPT ERROR! Showing error screen

scripts/gamelogic.lua(445,1) Loading Nav Grid

scripts/saveindex.lua(224,1) LoadSavedSeasonData CB

Force aborting...

Link to comment
Share on other sites

Sorry debugman18, I should have tried your recommendation of adding global earlier. New crash with it.

scripts/mods.lua(17,1) error calling PrefabPostInit: musha in mod workshop-244600638 (Puppy Princess):

...pps/common/dont_starve/data/scripts/entityscript.lua:418: attempt to index field 'entity' (a nil value)

LUA ERROR stack traceback:

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/entityscript.lua(418,1) in function 'AddChild'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/../mods/workshop-244600638/modmain.lua(312,1)

=(tail call) ?

=[C] in function 'xpcall'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/mods.lua(15,1) in function 'mod'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/mainfunctions.lua(133,1)

=[C] in function 'SpawnPrefab'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/mainfunctions.lua(156,1) in function 'SpawnPrefab'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/gamelogic.lua(418,1) in function 'PopulateWorld'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/gamelogic.lua(788,1) in function 'DoInitGame'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/gamelogic.lua(979,1) in function 'cb'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/saveindex.lua(632,1)

=[C] in function 'SetPersistentString'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/saveindex.lua(83,1) in function 'Save'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/saveindex.lua(660,1)

=[C] in function 'SetPersistentString'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/saveindex.lua(663,1) in function 'OnGenerateNewWorld'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/gamelogic.lua(988,1) in function 'cb'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/screens/worldgenscreen.lua(168,1) in function 'cb'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/frontend.lua(398,1) in function 'DoFadingUpdate'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/frontend.lua(450,1) in function 'Update'

D:/Program Files (x86)/Steam/steamapps/common/dont_starve/data/scripts/update.lua(46,1)

scripts/mods.lua(215,1) Disabling workshop-244600638 (Puppy Princess) because it had an error.

scripts/frontend.lua(712,1) SCRIPT ERROR! Showing error screen

scripts/gamelogic.lua(445,1) Loading Nav Grid

scripts/saveindex.lua(224,1) LoadSavedSeasonData CB

Force aborting...

 

Could you please post your modmain.lua?

 

Link to comment
Share on other sites

Floppycat, what code are you basing this technique on? Text is a UI widget, not an entity, so it can't be added as a child to an entity. That's probably the error you're getting, but I don't have access to the Don't Starve code right now to be sure.

I know there is a component that adds text above entities; I'd look into that (see talker.lua [or whatever component adds the speech text above players, I forget the name]).

Link to comment
Share on other sites

squeek I think I understand. The mods I looked at first statusplus had them attached to other UI elements, the games default status widgets. We're already using talker but it overlaps with other speech.

 

The interesting one was NTools which used AddSimPostInit to make child widgets. 

 

 

local function AddN_Tools( inst )
 
GLOBAL.RunScript("consolecommands")
 
inst:DoTaskInTime( 0.001, function() 
 
local N_Tools = require "widgets/n_tools"
 
local controls = inst.HUD.controls
 
menu_switch = controls:AddChild(Widget("menu_switch"))
 
    menu_switch:SetScaleMode(2)
    menu_switch:SetHAnchor(1)
    menu_switch:SetVAnchor(1)
    menu_switch:SetMaxPropUpscale(1.25)
    menu_switch:SetScale(1,1,1)
 
etc...
 
end
 
AddSimPostInit( AddN_Tools )

 

 

Fiddling with inst.HUD.controls I have it working:

 

 
local function AddExpBar(inst)
inst:DoTaskInTime( 0.5, function() 
local controls = inst.HUD.controls
mushaExpBar = controls:AddChild(Text(GLOBAL.NUMBERFONT, 28, "123"))
mushaExpBar:SetHAnchor(GLOBAL.ANCHOR_MIDDLE)
mushaExpBar:SetVAnchor(0.4)
mushaExpBar:SetScale(1,.78,1)
mushaExpBar:Show()
mushaExpBar:SetString("456")
end)
end

 

AddPrefabPostInit("musha", AddExpBar)

 

 Then mushaExpBar:SetString() works to change the text. Progress!

Link to comment
Share on other sites

Just trying to work out how to update using events.
 
In the player prefab:
 
inst:PushEvent("expUpdate", {updinfo = 2})

 

 

 
In modmain.lua

local function labelMusha(inst)
local xpLabel = inst.entity:AddLabel()
inst.Label:SetFontSize(20)
inst.Label:SetFont("stint-ucr")
inst.Label:SetPos(0, 3, 0)
inst.Label:SetText("1")
inst.Label:SetColour(255/255,255/255,0/255)
inst.Label:Enable(true)
print("set label")
end
 
local function updateLabelMusha(inst, data)
inst.Label:SetText(data.updinfo)
end
 
inst:DoTaskInTime( 0.5, function() 
AddPrefabPostInit("musha", labelMusha)
inst:ListenForEvent("expUpdate", updateLabelMusha(inst, data))
end)

 

 

labelMusha used to work before I added updateLabelMusha.
 
...t_starve/data/../mods/workshop-244600638/modmain.lua:317: attempt to call global 'updateLabelMusha' (a nil value)
 
Don't know if inst.Label:SetText is working with the created label, if the ListenForEvent is wrong or whatever else it might not like.
Link to comment
Share on other sites

The error is from calling updateLabelMusha and sending ListenForEvent the return of the function rather than sending it the updateLabelMusha function itself. But, there are other things to take care of:

Where is that inst:DoTaskInTime line? What is inst in that scope?

You shouldn't put AddPrefabPostInit inside a task, that's unnecessary at best (and likely non-functional). Put it straight in modmain. The DoTaskInTime itself seems unnecessary as well; you can put the ListenForEvent inside the labelMusha function instead.

 

-- in modmain.lualocal function updateLabelMusha(inst, data)    inst.Label:SetText(data.updinfo)endlocal function labelMusha(inst)    local xpLabel = inst.entity:AddLabel()    inst.Label:SetFontSize(20)    inst.Label:SetFont("stint-ucr")    inst.Label:SetPos(0, 3, 0)    inst.Label:SetText("1")    inst.Label:SetColour(255/255,255/255,0/255)    inst.Label:Enable(true)    print("set label")        inst:ListenForEvent("expUpdate", updateLabelMusha)endAddPrefabPostInit("musha", labelMusha)
EDIT: Also, if "musha" is a custom prefab that you made, then all of this is unnecessary and you should just put this stuff in your prefab files. There's no reason to use AddPrefabPostInit on a custom prefab (put the code in the prefab's fn function).
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...