local variables vs table entries

Hello devs,

I’m currently programming my scenes inserting every variable as self.variablename = display.new… (where self is the scene) so I can use them in every scene phase, such as enter and end

I assume writing every variable as local would be faster than acessing them indirectly (“self.something:setReferencePoint(…)” instead of simply “something:setReferencePoint(…)”) but how faster would that be? should I be worrying about this or am I paranoid about performance? [import]uid: 79152 topic_id: 29669 reply_id: 329669[/import]

Local is probably fastest but in my experience you are safe sticking to tables. For example, this is far more easier to understand

local guy = display.newGroup() guy.shirt = display.newImage(guy, "shirt.png") guy.pants = display.newImage(guy, "pants.png")

than this

guy[1] -- shirt guy[2] -- pants

I didn’t know you could just insert variables into the scene object; perhaps the danger there is just that they are destroyed if purge the scene? [import]uid: 41884 topic_id: 29669 reply_id: 119089[/import]

I wouldn’t worry about it.
Local is indeed faster, and in some cases redeclaring your variables as local in higher up scopes can be faster (i.e. local var = var to make that reference local to that scope), due to how lua operates under the hood, and helps contribute to why it’s significantly faster than comparative interpreted languages, but in the end, you are using an interpreted, heavily abstracted language, and so there is SO much else going on at any given moment(by you, the engine, and the language) that the way you access your variables should not be a problem. ever.

Make it readable, and design it to save programmer time, and as your game goes towards completion and starts running slow, fix the low hanging fruit, such as reprocessing things per-object or per-frame rather than from a singular control, or other MUCH more major slowdowns. [import]uid: 134101 topic_id: 29669 reply_id: 119148[/import]

thank you both for your kind answers.

indeed legibility should be a major concern.

I’m still bugged about writing self.something for most variables in a scene, or having to do slightly more bothersome copy&pasting due to the “self.”

I think I’ll try to use local variables on the Scene files, declaring them at the top of the file and using them throughout the scene states. Anyone has done so? [import]uid: 79152 topic_id: 29669 reply_id: 119533[/import]

thank you both for your kind answers.

indeed legibility should be a major concern.

I’m still bugged about writing self.something for most variables in a scene, or having to do slightly more bothersome copy&pasting due to the “self.”

I think I’ll try to use local variables on the Scene files, declaring them at the top of the file and using them throughout the scene states. Anyone has done so? [import]uid: 79152 topic_id: 29669 reply_id: 119533[/import]