Hi, I'm a long time python programmer and just began programming in Lua(I might like it better than python). How might I began to program a text adventure/IF in Lua? Any tips, Pointers? I have a basic knowledge of Lua and have began to read this great e-book called "Programming in Lua" by Roberto Lerusalimschy.

Any good lua text adventure tutorials on the world wide web?

much appreciated,

Python User

Dani AI

Generated

As recommended, start with design before typing lots of code: define the data model (rooms, objects, player state), list the verbs the game will accept, and sketch how actions change state. Keep the world tiny and iterate — that keeps complexity manageable and helps avoid spaghetti code. Roberto Ierusalimschy’s Programming in Lua is the best concise reference for Lua idioms such as using tables and functions for lightweight object systems. (lua.org)

A minimal architecture is: a table of rooms, a table of items, a player state, and a commands dispatch table that maps verbs to functions. Start with a very simple parser and expand only when needed; for robust grammars use LPeg. Example starter pattern and dispatch:

rooms = {
  hall = { desc = "A long hall.", exits = { north = "kitchen" } },
  kitchen = { desc = "A small kitchen.", exits = { south = "hall" } }
}

player = { location = "hall", inventory = {} }

commands = {
  look = function(target)
    if target == "" then print(rooms[player.location].desc)
    else print("Nothing special about " .. target) end
  end
}

function parse(input)
  local verb, rest = input:match("^(%w+)%s*(.*)$")
  if verb and commands[verb] then commands[verb](rest)
  else print("I don't understand that.") end
end

For natural-language-like commands consider LPeg for parsing. (luarocks.org)

Useful tools and examples: ZeroBrane Studio for Lua debugging, LÖVE (Love2D) if the project grows toward a GUI, and the INSTEAD engine as a real-world Lua-based IF system to study. ’s pointer to Mat Buckland’s sample code is a good reminder to learn from examples but not copy them verbatim. Start tiny, write tests for the parser/commands, and refactor into modules as features are added. (studio.zerobrane.com)

Recommended Answers

All 3 Replies

> How might I began to program a text adventure/IF in Lua?
The same way you would in any other programming language.

There is so much more to writing a program than just figuring out the syntax of your current implementation language.
http://en.wikipedia.org/wiki/Software_development_process

There is no more a "how to write an adventure game" than there are "how to write a mobile phone" or "write an air traffic control system". At the fundamental level, they're all the same (except for the size of the problem).

First you figure out "what" you want.
Then you figure out "how" you're going to do it (in several iterations usually, getting more detailed each time).
Then you can start writing some code.

Once you've actually figured out the "how to program" step, then the problem and implementation are details.

Think "learning to drive". You only do this once.
Then "go fast, drive Ferrari" is one problem and implementation
and "move stuff, drive van" is another.
Sure there are a few details, and a bit of practice, but the basics should be there for either (or other) case.

> How might I began to program a text adventure/IF in Lua?
The same way you would in any other programming language.

There is so much more to writing a program than just figuring out the syntax of your current implementation language.
http://en.wikipedia.org/wiki/Software_development_process

There is no more a "how to write an adventure game" than there are "how to write a mobile phone" or "write an air traffic control system". At the fundamental level, they're all the same (except for the size of the problem).

First you figure out "what" you want.
Then you figure out "how" you're going to do it (in several iterations usually, getting more detailed each time).
Then you can start writing some code.

Once you've actually figured out the "how to program" step, then the problem and implementation are details.

Think "learning to drive". You only do this once.
Then "go fast, drive Ferrari" is one problem and implementation
and "move stuff, drive van" is another.
Sure there are a few details, and a bit of practice, but the basics should be there for either (or other) case.

Thanks. I'll take that into consideration when i make my IF game. :)

You might find interesting source code samples from the book "Programming AI by Example" from Matt Buckland.

Check out the Lua projects. Do not copy paste. Much of that code is educational and a graphic adventure probably requires a pretty good design to avoid spaguetti patterns.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.