Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 29 Jun 2012
Offline Last Active Jun 29 2012 07:41 AM

Topics I've Started

[solved] attempt to index global 'location' (a nil value) Help please!

29 June 2012 - 03:48 AM

Been re reading this code so many times and can't figure out the problem, been tearing my hair out, any suggestions/solutions would be greatly appreciated.
Ignore the naff attempts at story writing its just filler till I get the engine working.
I've highlighted the area I believe the problem is in.
full code attatched

Basically when location = location.exit.(N,S, E or W) is run its meant to change the players location to a different place, in the current location two of these exits lead back to the same place, these commands work, but when I type N or W it says "attempt to index global 'location' (a nil value)", now changing location = location.exit.W to location = clearing it works but if I did it this method then I would have to right masses of code I'm trying to shorten it so each location has a set of exit values which link up to a standard move function thus dramatically reducing code.
I'm not being lazy I just want to get in the habit of writing concise readable code.

In advance thanks for even reading this.

relevant code: (Lua code)
[source lang="plain"]--move standardfunction stmove() --standard functions if intAA == "N" then [color=#ff0000]location = location.exit.N[/color] elseif intAA == "E" then location = location.exit.E elseif intAA == "S" then location = location.exit.S elseif intAA == "W" then location = location.exit.W elseif intAA == "statcheck" then charsheet() elseif intAA == "showi" then Inv.display() elseif intAA == "help" then print(help()) else stmoveused = 0 endend--places oooooooo repeatr .exit = {N = , E = , S = , W = } = {name = "", Desc = ""}foresta = {name = "foresta", Desc = "you look around and you are in a dark forest, something is rustling North of you"}[color=#ff0000]foresta.exit = { N = forestsnork, E = foresta, S = foresta, W = clearing}[/color]function foresta.move() stmoveused = 1 stmove() if intAA == "examine forest" then print("\n".."you look around and see a sea of fir trees coating in small fern skirts, the sun looms \nhigh and the blossoming green of the leaves suggests the season to be early spring.") elseif intAA == "smell forest" then print("the smell of ferns and dry leaves freshens your senses") elseif stmoveused == 1 then _ = 4 else print("\n","unknown command") endendclearing = {name = "clearing", Desc = "you enter a small clearing, and find a small health potion"}clearing.exit = {N = foresta, E = foresta, S = foresta, W = foresta}function clearing.move() stmoveused = 1 stmove() if intAA == "examine clearing" then print("\n" .. "you see nothing of note") elseif intAA == "smell forest" then print("still smells like the forest") elseif stmoveused == 1 then _ = 4 else print("\n" .. "unknown command") endendforestsnork = {name = "forestsnork", Desc = "A wild snork sharply shoots its eye's towards you, its looks angered by your presence" }forestsnork.exit = {N = temple, E = forestsnork, S = foresta, W = forestsnork}function forestsnork.move() stmoveused = 1 stmove() if intAA == "examine snork" then print("\n" .. "a pudgy little creature looks at you angryly") elseif intAA == "smell forest" then print("\n" .. "smells like snork poo") elseif stmoveused == 1 then _ = 4 else print("\n" .. "unknown command") endendtemple = {name ="temple", Desc = "you have found the holy grail well done, you may now win",}print ("\n".."welcome to the land of spud you are an adventurer")[color=#ff0000]location = foresta[/color]stmoveused = 1n = 1Wmonster = snorkDamage = 1monDamage = 1repeat [color=#ff0000]print (location.Desc)[/color] [color=#ff0000]intAA = io.read() location.move()[/color] if Warrior.H <= 0 then print("sadly you have died") break enduntil location == temple[/source]