Learning Code

Greetings! I’m looking for someone to shove me in the direction of a COMPLETE, from the deepest pitt of Hade’s underpants up, tutorial of the language and concepts of codea. I’ve tried reading some guides and looking at the examples but its like trying to learn a forgein language by examining a book wrttien in that language. It does’nt work. Some of the basic things I’m needing to know are

  1. Whats a function and what are there rules?
  2. What up with the tabs on the top?
  3. If Then statments and rules?

Please help me use this amazing app so I dont waste my time playing games and instead I’m making the games.

Here’s an example function:

function sayHello()
    print("Hello, world!")
end

This simply prints “Hello, world!” into the console. You can also call functions from within functions, and use parameters.

-- Use two minuses to add a comment, which isn't used when the program is ran, for notes.
function saySomething(text) -- When we have some text between the ()s, it is a parameter.
    print(text)
    -- Separate parameters with commas for multiple parameters i.e. (these, are, parameters)
    -- That would let you use the parameters in the function, "these", "are", and "parameters".
    -- Their type is what they are first defined as, i.e. if we say "if these == true then" "these" will
    -- be a Boolean type. I'll explain if statements in a moment.
    sayHello()
end

function sayHello()
    Print("Hello, world!")
end

If, then, else, end. These can make a check for any Boolean values, i.e.

value = 1

if value == 1 then -- Use two == to check whether a value is exactly equal to another.
    -- == is exactly equal, < is less than, > greater than, >= greater than or equal to, <= less than or
    -- equal to, and ~= is not equal too. You can also use "and" and "or" to add more checks.
    print("Value is one!")
elseif value == 2 then
    print("Value is two!")
else
    print("Value is not one or two!")
end

You can also use while and for loops.

value = 1
while value < 5 do
    print(value)
    value = value + 1
end

This will print:

1
2
3
4

You can do this easier with for loops.

for value = 1, 4 do -- For loop format is kind of weird, but you get the hang of it.
    print(value)
end -- Basically, it runs the code multiple times, with each time the value you named different.
-- Here since we declared "value" as what it should count, it will run the code four times, just
-- like last time, with the same output, but one less line.

Codea isn’t just for games, either, you can do anything you want with it. But it is great for games :wink:

I’m not quite sure what you mean by “rules,” sorry.

And the tabs at the top handle your classes. It just kind of makes your code more organized, when you have different sections of code you can put them in separate tabs to make finding different parts a bit easier.

Hope this helped! It’s kind of long though, sorry… When you’re in Codea, by the way, and you have the keyboard up, press the little eye icon at the top of the keyboard. It’ll bring out a sidebar with lots of tutorials of everything.

By the way, when you’re running your code, and there’s the blank screen with the toolbar on the side, down at the bottom is the console. It’s used to print messages, whether th messages are the game, or for debugging or errors. Use the print(“Some text”) function to (as the name implies) print something to the console. If you want to add a value to the text you can do this:

value = 1
print("Value is " .. value .. "!") -- Use two periods (..) to add a something else to text.

theMessage = "Hello, world!"
print("The message is " .. theMessage) -- This works for strings, too.

If you’re going to add strings together (By the way, anything in quotes “” is considered a “String” in Lua), be sure to add extra spaces, i.e.

