Author Topic: Error: Attempt to index local 'gc' (a nil value)  (Read 6605 times)

0 Members and 1 Guest are viewing this topic.

Offline LUAnoob

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 10
  • Rating: +0/-0
    • View Profile
Error: Attempt to index local 'gc' (a nil value)
« on: January 31, 2012, 11:30:42 am »
Hey guys,

i got the problem, that i wanna made a game for me and my classmates, in which you can get money and fame :D

That's my source yet:


Spoiler For Spoiler:
Code: [Select]
money=0
energy=100
fame=0


function on.paint(gc)



gc:setFont("sansserif","b",15)
gc:drawString("Pennergame",0,0,"top")
gc:setFont("sansserif","b",12)
gc:drawString("1.Arbeiten (Bringt Geld)",0,80,"top")
gc:drawString("2.Ausruhen (Bringt Energie)",0,100,"top")
gc:drawString("3.Sachen kaufen (Bringt Fame)",0,120,"top")
gc:setFont("sansserif","r",6)
gc:drawString("Money: "..tostring(money),250,0,"top")
gc:drawString("Energie: "..tostring(energy),250,10,"top")
gc:drawString("Fame: "..tostring(fame),250,20,"top")
gc:setFont("sansserif","b",8)
gc:drawString("(C)Tim Krase",0,200,"top")




end

function on.charIn(ch)
if ch=="1" then
drawArbeiten(gc)
elseif ch==2 then
elseif ch==3 then


end
end

function drawArbeiten(gc)

gc:setFont("sansserif","b",15)
gc:drawString("1.Pflaschensammeln - 10",0,80,"top")

end

function arbeiteneingabe(ch)
if ch==1 then
money = money + 10
fame = fame - 5
end
end

And that's the error:

line 41: attempt to index local 'gc'(a nil value)

Line 41 is the line which "gc:setFont("sansserif","b",15)" includes.

Greetz,

LUAnoob

Sorry for bad english, i'm German.

Offline jwalker

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 660
  • Rating: +13/-0
  • Almost everything I have released includes a 'WZ'
    • View Profile
Re: Error: Attempt to index local 'gc' (a nil value)
« Reply #1 on: January 31, 2012, 11:43:31 am »
you have to pass gc from on.paint(gc)
also dont expect too much fame and fortune, at least not for now. lol :)
how many poeple have an nSpire in your class anyway??
EDIT: also do you mean you are getting an error on line 40 too
« Last Edit: January 31, 2012, 11:45:28 am by jwalker »
<a href="http://www.nerdtests.com/ft_cg.php?im">
<img src="http://www.nerdtests.com/images/ft/cg.php?val=9612" alt="My computer geek score is greater than 41% of all people in the world! How do you compare? Click here to find out!"> </a>

Support Casio-Scene against the attacks of matt @ matpac.co.uk ! For more information: Casio-Scene shuts down & Matt actions threads

Offline LUAnoob

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 10
  • Rating: +0/-0
    • View Profile
Re: Error: Attempt to index local 'gc' (a nil value)
« Reply #2 on: January 31, 2012, 11:58:41 am »
Thank you for your fast answer ;)
you have to pass gc from on.paint(gc)
What do you mean with "you have to pass gc from on.paint(gc)" ?
Can you give me a code example or explain it please ?
also dont expect too much fame and fortune, at least not for now. lol :)
how many poeple have an nSpire in your class anyway??
EDIT: also do you mean you are getting an error on line 40 too
Yes, i got an error at line 40 too, but i've corrected it.
All people in my class have an nSpire, because it was a demand of our teacher.
« Last Edit: January 31, 2012, 11:59:41 am by LUAnoob »
Sorry for bad english, i'm German.

Offline jwalker

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 660
  • Rating: +13/-0
  • Almost everything I have released includes a 'WZ'
    • View Profile
Re: Error: Attempt to index local 'gc' (a nil value)
« Reply #3 on: January 31, 2012, 12:23:16 pm »
function on.paint(gc)
 blah
 reno(gc)--calling a function and passing gc
end
function reno(gc)
  blah
  neon(gc)--passing gc again from our function
end
i only figured that out from when i was working on my gui library and it wasnt working correctly
in the example i passed gc from on.paint(gc) all the way to neon(gc)
you cant just say gc amywhere because it wasnt declaired anywhere else, you have to pass it as an argument.
<a href="http://www.nerdtests.com/ft_cg.php?im">
<img src="http://www.nerdtests.com/images/ft/cg.php?val=9612" alt="My computer geek score is greater than 41% of all people in the world! How do you compare? Click here to find out!"> </a>

Support Casio-Scene against the attacks of matt @ matpac.co.uk ! For more information: Casio-Scene shuts down & Matt actions threads

Offline LUAnoob

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 10
  • Rating: +0/-0
    • View Profile
Re: Error: Attempt to index local 'gc' (a nil value)
« Reply #4 on: January 31, 2012, 12:29:06 pm »
Thank you for your help. I've made it now with an true/false variable :) I will try your way and have a look which way is the best.
Sorry for bad english, i'm German.

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Error: Attempt to index local 'gc' (a nil value)
« Reply #5 on: January 31, 2012, 02:57:29 pm »
Nice to see another Lua programmer :)
In case you have any other questions, don't be afraid to ask!

