This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
WarriorDB's profile picture

How to store user input?

Started by WarriorDB, 14 June 2014 - 09:58 AM
WarriorDB #1
Posted 14 June 2014 - 11:58 AM
I am trying to make a rpg type game, I'm still kind of noobish at computercraft and my code is bunched up and I don't tab. I'm trying to get my game to remember what my player picks for the first question, what weapon do you choose sword or bow? So that I can use it for skill trees. Help?
WarriorDB #2
Posted 14 June 2014 - 12:06 PM
pastebin get HTEgGnKF game also this is my code if you would like to see and or edit it.
Bomb Bloke #3
Posted 14 June 2014 - 12:15 PM
A link, for convenience.

When you're getting user input, you're storing it in a variable called "input". You can have lots of variables - generally, whenever you type in a word Lua doesn't understand, it'll assume you want to treat that term as a new variable.

So, consider using a variable called "weapon" (instead of "input") when asking the player about their starting weapon, then take care not to overwrite that new "weapon" variable later.
WarriorDB #4
Posted 14 June 2014 - 12:25 PM
Ok I get it, weapon = read() and so on thanks for the info, I learned about input = read() in a simple unhackable computer lock and I thought you had to use input each time, thanks.
Lignum #5
Posted 14 June 2014 - 03:51 PM
You should also prefix your variables with "local" the first time they're declared. Otherwise, they're considered global and all programs will have access to them.
E.g:

local input = read() --# Store the read value in "input"
input = read() --# Do it again. Local is not required because "input" already exists.

EDIT:
Looking through your code, you can simplify stuff like

if input == "left" or input == "Left" then
with this:

if input:lower() == "left" then

input:lower() is the same as string.lower(input) and will set all characters in the string to their lower case equivalent.
E.g: string.lower("HeLLo") -> "hello"
Edited on 14 June 2014 - 01:55 PM
Neywiny #6
Posted 15 June 2014 - 02:32 AM
may be a bit late on the help train, but tables would be great for an rpg game. for example, you'd save the current table row to a variable, then you'd do something like

curLine = 1
while true do
    tInput[curLine] = read()
    curLine = curLine+1
end
which would store every user input into a table. for weapons and things, you could use a table for that. something like this

tWeapon = {}
tWeapon["name"] = "Sauron's Flame"
tWeapon["damage"] = 10
tWeapon["durability"] = uses
obviously the variable uses would have to be defined, but something like that would work