Wiki

Clone wiki

Core / Parameter

Variables and Parameters

After doing the First Program your code should look something like this:

-- Use this function to perform your initial setup
function setup()
    print("Hello World!")  
end

-- This function gets called once every frame
function draw()
    ellipse(WIDTH/2,HEIGHT/2,100,200)
end

Now, let's remove the hello world print and replace it with that math the ellipse is doing.

function setup()
    print(WIDTH/2)
    print(HEIGHT/2)    
end

The numbers represent the position in the middle of the screen.

Next, we will add variables with comments. We will keep the print statements to show we are getting the same results.

function setup()
    x = WIDTH/2 
    y = HEIGHT/2
    w = 100
    h = 200
    print(x)
    print(y)    
end

We can use these variables in the ellipse function.

function draw()
    ellipse(x,y,w,h)
end

This has been much effort for no visible results but it has shown how you can move things around. It also gets us ready for the next step. Just a little bit more then we will show what the effort was for.

function setup()
    parameter.number("x",1,WIDTH,WIDTH/2)
    parameter.number("y",1,HEIGHT,HEIGHT/2)
    parameter.number("w",1,WIDTH,100)
    parameter.number("h",1,HEIGHT,200)
end

What are these? They are a set of parameter.number functions for those variables we made. They create sliders for the variables (so we can vary them). They works like this (name,minimum value,maximum value,initial value).

Slide them around to make your ellispe change.

How can parameter.number change anything? It's in setup which you said was only executed once, right?

Yes, but the function just creates the sliders, the sliders are then part of the Codea loop without any other code.

You can change the variable names of your parameter.number as long as you remember to change it in your ellispe. That name is the connection. w to width would be a bit more clear.

You don't have to control your ellispe with parameter.number functions. You can build logic using standard lua to change those variables. This demo just shows what the ellipse variables mean visually.

Updated