Offline jwalker

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 660
  • Rating: +13/-0
  • Almost everything I have released includes a 'WZ'
    • View Profile
Re: Error: Attempt to index local 'gc' (a nil value)
« Reply #6 on: January 31, 2012, 03:00:10 pm »
your welcome, and i wish my teacher would make everyone have an nSpire :P
« Last Edit: January 31, 2012, 03:00:22 pm by jwalker »
<a href="http://www.nerdtests.com/ft_cg.php?im">
<img src="http://www.nerdtests.com/images/ft/cg.php?val=9612" alt="My computer geek score is greater than 41% of all people in the world! How do you compare? Click here to find out!"> </a>

Support Casio-Scene against the attacks of matt @ matpac.co.uk ! For more information: Casio-Scene shuts down & Matt actions threads

Offline cyanophycean314

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 363
  • Rating: +43/-1
  • It's You!
    • View Profile
Re: Error: Attempt to index local 'gc' (a nil value)
« Reply #7 on: January 31, 2012, 09:17:39 pm »
At our school, they require it by Algebra II for some reason... It's really not that much better than an 84 at that level.

Offline LUAnoob

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 10
  • Rating: +0/-0
    • View Profile
Re: Error: Attempt to index local 'gc' (a nil value)
« Reply #8 on: February 01, 2012, 06:49:41 am »
In our school we have to buy it at the 8 grade (i'm living in Germany, but here are to few users which speak german, so i ask in english ;D I hope my sentences aren't so bad and you got no problem with it that my functions partly have german names :D)


I got another question now :)

I got this code:
Spoiler For Spoiler:
Code: [Select]
money=0
energy=100
fame=0
menu = true



function on.paint(gc)


if menu then
gc:setFont("sansserif","b",15)
gc:drawString("Pennergame",0,0,"top")
gc:setFont("sansserif","b",12)
gc:drawString("1.Arbeiten (Bringt Geld)",0,80,"top")
gc:drawString("2.Ausruhen (Bringt Energie)",0,100,"top")
gc:drawString("3.Sachen kaufen (Bringt Fame)",0,120,"top")
gc:setFont("sansserif","r",6)
gc:drawString("Money: "..tostring(money),250,0,"top")
gc:drawString("Energie: "..tostring(energy),250,10,"top")
gc:drawString("Fame: "..tostring(fame),250,20,"top")
gc:setFont("sansserif","b",8)
gc:drawString("(C)Tim Krase",0,200,"top")
end

if not menu then
drawArbeiten(gc)
end

end



function on.charIn(ch)
if ch=="1" then
menu=false

elseif ch=="2" then
elseif ch=="3" then
end
end

function drawArbeiten(gc)

gc:setFont("sansserif","b",15)
gc:drawString("1.Flaschensammeln",0,80,"top")
arbeiteneingabe(gc)

end


function arbeiteneingabe(ch)
if ch=="1" then
money = money + 10
fame = fame - 5
end
end

I got now "ch=="1"" but if i press the 1 at my TI, nothing happens :( But if i press the book sign (picture
Spoiler For Spoiler:


Uploaded with ImageShack.us
:) the function "drawArbeiten(gc)" is called.What code need i there if i want that the function "drawArbeiten(gc)" is called when i press 1 ? (By the way, it is "the function is called" or "the function was called?" sry for these questions, but i want to improve my "coding" english ;D)

« Last Edit: February 01, 2012, 06:50:48 am by LUAnoob »
Sorry for bad english, i'm German.

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Error: Attempt to index local 'gc' (a nil value)
« Reply #9 on: February 01, 2012, 08:10:46 am »
Okay, first little comment about some code :D
Instead of
Code: [Select]
if something then
  --code
end

if not something then
  --code
end
Its better to do
Code: [Select]
if something then
  --code
else
  --code
end


Now, your menu does get triggered when you press '1', but you don't invalidate the screen so it doesn't get drawn. Pressing the category button will trigger this, so only then you see the menu.
To solve this you need to add platform.window:invalidate() to your code (in on.charIn for example). This sets a flag which will make that the screen gets redrawn.

Hope this helps ;D
« Last Edit: February 01, 2012, 08:12:02 am by jimbauwens »

Offline LUAnoob

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 10
  • Rating: +0/-0
    • View Profile
Re: Error: Attempt to index local 'gc' (a nil value)
« Reply #10 on: February 01, 2012, 08:14:46 am »
Thank you, that helps me a lot.
Sorry for bad english, i'm German.

Offline LUAnoob

  • LV1 Newcomer (Next: 20)
  • *
  • Posts: 10
  • Rating: +0/-0
    • View Profile
Re: Error: Attempt to index local 'gc' (a nil value)
« Reply #11 on: February 01, 2012, 12:29:03 pm »
Well, got another problem.

I want, that wenn the function "drawArbeiten(gc)" is called and if I than press the key 1, that then not "menu=false", but "money = money + 10" happens (sry, don't know another word for it ;(). How can i realize it ?
Sorry for bad english, i'm German.