Skip to content
Tags

17. Things I would have liked to have known sooner

March 28, 2013

These are some of the things I would liked to have known sooner.

Pasting code into Codea

I have mentioned this before, but it’s important you know it. It took me over a month to discover it.

If you have been pasting someone’s code into Codea and trying to make sense of one big pile of code, you will wish Codea could put all the classes into tabs as they looked in the original. Well, it can, if you know the trick.

Press the new project button and hold it down until you get the option to copy into a new project. Press that and bingo, the code is in tabs.

Copying one thing to another may not work as you expect

Copying numbers or strings

a=10
b=a
a=20
print(a,b) --result is 20, 10

If you copy a to b, then change a, b does not change. This is what we expect, and it works for strings too.

Copying tables, classes etc

a={10,11,12}
b=a
a[1]=20
print(a[1],b[1]) -- result is 20, 20

In this case, any change you make to a also affects b, and vice versa. This is because they are actually the same table. More accurately, a and b don’t store the actual table, but a “pointer” or address of the table, so when you copy a to b, you are simply copying the address, and they will both point at the same table.

The same is true of classes. This means that copying a class also results in two variables pointing at the same object.

a=SomeClass() --create a class object
b=a
a=nil
print(a,b) -- result is nil,nil --we only deleted a, but b is gone too

So how do you copy (say) a table? You literally have to copy all the items from one to the other. Here or here is some code from the Lua wiki, that will do it, even if your table has two or more dimensions. Just say b=deepcopy(a)

Copying a class object would be more difficult, because you would have to copy each and every bit of stored data from one to the other, one at a time.

The built in Codea help is very good

If you press the little square at top left of the main Codea screen, you’ll be given an option to see a Reference library. It’s worth exploring to see what Codea can do, and it’s beautifully laid out.

Codea doesn’t have an obvious system timer

If you are trying to measure time accurately while running a program, the best way to do it is with DeltaTime, according to my reading of a number of forum posts, is as shown in this tiny program (yes,it will run without a setup function).

t=0
function draw()
    t=t+DeltaTime
    print(t)
end

DeltaTime is the time since the last screen draw, and if you add them up inside the draw function, you will get a running time counter. Generally, it will increment by 1/60 of a second, because that is how often Codea tries to draw.

There are other date and time functions in Codea, but there are issues in using them for accurate timing.

More later.

From → Programming

One Comment
  1. Ken Duerden permalink

    I think you must have psychic abilities. I was messing around with trying to simulate a pause this morning and this works great. Nice one !

Leave a comment