Jump to content




Text adventure parsing help

game help

3 replies to this topic

#1 danieldigital

  • Members
  • 7 posts
  • LocationYou'll never find me!!!! hehehe

Posted 07 May 2018 - 11:41 PM

Hey! This is my first post on the forum, and I'm not really that good at Lua either, so sorry if I miss something obvious.

really crappy explanation of what it should do
Ignore that ^^ it's probably not the best way

I'm trying to make a text adventure game and there will be one word commands.
However, there will be parameters after them.
So I need to seperate the first one and know what it is, but also know what all of the rest of them are together.

Just like I said at the top, if I'm missing something, tell me.

#2 Saldor010

  • Members
  • 467 posts
  • LocationThe United States

Posted 08 May 2018 - 02:01 AM

Someone else will probably give a more detailed explanation later, but what you want to do can be achieved by using string.find along with string.sub (same website page). The basic idea of what you would do using string.find is this:

- Find the closest space in the string (using string.find)
- Remove everything from the start of the string to that point and store it in a table (using string.sub)
- Repeat first step with the new string until all spaces are gone

You should then have all of the words of your string separated into a table, from which you can do whatever you want with them.

#3 Marc1miner

  • Members
  • 8 posts

Posted 08 May 2018 - 02:09 AM

First of all you need to declare a table to store your words:
local words = { }
Then you should split the input string like this (assuming that it is user input, as you told)
string = read() 
for w in string:gmatch("%S+") do 
table.insert(words, w)        
end

The first line gets an user input, the for loop after that looks for white spaces between words and inserts them in a table.

To access those values or compare them to a set of commands do the following
Accessing: words[int] where int is the place of the word, starting at 1
Comparing: if words[int] == "moveUp" then (do some code)

I hope this clarifies a lot for you

#4 danieldigital

  • Members
  • 7 posts
  • LocationYou'll never find me!!!! hehehe

Posted 09 May 2018 - 05:38 PM

Thanks so much for the help guys!
I will post a thread when it is done.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users