• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
arone8900

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

3 posts in this topic

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[/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
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}[/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")
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[/color]
stmoveused = 1
n = 1
Wmonster = snork
Damage = 1
monDamage = 1

repeat
[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
end
until location == temple[/source]
[url="http://codeviewer.org/view/code:2785"]http://codeviewer.org/view/code:2785[/url]
0

Share this post


Link to post
Share on other sites
Look at the order in which you construct your rooms. First, you have this:

[code]

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}
[/code]

Then further down you have this:

[code]

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}
[/code]

When the [b]foresta[/b] room is constructed, the exits are set to [b]forestsnork, foresta, foresta, [/b] and [b]clearing[/b]. The problem is, [b]forestsnork[/b] and [b]clearing[/b] 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.
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0