How to reference .ccscene text field from .lua file?

I’m hoping you can help me bridge my knowledge gap… here’s the scenario:

  • I create a scene in Composer GUI. In the scene I add a text field, for example named txtRevenueTotal

  • the .ccscene file is generated (scene1.ccscene) as is the scene template (scene1.lua)   (these are implicitly paired as they share the same name, right?)

  • my intent is to pull in remote data via json, insert into a SQLite3 database and …this is where I need your help… update the visible text field on the screen with the new revenue total, but how?

My instinct is to do the hard work in the “did” part of scene:show and update the text field from there, but how to do I reference scene1.ccscene->txtRevenueTotal.text?

Thanks in advance for any guidance you can give me on this.

Ben

Make sure the name of the text (inside Composer GUI) is txtRevenueTotal and then inside scene:create() do this:

local txtRevTot = self:getObjectByTag( "txtRevenueTotal" ) txtRevTot.text = "foo"

Actually, I’d suggest doing a forward reference of local txtRevTot outside of scene:create() so it’s available in other functions (I’m not a fan of putting “real” code inside the scene:foo functions).

That should work, but it’s based on theory, not because I actually wrote code and tried it. :wink:

 Jay

[EDIT - Now based on practice. I tried it and it worked fine.]

Hey appilee,

I can only confirm that Jay’s response is 100% valid. 

Thanks, Jay, for the quick response!

alex

This is it… exactly the answer I needed. Thank you both for such a quick and complete response!

Just keep in mind that in modules you can only have 60 upvalues, and it’s sometimes better to reference variables as part of the scene as opposed to local’s floating in the module’s top level chunk…

<--------- very guilty of that.

Rob

Thanks Rob, but I’m definitely going to need some more info to fully understand your advice…

Just keep in mind that in modules you can only have 60 upvalues, and it’s sometimes better to reference variables as part of the scene as opposed to local’s floating in the module’s top level chunk…

module = scene1.lua ? Or scene1.ccscene?

60 upvalues = 60 * local myVar = self:getObjectByTag(… ? Or something else?

it’s sometimes better to reference variables as part of the scene as opposed to local’s floating in the module’s top level chunk

Are you suggesting I can reference the Composer GUI text object directly without having to assign it to a local variable (e.g. self:getObjectByTag(“myTag”).text = “foo”)?

Thanks for jumping in to help with additional clarity on this topic!

Ben

module = scene1.lua

upvalues are anything “local” in the top level chunk:

local myNum = 1

local myString = “H”

local myVar = scene:getObjectByTag()

local function spawnSomething()

= 4 upvalues.

function scene:create( event )

     local myVarHere = self:getObjectByTag() – local to scene:create, not an upvalue to the main chunk

     if something == somethingElse then

           myVarHere.alpha = 0.5 – myVarHere is now an upvalue since it’s defined in the chunk above this block

     end

end

Note that “self” inside the scene functions is “scene”.  Use self inside of scene methods, if you’re outside of the scene’s methods, i.e. stuff in the scene’s top chunk, then you need to reference it as “scene”

I’m confused, Rob. In my current project I have 109 upvalues – variables “declared” outside of functions in one scene. This is using Storyboard.

Why is my game working?

 Jay

Ah, that makes a lot of sense. Thanks Rob. I get it now… thanks for being so patient and explaining so thoroughly!

I might be wrong Jay.  I know there is a 200 “local” limit.  But I’m pretty sure I’ve seen people hit a 60 upvalue limit.  Maybe I’m describing it wrong.

I also have 69 local functions in that file, so I might be getting close to a 200 limit, anyway.

 Jay

PS - No, I don’t recommend a single file that big. It’s what happens when you prototype something and then decide to just keep going – which means you didn’t really plan it out ahead of time. :slight_smile:

Make sure the name of the text (inside Composer GUI) is txtRevenueTotal and then inside scene:create() do this:

local txtRevTot = self:getObjectByTag( "txtRevenueTotal" ) txtRevTot.text = "foo"

Actually, I’d suggest doing a forward reference of local txtRevTot outside of scene:create() so it’s available in other functions (I’m not a fan of putting “real” code inside the scene:foo functions).

That should work, but it’s based on theory, not because I actually wrote code and tried it. :wink:

 Jay

[EDIT - Now based on practice. I tried it and it worked fine.]

Hey appilee,

I can only confirm that Jay’s response is 100% valid. 

Thanks, Jay, for the quick response!

alex

This is it… exactly the answer I needed. Thank you both for such a quick and complete response!

Just keep in mind that in modules you can only have 60 upvalues, and it’s sometimes better to reference variables as part of the scene as opposed to local’s floating in the module’s top level chunk…

<--------- very guilty of that.

Rob

Thanks Rob, but I’m definitely going to need some more info to fully understand your advice…

Just keep in mind that in modules you can only have 60 upvalues, and it’s sometimes better to reference variables as part of the scene as opposed to local’s floating in the module’s top level chunk…

module = scene1.lua ? Or scene1.ccscene?

60 upvalues = 60 * local myVar = self:getObjectByTag(… ? Or something else?

it’s sometimes better to reference variables as part of the scene as opposed to local’s floating in the module’s top level chunk

Are you suggesting I can reference the Composer GUI text object directly without having to assign it to a local variable (e.g. self:getObjectByTag(“myTag”).text = “foo”)?

Thanks for jumping in to help with additional clarity on this topic!

Ben

module = scene1.lua

upvalues are anything “local” in the top level chunk:

local myNum = 1

local myString = “H”

local myVar = scene:getObjectByTag()

local function spawnSomething()

= 4 upvalues.

function scene:create( event )

     local myVarHere = self:getObjectByTag() – local to scene:create, not an upvalue to the main chunk

     if something == somethingElse then

           myVarHere.alpha = 0.5 – myVarHere is now an upvalue since it’s defined in the chunk above this block

     end

end

Note that “self” inside the scene functions is “scene”.  Use self inside of scene methods, if you’re outside of the scene’s methods, i.e. stuff in the scene’s top chunk, then you need to reference it as “scene”

I’m confused, Rob. In my current project I have 109 upvalues – variables “declared” outside of functions in one scene. This is using Storyboard.

Why is my game working?

 Jay

Ah, that makes a lot of sense. Thanks Rob. I get it now… thanks for being so patient and explaining so thoroughly!

I might be wrong Jay.  I know there is a 200 “local” limit.  But I’m pretty sure I’ve seen people hit a 60 upvalue limit.  Maybe I’m describing it wrong.