what is difference between adding variable above setup and inside setup?

Hello guys,

I was just wondering this, and moving my variables into the setup loop doesnt seem to have any effect. Does the variables inside setup not work in other tabs?

Setup isnt a loop its a function and runs one time, for example if you to set up a variable. Function draw is a loop but do only work when you don’t setup a variable for example

function setup()

a=0

function draw()

a=a+1

Now it counts up from 0 to however long time the program is running

Oh yeah umm I forgot to add ‘end’ but imagine like its there

I meant before the setup loop , for example:

var1 = nil

function setup()
var1 = nil
end

There’s no difference, because the variables outside setup are initialised when Codea starts, then it runs setup, which will initialise any variables in there. No difference, because the variables are global.

However, there is a difference if you make local variables outside of setup. Now they can only be seen by code within that tab.

Imagine if you create a code library, which could be used in lots of projects, and you don’t want its variables to clash with any of the variables in those projects. You can either

(a) give them weird names

(b) put your library into a table (of functions and variables)

(c) make your library variables local to the tab, by putting them at the top, outside of any functions. Then, if variables with the same name exist elsewhere in the project, your local names will be used within this tab, but nowhere else. Result is no clash.

Your question reminds me of this thread:

http://codea.io/talk/discussion/6223/what-is-the-scope-of-code-that-is-outside-of-any-function-or-other-explicit-block#latest

If a variable is global (ie isn’t declared with the local keyword), then it will be available throughout your code after it has been declared.

If it is declared as local, then it won’t be available outside of the block (ie past the end of the function, if clause, for loop, or whatever the block is). So local variables declared in setup won’t be available outside of setup.

If a variable is declared outside of any block (ie a line that does not indent at all) then it will be declared at “compile time” (I think this is what you mean by “above setup”), whereas if it is in setup then it gets declared as soon as the code starts running (“run time”).

If a local variable is outside of any code block then it will be available throughout that tab. This is the main thing I use compile-time variable declarations for. You see it a lot with “library”-style tabs. It’s a useful way of sharing a local variable across all the functions in a tab, but shielding it from anything outside the tab.

A global variable outside of any block will behave the same as a global variable declared in setup, so for simple globals I guess there’s not a lot of difference between whether stuff is declared at compile time or run time, ie inside setup or outside of any block (other than the neatness of having all of your code that sets things up in a function called setup).

If it’s anything to do with graphics though, then you should declare it within setup (eg during run-time, not compile-time): eg, something like centre=vec2(WIDTH, HEIGHT)/2 should go in setup, because at compile time, the WIDTH variable will reflect the display width before Codea has had a chance to enforce things like supportedOrientation or displayMode. Another example, you can’t declare a new shader myShader=shader(vertexShader, fragmentShader) outside of a code-block, because OpenGL isn’t yet available during compile time.

@yojimbo2000 @ ignatz Ah yes I had read that exact thread about an hour ago, which surfaced my question, I was praying to the codea gods that by doing a simple moving of variables into the setup code that it will speed up my code a tiny fraction… there goes that idea.

I havent started using multiple tabs yet as I find it more convenient to just use one tab per app, perhaps in the future local ‘global’ variables will come to be useful for me

When I was typing my response everyone else’s responses hadn’t appeared yet, in case anyone thinks I’m just repeating things people above me said…

@archistudent

I find it more convenient to just use one tab per app

NO. Really, you do not. :((

I cannot imagine a worse nightmare than having an entire project’s code in one tab. A recipe for utter disaster

one thing that will speed up your code is using local variables as much as possible, eg if you use math.sin a lot, you could define it at the top of your tab as sin=math.sin, and use sin instead.

Equally, if you have a table of stuff T, and you are using the item T.index a lot in a function, it is faster to write local index=T.index and use index.

This is because Lua has to do less work finding your variable if it is local, and if it doesn’t have to look it up in a table (ie there are no “dots” in the variable name).

Experiment a bit and see if it helps…

@yojimbo2000 haha I do a lot of backups by copy and pasting code to dropbox, mutli tabs will make this very inconvenient… perhaps it will take one project to crash and burn before I change my ways…

I just submited a link to my first app that was made in one tab on appstore to the ‘app made on codea’ thread, link is here if you want to try it out, its free :slight_smile: https://itunes.apple.com/app/id946309263

@ignatz I use sin a lot, will try that out. My current app inefficiencies come from the cloud function which I had posted before and 10x10 grid of 550x550 pixel images function, but still able to keep above 60fps…

I’ll post the link to the current app when its out next week !

 the WIDTH variable will reflect the display width before Codea has had a chance to enforce things like supportedOrientation or displayMode.

Wow this part blew my mind, I am starting to understand it

It’s the difference between compile time and run time. If it’s a variable that interacts with other systems such as the graphics, then you need to wait until run time before declaring (ie declare it inside setup)

@Ignatz It does matter if you have a global variable inside or outside of setup, if the variables involved calling a function that was in a tab to the right of Main.

I think the main aim to use variables outside the setup is when you want them to be local to the current tab. On the other hand, you lose a little debug-control / convenience, since they don’t get reset (defined again) when you rerun the project from the player (this little round arrow under the console)

This type of code ist executed only once, when the player starts. From then on only the main loop-functions are executed (setup, draw, touched, orientationChanged, etc)

That is the reason for using this:

supportedOrientations(...)
function setup()
end

VS

function setup()
    supportedOrientations(...)
end

since they don’t get reset (defined again) when you rerun the project from the player (this little round arrow under the console)

That used to be the case in previous versions of Codea, but now they do get redefined when you reset the project

@yojimbo2000 @skythecoder Do you guys know if my app designed for ipad2 with 1024 x 768 resolution will work on the ipad air 2? The only changes will be the blurriness of the images right?

but all my coordinates will be in the same place, for example if I open up the two apps, the ipadair 2 will ‘look’ the same as the ipad 2 screen?

yes, the WIDTH/HEIGHT variables stay the same irrespective of whether the display is retina

@yojimbo2000 okay cheers thanks!