Updating Widget Label

Hi everyone,

I am trying to create a game where you can purchase upgrades for your units. To do this I have created a widget that will increase the level of the units, reduce the money the player has by the cost and increase the cost for the next upgrade. My problem is that I am unable to find a way to update the label of the widget automatically to show the new cost without reloading the scene.

Here is my code:

local function increaseWarriorLvl ( event ) if ( "ended" == event.phase ) then if mydata.money \>= mydata.warriorCost then mydata.money = mydata.money - mydata.warriorCost mydata.warriorCost = mydata.warriorCost + 10 mydata.warriorLv = mydata.warriorLv + 1 --warriorUpgrade:setLabel( "Upgrade Warrior " .. "$" .. mydata.warriorCost ) end end end local warriorUpgrade = widget.newButton( { label = "Upgrade Warrior " .. "$" .. mydata.warriorCost, onEvent = increaseWarriorLvl, emboss = false, shape = "roundedRect", width = 170, height = 50, cornerRadius = 2, fillColor = { default={0,0,0,0}, over={0,0,0,0} }, strokeColor = {default={1,1,1,1}, over={1,1,1,1} }, strokeWidth = 4, } ) sceneGroup:insert(warriorUpgrade) warriorUpgrade.x = cenx warriorUpgrade.y = ( toty/12 )\*3 warriorUpgrade:setLabel( "Upgrade Warrior " .. "$" .. mydata.warriorCost )

Whatever help is greatly appreciated.

Cheers,

Daniel

Have you tried the :setLabel() API?

https://docs.coronalabs.com/api/type/ButtonWidget/setLabel.html

Rob

Hi Rob,

I have tried it on both lines 62 (comment out currently) and 84. When on line 62 it does not work and comes up with the error: trying to index nil value, warriorUpgrade does not exist and on line 84 it only updates when the scene is exited and re-entered.

Hi Daniel,

The reason it doesn’t work on line 62 is that, because of Lua scope rules, Lua doesn’t know about the local button object “warriorUpgrade” until line 67 where you’ve declared it. There are, of course, ways to handle the scoping of this… if you’d like, we can point you to a tutorial on Lua scope.

Take care,

Brent

Hey Brent,

I understand that warriorUpgrade has not been defined at line 62, so I would love if you could point me in the right direction for fixing this. I assume that if I could have it defined by that point then I when the “increaseWarriorLv1” function runs the label will update.

Cheers,

Daniel

At the top of your module do:
 

local warriorUpgrade

Then later where you create the button, leave off the “local”

warriorUpgrade = widget.newButton(

This will pre-declare “warriorUpgrade” so Lua will assign a memory address to the variable and later you populate it with the button.

Rob

That got it working, thanks for all the help.

Have you tried the :setLabel() API?

https://docs.coronalabs.com/api/type/ButtonWidget/setLabel.html

Rob

Hi Rob,

I have tried it on both lines 62 (comment out currently) and 84. When on line 62 it does not work and comes up with the error: trying to index nil value, warriorUpgrade does not exist and on line 84 it only updates when the scene is exited and re-entered.

Hi Daniel,

The reason it doesn’t work on line 62 is that, because of Lua scope rules, Lua doesn’t know about the local button object “warriorUpgrade” until line 67 where you’ve declared it. There are, of course, ways to handle the scoping of this… if you’d like, we can point you to a tutorial on Lua scope.

Take care,

Brent

Hey Brent,

I understand that warriorUpgrade has not been defined at line 62, so I would love if you could point me in the right direction for fixing this. I assume that if I could have it defined by that point then I when the “increaseWarriorLv1” function runs the label will update.

Cheers,

Daniel

At the top of your module do:
 

local warriorUpgrade

Then later where you create the button, leave off the “local”

warriorUpgrade = widget.newButton(

This will pre-declare “warriorUpgrade” so Lua will assign a memory address to the variable and later you populate it with the button.

Rob

That got it working, thanks for all the help.