Globals isn’t really the way to go. Sure they work, but they might well catch you out later.
Functions and modules are where you want to be, so you can reuse code by passing it the relevant information.
Understanding SCOPE is critical. Knowing where and when to declare a variable can drive you crazy until you understand it. The link you posted however is a good starter. I tend to use a ‘common’ routine that I learned from Roaming gamer. I’ll post more below,
As for your original code, once you set up a listener, you can determine the object via the listener, and proceed accordingly.
As for everything happening at once, if you only have one button what would you like to happen. It sounds like you might need some timers to slow things down perhaps? timer.performwithdelay() perhaps?
The common routine is as simple as…
Create a new file called common.lua
In this file, I have the following…
local common={} common.variableName = "Fred" --Freds Name common.tblItems = {} -- Common table of items common.debugparms = false -- Set to true to see properties loaded in parms modules. ---------------------------------------------------------------------- -- When loading parms, if we have debugparms set to true, we'll run this function. function common:parmprint (message) if common.debugparms==true then print( message ) end end --==================================================================== return common
In my usual code, I just have
common=require("common")
Having that there, allows me to refer to any of the variables (and change them) from any module or function. The function I included is available too.
Eg:
common:parmprint("Name provided:"..common.variableName) common.variableName = "Barney" common:parmprint("Name provided:"..common.variableName)
This would print, change and print the variables from any module. Good for some scenarios.