Functions / vectors

I need a function that will receive a vector variable, and see whether it length is more, than some maxLength.
If yes, it will decrease the vector, so that its length is now maxLength. If no, it will return the vector back (with its original size).

function limit (v, max)
    self.v = v
    self.max = max
    if self.v:len() > self.max then
         self.v = self.v:normalize()
         self.v = self.v*max
return self.v        

Elsewhere in the program I write

vel = limit (vel, 10) -- 'vel' is a global parameter 

The function doesn’t work, saying I am attempting to change a global variable.

Thank you for your help,


you should only use “self” inside a class. This should work…

function limit (v, max)
    if v:len() > max then return v:normalize() * max
    else return v  

PS you were also missing an end statement after your if

Thank you very much. I read your post about the “self” word, but I am still struggling with it)

The code in a class is used by all the objects you create with that class.

If, for example, the class has a position property, that will be different for all the objects you create with that class, then you need some way of storing position separately for each object, and being able to find it later.

Self does that. It simply means “whichever object is using the code at the moment”, and I think of it visually, as being like a room full of lockers, where each locker holds information for an object in the class, and self is the name on the locker.

So you might give instructions like “when people come into the locker room, get their id number, ID, and make them put their clothes in the locker labelled ID”.

In the same way I used ID as a placeholder for whatever the id number is, “self” is just a placeholder. It tells Codea to replace it with the address of the data for the current object using the code.

Why would this function not work?

It is intended to return the pod vector, which I will be using in the main tab

function Unit:pos()
return self.pos

Maybe you are calling it incorrectly

b=a.pos()  --incorrect, uses full stop
b=a:pos()  --correct, uses colon

The problem lies somewhere in this line:

I have previously initialized each object in he table.

That code looks all right, you’ll need to post more code

I will, thank you for your feedback.

function setup()
number = 20
spacecraft = {}

    for i = 1, number do
    table.insert (spacecraft, Unit (math.random(100,200), math.random(200,300), math.random (1,8)))

function draw()

background(40, 40, 50)

 for i = 1, 20 do

And the function itself

function Unit:pos()
return self.pos

@alexNaumov - this is an interesting problem.

I think you may find it works if you change the pos function name in the Unit class, to something else, eg position. That worked for me.

The reason, I think, is that self.pos can refer to both the function Unit:pos and the class variable self.pos, so you need to have different names.

When you call spacecraft[i]:pos(), Codea actually calls spacecraft[i].pos(self), then it finds that self.pos is a vec2 and causes an error.

You can’t have a function named the same as your variable

You could use metatables to make it so that what you are doing works, or you could just rename one of them