A few quick questions

I haven’t been using Codea for very long, so forgive me for asking stupid questions, but:

  1. Is it bad to use global functions in a Codea project? I’ve taught myself to use as many local functions as possible and all the global functions in Codea are bugging me… can other projects change them or anything?
  2. What happens if you DON’T use the setup function to create variables and stuff you might use later? Can you create functions outside the setup function without anything screwing up?

Other projects can’t affect them, no.

It’s not bad to use global variables, if you’re careful. But local variables are safer, and they also run a bit faster.

The setup function isn’t compulsory, no. You can create all the functions you want outside of draw. In fact, I think the draw function should rather be called the update function, and that it should call other functions to do the work, eg

function draw()
    UpdateObjectPositions()
    Shoot()
    DrawObjects()
end

As @Ignatz said, each Codea project is self-contained, so don’t worry about conflicts there

Each tab in a Codea project is a separate .lua file within the project. If you want to call a function across tabs, then yes, they do need to be global. If a function is only called within the tab, it can be local function.

What happens if you DON’T use the setup function to create variables and stuff you might use later?

Conceptually, I think it makes sense to set things up in setup, rather than having set-up stuff strewn around the global scope. As well as being able to see where all of your setup code is, its important for anything graphics related (eg variables like WIDTH, or graphics-related methods like shader will not work properly from the global scope) Of course, you can break that out into separate functions, eg


function setup()
    loadAssets()
    scene = SplashScreen()
end

or whatever.

Can you create functions outside the setup function

I’m not sure what this means.

And yes, try and keep as many of your variables local as you can.

You will nearly always need the setup function to create objects or settings, so I would include it anyway.

The main purpose of the setup function is to initialize things before the program gets started or to set things that need to be done once. Depending on what your program does, you might only need the draw function or functions called from draw.

@Ignatz - If local variables are safer and run faster then why (by default) are both the setup and draw functions global when you create a new project?

See my answer above

If you want to call a function across tabs, then yes, they do need to be global. If a function is only called within the tab, it can be local function.

If setup and draw were local, you’d only be able to call them from within the Main tab. The Codea runtime wouldn’t be able to access them.

Lua is incredibly fast, flexible and powerful, and has a tiny footprint, but the trade-off for that is that it has precisely zero safety features.

I wouldn’t worry too much about the local/global thing until your projects start getting large, although it’s a good habit to get into.