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

Started by
2 comments, last by arone8900 11 years, 9 months ago
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 standard
function stmove() --standard functions
if intAA == "N" then
[color=#ff0000]location = location.exit.N
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
end
end
--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}

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")
end
end


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}

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")
end
end

forestsnork = {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")
end

end

temple = {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
stmoveused = 1
n = 1
Wmonster = snork
Damage = 1
monDamage = 1

repeat
[color=#ff0000]print (location.Desc)
[color=#ff0000]intAA = io.read()
location.move()
if Warrior.H <= 0 then
print("sadly you have died")
break
end
until location == temple[/source]
http://codeviewer.org/view/code:2785
Advertisement
apologies the <span> stuff was to colour the code to show which bits where relevant.
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.
Thank you, you sir are a god!!!!!!!!!

This topic is closed to new replies.

Advertisement