How do I refresh data on a scene immediately

I have just implemented adbuddiz reward video in my app and it works as expected but I have a slight challenge.

After watching the video, it triggers the “didComplete” event where it adds the needed value into a json file.

The challenge now is the value does not automatically reflect on the screen except I go out of the scene and go back before the new value shows.

Is there a way to refresh the screen so the new value appears immediately?

**Note: This all happens on a button click**

 local savecount = Lib.getSaveValue("savecount") -- Events (rewarded video) local function listenerRewardedVideo( event ) if event.value == "didFetch" then print( "didFetch" ) end if event.value == "didComplete" then print( "didComplete" ) -- Reward user here print ("savecount") savecount = savecount + 1 Lib.setSaveValue("savecount", savecount, true); local alert = native.showAlert("Successful","Count saved successfully!", {"OK"}) end if event.value == "didNotComplete" then print( "didNotComplete" ) end if event.value == "didFail" then print( "didFail - " .. event.detail ) end end Runtime:addEventListener( "AdBuddizRewardedVideoEvent", listenerRewardedVideo )

This displays the value on the menu scene

 label\_saveme = display.newText({parent=uiGroup, text=savecount, x=0, y=0, font=native.systemFont, fontSize=24}) label\_saveme.xScale = 0.92 label\_saveme.x = button\_buysaveme.x + 22 label\_saveme.y = button\_buysaveme.y + 6 label\_saveme:setFillColor(255/255, 255/255, 255/255) label\_saveme.alpha = 0.4 uiGroup:insert(label\_saveme)

Thanks

There are multiple ways to attack this issue. The simplest one is to make the “label_saveme” display object available to main.lua. If you follow the non-global global data table idea presented in: http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

Then in main.lua you can:

local myData = require("mydata")

In menu.lua you can :

local myData = require("mydata")

when you create the display object:

 myData.label\_saveme = display.newText({parent=uiGroup, text=savecount, x=0, y=0, font=native.systemFont, fontSize=24}) myData.label\_saveme.xScale = 0.92 myData.label\_saveme.x = button\_buysaveme.x + 22 myData.label\_saveme.y = button\_buysaveme.y + 6 myData.label\_saveme:setFillColor(255/255, 255/255, 255/255) myData.label\_saveme.alpha = 0.4 uiGroup:insert(myData.label\_saveme) --\<----- btw, you don't need to do this since you added it to uiGroup in its creator above.

now back in main.lua your listener can be updated to:

if event.value == "didComplete" then print( "didComplete" ) -- Reward user here print ("savecount") savecount = savecount + 1 Lib.setSaveValue("savecount", savecount, true); myData.label\_saveme.text = tostring( savecount ) --\<----- added line local alert = native.showAlert("Successful","Count saved successfully!", {"OK"}) end

Rob

Hi Rob

I have just tried your suggestion but when i test on my phone, none of the buttons are responding.  I can see in logcat that its responding to touch but nothing moves on screen.

Thanks

In mydata.lua file, I only put this according to the article u sent:

–my global space

local M = {}

return M

 

Is this correct?

Yes, that’s all you need to start.

You’re going to have to provide some updated code so we can see what’s going on. Are you getting any error messages?

Please post your code in between

[lua] and [/lua]

tags or use the blue <> button in the formatting bar.

Thanks

Rob

never mind Rob, my fault :frowning:

Found where i missed a line and all working like a charm. no way i would have thought of this method :frowning:

As always, you the best.

You almost never need to send all your code for issues like this. Your listener function where you’re adding the score and your creation code where you’re creating the object would have been plenty to start with. But I’m glad you solved it.

Rob

There are multiple ways to attack this issue. The simplest one is to make the “label_saveme” display object available to main.lua. If you follow the non-global global data table idea presented in: http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

Then in main.lua you can:

local myData = require("mydata")

In menu.lua you can :

local myData = require("mydata")

when you create the display object:

 myData.label\_saveme = display.newText({parent=uiGroup, text=savecount, x=0, y=0, font=native.systemFont, fontSize=24}) myData.label\_saveme.xScale = 0.92 myData.label\_saveme.x = button\_buysaveme.x + 22 myData.label\_saveme.y = button\_buysaveme.y + 6 myData.label\_saveme:setFillColor(255/255, 255/255, 255/255) myData.label\_saveme.alpha = 0.4 uiGroup:insert(myData.label\_saveme) --\<----- btw, you don't need to do this since you added it to uiGroup in its creator above.

now back in main.lua your listener can be updated to:

if event.value == "didComplete" then print( "didComplete" ) -- Reward user here print ("savecount") savecount = savecount + 1 Lib.setSaveValue("savecount", savecount, true); myData.label\_saveme.text = tostring( savecount ) --\<----- added line local alert = native.showAlert("Successful","Count saved successfully!", {"OK"}) end

Rob

Hi Rob

I have just tried your suggestion but when i test on my phone, none of the buttons are responding.  I can see in logcat that its responding to touch but nothing moves on screen.

Thanks

In mydata.lua file, I only put this according to the article u sent:

–my global space

local M = {}

return M

 

Is this correct?

Yes, that’s all you need to start.

You’re going to have to provide some updated code so we can see what’s going on. Are you getting any error messages?

Please post your code in between

[lua] and [/lua]

tags or use the blue <> button in the formatting bar.

Thanks

Rob

never mind Rob, my fault :frowning:

Found where i missed a line and all working like a charm. no way i would have thought of this method :frowning:

As always, you the best.

You almost never need to send all your code for issues like this. Your listener function where you’re adding the score and your creation code where you’re creating the object would have been plenty to start with. But I’m glad you solved it.

Rob