theMessage = "Hello, world!"
print("The message is" .. theMessage")

Would print

The message isHello, world!

Yeah…

Wow! Thanks for typeing all this for me. Thanks! The eye symbol hint really halped me.

A few more questions of mine.

  1. What exactly is a function? ( What does it do/ laws behind it)

  2. What are the rules for a function? By rules, I mean what makes a function work.

  3. When and where do you putt “end”?

  4. What are the “rules” for If Thens? What makes them work?
    I think got got the concept of the if thens from you examples gut with all the notes it’s hard to read.

  5. Whats a paramiter?

I got the first chunk of the loops section then the second have lost me. How does “for” work?

I think this wraps up my ponderings for now, I see you soon though!

Also whats a string and a boolein?

A function can execute some lines of code only when you call it.

When you want to stop you code only working in an if statement, elseif statement, else, while, for, or function statement, you place an end, i.e.

value = 2
if value == 1 then
    Print("Value is one!")
end -- I don't want the rest of the code only to work if value == 1
print("Done")

This will only print “Done!” because 2 ~= 1.

A Boolean value is basically the code term for a true or false value.

The syntax (Order you type something in, how it’s supposed to look) for an if statement is simple.

if <insert Boolean> then
    <insert code to run when above Boolean is true>
end -- Finishes the if statement

I explained what Strings are in my second post, but I think you missed that. I’ll say it again; A string is basically what you call any text, and you put it in quotes (“”) to make it.

example = 1 -- No quotes makes it an Integer.
example = "1" -- Quotes make it a String

Parameters (I’m pretty sure that’s how it’s spelled, but it’s different in different programming languages, sometimes called arguments.) are any extra data you want to give to a function, i.e.

function sayText(text) -- This function has a parameter called text. If you call this
    -- function, you must have the parameter.
    print(text) -- Using the parameter text we defined, we pass it on to the built-in function print.
end -- End the function.

sayText("Hello, world!") -- Call our function sayText, with the parameter text as "Hello, world!"

For loops are more advanced, but essentially if we say

for value = 1, 10 do
    print("The value is " .. value .."!")
end

it will run the code after the do and before the end as many times as needs so that it runs it 10 times, the first with value = 1, the second with value = 2, and so on, up to value = 10. S the output will be:

The value is 1!
The value is 2!
The value is 3!
The value is 4!
The value is 5!
The value is 6!
The value is 7!
The value is 8!
The value is 9!
The value is 10!

If statements, functions, and how everything else works is a completely different topic, that’s basically learning how to make your own programming language. I hope to learn how to do it some day, but for now just knowing it works is good enough.

Sorry for lots of notes, huge posts and clutter, but it can be hard to explain some things.

Glad to be able to help!

Good work, SkyTheCoder!

Its people like you that make the internet so great! I don’t care how big your posts are.

Im still quite confused about functions.

  1. Is all if, for, and while’s called a function?

  2. What code do you putt into a (below)? (How do you copy down code like you did into a post?)

function example()

end
  1. Also, what goes where the “example” is and also inside the parenthesis?

I also have some questions on the syntax of touch related things and the touched function but one thing at a time. Thanks again!

@Goatboy76 - have a look at some Lua tutorials (google, there are quite a few) to get a basic idea of the language, ie to understand loops, if tests, and functions and tables. While we can help you with specific problems, we can’t give you a full set of tutorials in a forum thread.

When you have some idea about Lua, I have written lots of tutorials about codea, here
https://coolcodea.wordpress.com/2013/03/10/starting-with-codea/
…but you really need to know some Lua first…

Ok, I understand. You guys can’t answer every question have. I have read quite a few tutorials, including yours (Ignatz) and my brain just doesn’t learn like that. The guides are great, don’t get my wrong, but my brain does’nt learn by doing something and then extracting a concept out of that. I find myself grinding my face on the wall randomly wondering why the example worked and this doesn’t because I just don’t get the concept.

I guess I’ll just go back to figuring it out on my own. Thanks again, you guys are awesome!

One last thing though. I don’t want to be known as that annoying person on the forum so can I still ask what must be extremely beginner questions of the forums i.e How do you change a variable inside a function?

a=3
sets a equal to 3

@Goatboy76 here is a great tutorial for understanding the very skin of lua before you decide to actually decide to dive in to the pie: http://lua.gts-stolberg.de/en/Programmieren.php

@Goatboy

if, for and while are examples of controls structures.

if…then statements allows the program to check to see if a condition is met within your program, and if it is, then to execute an action.

For example: If (the user has tapped the screen) then (play a sound)

for and while are types of loops, which allows the same piece of code to be executed multiple times. For loops says repeat the code a set number of times, while loops says repeat the code until a stopping condition is met.

A function is just a block of code. Your entire program could be a single function if you like. However, there are advantages of breaking down your code into smaller functions including the ability to re-use a function.

The brackets are used to pass information (or variables) to the function.

to format the code in the posts, put three tildas ~ at the start and another three tilsas at the end of the code

Thanks guys! Is “computer lua” the same as “codea lua”?

In this bit of code, I am getting the “Touch is MOVING” string in the console but it doesnt seem like the boolien MOVED is changing

  if touch.state == MOVING and MOVED == no then
        
        print("Touch is MOVING")
        MOVED = yes
        end

yes is not a synonym for true (and likewise no for false). Lua is interpreting both as user-defined variables. I suspect you haven’t predefined them, in which case they both evaluate to nil. So if you don’t predefine MOVED then MOVED == no tests nil == nil first time, which succeeds, and then in the block you do MOVED = nil (since yes is nil), whereupon the conditional still works since MOVED is still nil.

This is my setup

function setup()
    y = CurrentTouch.y
    x = CurrentTouch.x
    c = fill(0, 68, 255, 255)
    tc = fill(255, 55, 0, 255)
    MOVED = no
end

Then skipping past all of my drawing and Back round stuff

function touched(touch)
    if touch.state == BEGAN then
        
      print ("Touch has BEGAN")
    
    end
    
    if touch.state == MOVING and MOVED == no then
        
        print("Touch is MOVING")
        MOVED = yes
        end
        
    if touch.state == ENDED then
        
        print("Touch has ENDED")
        MOVED = no
    
        end

end

What I’m wanting this to do is when I touch the screen it prints says “The touch has begone” then when I move my finger I recognizes that it’s moving with" The touch is moving" then when I kift my finger it says “Touch has ended”. But I only want it to print “The touch is moving” once, right now its doing it 60 times a second. Hence for the MOVED variable. Puueee that was a lot.

Try it with replacing yes by true and no by false.

Yeah, Boolean values only work with true or false. It seems like the goal of some programming languages are to be complicated, but every programming language has Boolean values, which can only be set to exactly true or false, but not yes or no.

Yup, that did the trick, thanks!