Log In  


Hello I started lua programming just yesterday.

Because my project should be started right now, I hadn't so much time to read through lua tutorials.

I am an web developer and I have never seen such errors when I worked with Javascript and Java and I don't understand meaning of the error.

This is my code

The error is occured in function drawLife.draw()

function _draw()
	cls();
	rectfill(0,0,128,128,12);
	rectfill(0,72,128,128,7);
	clouds.draw();
	mapdraw(16,16);
	mapdraw(0,0);
	enemies.draw();
	bullets.draw();
	p.draw();
 	mapdraw(16,0);
	fires.draw();
	score.draw();
	drawLife.draw();
	print(bullets.type,4,4,7);
end
function drawLife.draw()
	rectfill(80,80,60,60,#FF004D);
	rectfill(80,80,60,60,#FF004D);
	rectfill(80,80,60,60,#FF004D);
	rectfill(80,80,60,60,#FF004D);
	rectfill(80,80,60,60,#FF004D);
end

I added .draw() after drawLife because I got lua code from my friend's brother who is game developer... and there were many .draw() and :draw() after the function name.. I need to learn about that



"Because my project should be started right now, I hadn't so much time to read through lua tutorials."

Sorry but you can't just code something in a language without at least a good understanding of the key aspects of it (and then expect the community to fix it!)

Let's start over: https://www.lua.org/pil/1.html


freds72// Yes I know. I am reading lua tutorials. I wll learn and learn untill project is done.

I just meaned I didn't have much time to read tutorials enough.

I just wanted ask meaning of the error.

I found it online that a nil value means like a null in other programming language but Attempted to index global 'drawLife.draw()' is quite difficult to me figure out.

I don't want solution. I just wanted know definition of the error.


A variable which has not been assigned yet evaluates as 'nil'.

Lua function definitions are really just assigning a lambda function to a variable:

function add(a,b)
    return a+b
end

-- same as

add=function(a,b)
   return a+b
end

This means your code tried to do this:

drawLife.draw=function()
    ...
end

But there is no variable called drawLife. If you try to print(drawLife), you will get 'nil'. Trying to reference a member of drawLife, with drawLife.draw, will fail with the kind of "nil reference" error you saw.

As freds72 says, you need to do your reading. Lua is different from other languages in multiple ways. You will only find frustration if you don't learn first.


Elaborating slightly: "drawLife.draw" refers to an attribute of a table value assigned to the variable "drawLife". To get that function assignment to work, you must first initialize the "drawLife" variable with a table:

drawLife = {}
function drawLife.draw()
  -- ...
end

This is true for every "xxx.draw()" call inside your _draw() function: each one refers to an attribute named "draw" in a table assigned to a (global) variable. So those need to be defined for the "_draw()" function to work. (These will also complain about nil values for the same reason.)

I'll temper what others have said: I think it's absolutely fine to jump in with both feet with minimal reading, and Pico-8 is the kind of platform to do that with. It just so happens that using tables this way is a more advanced subject of the Lua language.

You might try replacing the table attribute expressions with just a simple function name and see how far that gets you:

function _update()
end

function _draw()
 cls()
 drawlife_draw()
end

function drawlife_draw()
 rectfill(80,80,60,60,7)
 rectfill(80,80,60,60,7)
 rectfill(80,80,60,60,7)
 rectfill(80,80,60,60,7)
 rectfill(80,80,60,60,7)
end

Good luck!


Thanks everyboy!



[Please log in to post a comment]