Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 arone8900   Members   -  Reputation: 102

Like
0Likes
Like

Posted 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]
http://codeviewer.org/view/code:2785

Sponsor:

#2 arone8900   Members   -  Reputation: 102

Like
0Likes
Like

Posted 29 June 2012 - 03:50 AM

apologies the <span> stuff was to colour the code to show which bits where relevant.

#3 JTippetts   Moderators   -  Reputation: 8262

Like
1Likes
Like

Posted 29 June 2012 - 07:18 AM

Look at the order in which you construct your rooms. First, you have this:


foresta = {name = "foresta", Desc = "you look around and you are in a dark forest, something is rustling North of you"}
foresta.exit = { N = forestsnork, E = foresta, S = foresta, W = clearing}

Then further down you have this:


clearing = {name = "clearing", Desc = "you enter a small clearing, and find a small health potion"}
clearing.exit = {N = foresta, E = foresta, S = foresta, W = foresta}

When the foresta room is constructed, the exits are set to forestsnork, foresta, foresta, and clearing. The problem is, forestsnork and clearing haven't been created yet. Lua is interpreted. That means that objects will be constructed in the order they are encountered during execution. Since forestsnork and clearing don't exist yet when you try to set them to entries in foresta.exit, then instead the N and W values of foresta.exit are set to nil. Then if you move N or W, the location variable will be set to nil and the next time you try to access any members of location, you are trying to access members of a nil table and Lua will error.

Try constructing all your room tables first, then setting the various exit members once all rooms are constructed.

#4 arone8900   Members   -  Reputation: 102

Like
0Likes
Like

Posted 29 June 2012 - 07:41 AM

Thank you, you sir are a god!!!!!!!!!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS