What is the scope of code that is outside of any function or other explicit block?

Thanks SkyTheCoder. In my searches so far I have seen one or two calls made outside the two main functions. Some lessons are hard to learn

@SkyTheCoder you could just have an “explosion.prerender()” function, and just call it once from the setup() in your Main tab

@yojimbo2000 I wouldn’t want the user to have to do anything to use the class.

@SkyTheCoder Oh I see, you’re going for a totally self-contained modularity.

I just ran into a really strange issue which I think is to do with this question of the scope of code that is outside any block.

In one project I’m working on I have all my shader strings in a separate shader tab. They’re not in any block. ie:

--#Shaders
myShader={
vertexShader=[[ blah ]],
fragmentShader=[[ blah ]]
}
myShader2={

etc.

and then call them with myMesh.shader=shader(myShader.vertexShader, myShader.fragmentShader)

This has worked fine.

Then, mainly because I was bored of writing out fragmentShader so many times, I thought I’d prewrap some of the strings as a shader.

So now the tab looks like this:

--#Shaders
myShader=shader([[ blah ]], [[ blah ]])

myShader2=shader([[ blah ]], [[ blah ]])

and call them with myMesh.shader=myShader.

And suddenly, all my objects disappear. It’s really odd, I’d’ve expected an error (“shader expected, got nil”) or something, but just a blank screen.

The shader calls have to be inside a function it seems, they cannot be precompiled. So do this:

--#Shaders
function shaderSetup()
myShader=shader([[ blah ]], [[ blah ]])

myShader2=shader([[ blah ]], [[ blah ]])
end

(and call the function obviously), and everything works again.

So why is it fine to declare the shader string outside of any block, but a shader has to be called from a function?

@yojimbo2000 This problem was brought up a while ago, I don’t think it has anything to do with the scope, just that the OpenGL frame hadn’t been initialized yet (before even the first frame!) and couldn’t load shaders.

@SkyTheCoder OK, that makes more sense, thanks! It was with a new shader that I was introducing, and I thought that nothing being drawn was a problem with the shader, took me about an hour to figure out!

@SkyTheCoder you could use a function inside a function

function draw()
    --your explosion prerender
    function draw()
        --all other code that you'd normally have in draw
    end
end

the explosion prerender would only get excecuted once, then the draw function would get overwritten by the inner one, same principal as using a flag, but… It looks cooler (imo)

@stevon8ter But it’s in its own tab, and should require no setup for the user. That would have to be part of main.

@SkyTheCoder can’t you hijack the setup function like you do with the touched function? I don’t know, I’m just thinking out loud :stuck_out_tongue:

@stevon8ter Actually, I’m not sure if you can hijack the setup function. But again, the code would be in main, and would require user setup.

Why are newbies thrown in a the deep end when they can enter Codea via the back door using some of the techniques revealed in this thread ? So much can be done outside ‘FunctionDraw’ just using the 'setup function

backingMode(RETAINED)
function setup()
   w=WIDTH/2
   h=HEIGHT/2

    background(5, 247, 6, 255)
-- Draw a square the hard way
    stroke(256)
    strokeWidth(5)
    line(130,200,230,200)
    line(230,200,230,300)
    line(230,300,130,300)
    line(130,300,130,200)
    fill(233, 32, 32, 255)
    rect(w,h,100,100) -- A square made easy
    ellipse(600,200,200) -- Add a circle
 
    for x=1,10 do     -- a simple loop
        font("AcademyEngravedLetPlain")
        fontSize(64)
        fill(22, 21, 21, 255) 
        text("O",x*40,650)
    end
    text("Try text",w-100,h+150)
end

It’s a pity that Codea can not be coaxed into letting Lua run full screen as a starting environment for newbies.

@Steep I don’t know why my question has caused you so much anxiety! There is no requirement that “newbies” do anything suggested on this forum or anywhere! Have you not heard the expression “a bad workman blames his tools”?

I was a “newbie” in November last year, but I don’t consider myself to be one now, largely because of reading a lot on these forums, on the wiki link at the top of this page, on the blogs coolcodea (by @Ignatz) and codeatuts, and also @Ignatz 's free ebook series (which he links to in the first post on his blog). Have you read and digested his “Lua for beginners” and “Codea for beginners” books yet? If you do, you won’t have to start all of your posts with “as a newbie”, “as a newbie”!

You can go fullscreen with displayMode(FULLSCREEN) or FULLSCREEN_NO_BUTTONS.

There are lots of functions besides draw and setup. With system functions there’s touched, collide, orientationChanged, keyboard (and of course there are all the functions that you declare yourself. I’m assuming you’re not putting everything directly in draw and setUp…)

So can Codea be set up to make Lua fully functional so that a newbie can start at the beginning ? I consider myself a newbie with a long long way to go.
http://homepage.ntlworld.com/g.steeper/Link/FirstLink.htm

I started, and I recommend other newbies do as well, by just using Lua and print commands from the setup function, ignoring drawing on the main screen. That allowed me to get used to the language, before tackling graphics.

No special setup is required to do that.

I’m not exactly sure what you are asking, but Codea is a fully functioning implementation of Lua 5.2, you don’t need to set anything up. What makes you think it is not fully functional? It has lots of other layers added on top, which is necessary in order for us to access the features of iOS and the iPad (multitouch, device orientation, Open GL graphics, sound etc), as well as lots of other lovely things which are lots of fun for making games (Box 2D physics, Tweens etc), but, there’s no compulsion for you to use them! You are free to use as little as the instruction set as you choose!

Here’s the link to the Lua for beginners ebook:

https://www.dropbox.com/sh/mr2yzp07vffskxt/AABlplSGGFBTu8FzQy94lteua/Lua%20for%20beginners.pdf?dl=0

Thanks Ignatz I feel sure that if there was a way to use Lua full screen you would have used it instead of working in the black zone in your tutorials.
Ok - yojimbo2000 sorry for interrupting your search into the unknown. Thought it might help to take one step backward before trying to taking two steps forward.

Your alternative is to install a desktop version of Lua while you are learning the language, then you can use the full screen, until you aready to come back to Codea.

@Steep OK I understand now, you were asking about whether the console area could be made fullscreen.

@Steep I would also say that a simple and clearly defined question, “what is the scope of a variable”, counts as “a step back” rather than “2 steps forward” :slight_smile: