Setup() Variables don’t matter? But... Draw() Variables do?

I was tinkering around with this code, from @Ignatz (I don’t know who he is… but I love this guy) the only tutorial, thus far that has awakened me and as slow as I am, I leave his guide with understanding

Anyways… this is his tutorial I am following:

@Ignatz is teaching us noobs how to get Codea to re-draw a line every 60 frames a second (animated line). This is beyond cool, he breaks it down block by block. Again, I have to say it, this teaching style is amazing. I truly want to send monetary contribution to this guy via paypal. He’s making me smarter!


I wanted to tinker with the line starting point, in regards to the negative Y Axis (-Y Axis), with the sole purpose of attaining some insight with the code., no rhyme or reason, other than to glean and understand how to manipulate code in Codea.

Here is Where I Think I Understand What’s Going On, But Not Quite Sure If I Do Get It

In Function Draw() There is a code block in this tutorial, that states if the WIDTH is greater than the variable x=0 (x>WIDTH) then the animated line calls the value declared in the Function Draw(), which for the tutorial is zero.

I thinks I understand that part… But this one, concerning the variable - throws me off a little.

At the Function Setup() x is declared as a variable with the value zero.

For tinkering, I changed it to 300 to see what would happen. When I hit the play button, the negative y axis portion of the line (the swinging pendulum) started off in the center. (Yay! I did some manipulation)

But after, the line reached the far right of the screen, it would follow the IF and THEN statement made in the Draw() block. Here’s the confusion, I think. The line would start off at the far left of the negative Y Axis. Because the x value set to zero at the Draw() code block section.

*Soo, my question here is…
Are the variable values that do matter or get called, are the ones in the Draw() NOT the ones in the Setup()?

In other words, declaring a variable in Setup(), really means… “Hey simple Human, X is a variable of value zero” but that doesn’t really matter, because Setup() is only used to declare a variable.

where it really matters, is over at the Draw() setup, it is here, where changing the values matter and will be re-drawn every time, not at the Setup().

Am I explaining this correctly?
Am I comprehending the abstract idea correctly here?


–[[ SwingingLineGuide
Studied Here:

-- Use this function to perform your initial setup
function setup()
    -- Are establishing you want to see the play screen in full screen mode with the console view tucked off to the side.
    print("Hello World!")
    -- You know what this is -> don't be stupid!
    -- You are establishing, you'd like to know the width & height of the display, and would like it presented in the console via the print command.
    -- You are establishing, x variable equals zero

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)

    -- This sets the line thickness

    -- Do your drawing here
    --[[ You are establishing you want a line drawn via the line command.
    You are establishing you want the line transformed to the exact specific values placed in the x,y grid. 
    -- Learned->   +<Increment> and <Numerical> equals the speed at which the line is being re-rendered <animated>. 
    if x>WIDTH 
    then x=300

@tactfulgamer - you are right, the setup() function is called once, initially, when you run the program. It is used to set up the initial state of variables you wish to use in you project.

Any function you call from within the setup will be run during that initalisation, but that dame function could be called from within the draw() function or by other functions within you code. So you may initialise variables in a sub function called from setup() but then call the same routine from draw() later. A good example is when you need to reset conditions in a game.

Touch() is also parsed like draw every cycle() if it is present


Thank you!

@tactfulgamer If you’re trying to learn new things, here’s a spin-off of the above code. If you want to see an example or have a question about something specific, just ask. I can probably whip something up or give you a link to a discussion where I’ve already wrote something.

PS. Whenever you post code, put ~~~ (3 tildes) on a line before and after the code so it shows properly. I added them to the above code.


function setup()
    x,y=0,0 -- set to 0
    cx,cy=WIDTH/2,HEIGHT/2  -- set to center of screen
    stroke(255,0,0) -- line color
    strokeWidth(5)  -- line width
    speed=1 -- initial speed
    fill(255)   -- text color

function draw()
    line(cx,cy,x,y) --draw line from center to edge
    checkLimits()   -- check limits of x,y
    text("speed = "..speed,WIDTH/2,HEIGHT/2+100)
    text("tap here to increase speed x 2",WIDTH/2,HEIGHT-200)
    text("tap here to decrease speed / 2",WIDTH/2,200)    

function checkLimits()  -- add speed and ckeck limits of x,y
    if y>=HEIGHT then
    if x>=WIDTH then
    if y<=0 then
    if x<=0 then

function touched(t) -- increase or decrease speed based on where screen was touched
    if t.state==BEGAN then  -- beginning of screen touch
        if t.y>HEIGHT/2 then
            speed=speed*2   -- touched above middle of screen
            speed=speed/2   -- touched below middle of screen

@dave1707 Thank you. I will definitely test it out.

I am currently running through @West Noob Lander Walkthrough. Would like to see how I fair with this one.

I must share…

That it’s crazy, now after 6 months of studying C# with an on and off cadence, because of that and the struggles with Codea…

I think, I have just entered the first layer of understanding. The first contact of comprehension. Not even a week ago, I was struggling with something that @Simeon helped me with.

After that struggle and a few more hours of more struggling with another guide…

I now can actually read and understand some of the code here. The syntax is way more simple than C#, if I do not get it at glance or even know what the code is doing, I still feel like I kind of know, what it’s trying to say to me.

What a strange rewarding sensation all wrapped into one. Definitely feels like a window just opened to show me visuals, that I couldn’t see before, even when I was staring at the same window… what a trip this is.

So this is how it feels like?

Ok, I get it. I now get why you guys are so hooked on coding. I feel like Neo or something. It must be an even greater sensation with the higher level guys like you @dave1707 @Ignatz @Simeon

Either way, I just had to share this weird experience I am literally going through at this moment.

@dave1707 Greatly appreciate the ~~~ knowledge. I was wondering what I did wrong there. Now I know :smiley:

Will definitely try out your code and see what I can learn.

I will also keep your other offer in mind. Must warn you, I will be calling out to you a lot - since you offered and I am HUNGRY to learn!!

Thank you.

It is a great feeling to begin to understand how code works and how to write it. Glad you are making progress!

@tactfulgamer I’m here to help, so don’t feel like you’re bothering me. Once you understand how Codea works, it’s all fun and games after that. It’s now been 7 years this months since I started playing with Codea and I’m not tired of it yet. After all that time I’m still learning things, it’s like finding new presents that I haven’t opened yet.