Codea Internal Code

I noticed that in the loveCodify thread then Simeon posted a copy of the Class.lua code (

Could I be extremely cheeky and ask if there’s any other of the lua code that you’d be willing to post? I’m thinking of things like the vec2 class and the math stuff. Not being a programmer, and not having programmed in lua before, I find myself simply cut-and-pasting code from all over the internet (I believe that this is called Cargo Cult Programming). Sometimes there’s something in the existing code that I’d like to replicate, if only I knew what it was! Other times, I want to use the functions but there’s more than one way to do it (for example, calculating the length of a 3-vector) and I’d like to see how things are implemented to better select the “best” way to do it in my code.


Sure, but these are implemented in C and are fairly hairy. I’ve posted the vec2 implementation – since this will be included when we open source the backend anyway. (not very useful)

The math stuff is just part of Lua’s source code. Look for lmathlib.c in the standard Lua source distribution. We only changed this to use the 32 bit floating point math functions (see #define MATHF(c) in vec2.c) as they are less expensive than their double length counterparts.

We’ll be changing random and randomseed in lmathlib.c to use a better generator in the future, however.

Note: Lua’s math library is basically a wrapper around the standard C math library (math.h), so there is nothing too special in there.

Just been looking at this class and have a question about it, and about userdata in general. I’d like to be able to test whether a “thingy” is a particular “thingy”. If it’s an instance of a class, then I can use the v:is_a(what I'm expecting) method. That’s very useful. But for the “special” objects, like vec2, then it seems that the most I can do is find out that they are a special object (`type(v) == “userdata”).

It would be really useful if the special objects also had a is_a method.

From what I understand, only Codea can create these special objects, so if you added that to each of these things, that would mean that whatever code I added or imported, I’d (almost) always be able to find out what something was.

Where this becomes particularly useful is in writing extensions of these objects. So, for example, in the Colour class, I want to be able to define a Colour using either a color object or a colour specification. At the moment, I just test for the number of arguments. I’d like to be a bit more sophisticated! Similarly, when blending colours, it doesn’t actually matter if the other colour is a Colour or a color, but it might mean that I need to handle them slightly differently. Now, I could just assume that anything of type userdata that I get passed will be a color (who’d pass a vec2 to a colour operation??) but as Codea gets bigger, I’d worry that this wasn’t safe anymore.

So could you consider an is_a function for these “special” objects?

(I did try the checkvec2 function, but I guess it has to be made public for me to use it.)

That’s a good point. I’ll look at whether it’s possible to add an is_a method to vec2.

Class.lua defines it as a comparison on the metatable, which will be an identical instance when you compare an instance of a class to its type - myClass:is_a( MyClass ) - for example. I’m pretty sure it’s possible to do this for userdata types.

edit: Yeah, checkvec2 is internal only. is_a seems like a nice global solution.