• Post Reply Bookmark Topic Watch Topic
  • New Topic

Better way of handling text-based narrative  RSS feed

 
Benjamin Flintstone
Greenhorn
Posts: 2
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So i have begun to code this text-based game. I'm pretty much a beginner, working my way through a beginners book in Java and an MOOC-style online course.

I know that you can't do "GOTO"-statements in Java like in BASIC, so my solution for "jumping" across different parts of the narrative in the game looks like this:


etc til...


You guys have some tips for classes i should study in order to find better ways to do this? It just seems so lengthy to go on like this, not at all "elegant" or effective.
Laziness is a programmers virtue, right? Or am i stuck with 100+ if-statements?
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if( narraative == 1 ) // etc
Should be replaced with a switch statement.

You might want to rewrite this to load a game design from a (text?) file, rather than hard-coding the whole thing. In the file use named states instead of numbered states so that the file is easily modified, and then have your game engine be able to jump to a named state.
 
Benjamin Flintstone
Greenhorn
Posts: 2
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote: You might want to rewrite this to load a game design from a (text?) file, rather than hard-coding the whole thing. In the file use named states instead of numbered states so that the file is easily modified, and then have your game engine be able to jump to a named state.


I was thinking about doing it in XML, so the engine would take the data in different nodes in the XML-document. Which XML-parser would you use for this task?
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At this stage I would not even think of an XML parser. You have not yet worked out what you are going to do, so you cannot work out how to do it. Please explain carefully how you are going to use text to run your game. You will probably find that potential solutions come to mind as you do that.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Benjamin Flintstone wrote:So i have begun to code this text-based game.

OK, well from the look of it, it would appear to be a kind of "story" game, where the user can choose how they want to to continue/end.

My suggestion (basically the same as Campbell's):
1. StopCoding (←click).
2. Draw a "map" of the story on a piece of paper, with circles representing "parts" (maybe with a bit of text) and lines with numbers denoting which "option" gets you to the next part.

Then try and work out how you might code it. Right now, you're just trying to do it with 'if's and 'else's and that's likely to produce Spaghetti code.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Benjamin Flintstone wrote:. . .
Laziness is a programmers virtue, right? . . .

Not at all. What it means is that you spend lots of time thinking and end up writing shorter more elegant code. The more productive programmers write fewer pages of code.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!