Posted 26 May 2015 - 01:43 AM
Alright, I am very confused… I have recently taken probably a 3+ year break of CC but im back again! And I decided to use CC to attempt to learn about A* pathfinding. Howver, I quickly ran into a really weird error that I don't understand at all. When attempting to do object orientated programing, I keep getting nill values! For example:
The program breaks at the "print(self.xpos)" line for attempting to index a nill value. I don't understand this problem because I have a program that I made years ago on a different version of CC that utilizes this kind of programing and still works. Here is a small snapshot of code:
This works in my game perfectly fine. Now here is where things get really interesting! When I take this piece of code and paste it into a NEW program created on on the NEW version of CC (and I define all the variables such as h and l) It doesn't want to work anymore. And again, the program breaks on the line where "self.[whatever]" is called. I honestly don't think it has anything to do with the fact that this code came from a different version of CC because I'm pretty sure the files don't have anything in them that is able to tell the os which version of CC they were made in. But I still don't know what is going on here? Does anyone know what is happening? Here is another piece of code that breaks, just because its simpilar and easier to understand (maybe):
local nodes = {}
function createNode(x,y)
node = {
xpos = x;
ypos = y;
parent = null;
hValue = 0;
gValue = 0;
getXpos = function(self)
print(self.xpos)
end;
}
return node
end
nodes[#nodes] = createNode(1,1)
nodes[0].getXpos()
The program breaks at the "print(self.xpos)" line for attempting to index a nill value. I don't understand this problem because I have a program that I made years ago on a different version of CC that utilizes this kind of programing and still works. Here is a small snapshot of code:
function background()
stars = {
xpos = math.random(l);
ypos = math.random(h);
xvel = 1;
draw = function(self)
term.setCursorPos(self.xpos, self.ypos)
term.setBackgroundColour(colours.black)
term.setTextColour(colours.white)
term.write(".")
end;
update = function(self)
self.xpos = self.xpos - self.xvel
if self.xpos <= 0 then
self.xpos = l
end
end;
}
return stars
end
This works in my game perfectly fine. Now here is where things get really interesting! When I take this piece of code and paste it into a NEW program created on on the NEW version of CC (and I define all the variables such as h and l) It doesn't want to work anymore. And again, the program breaks on the line where "self.[whatever]" is called. I honestly don't think it has anything to do with the fact that this code came from a different version of CC because I'm pretty sure the files don't have anything in them that is able to tell the os which version of CC they were made in. But I still don't know what is going on here? Does anyone know what is happening? Here is another piece of code that breaks, just because its simpilar and easier to understand (maybe):
player = {
xpos = 5;
getX = function(self)
print(self.xpos)
end;
}
player.getX()