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

Variable Help

Started by iHopperDopper, 25 March 2016 - 05:31 PM
iHopperDopper #1
Posted 25 March 2016 - 06:31 PM
I'm trying to create a game that if you type the correct number in then you get so many points if you get it correct, but i'm having trouble trying to save the variables.

I'v created a directory and put everything in it. the directory is called "ButtonsPressed".

GameMenu

shell.run("delete Load-Variables")
shell.run("clear")
n=1
while true do
shell.run("clear")
print("Score:", SavedScore, " Money:", SavedMoney)
print("")
if n == 1 then
print(" > Play")
print("")
print(" Market")
print("")
print(" Exit")
elseif n == 2 then
print(" Play")
print("")
print(" > Market")
print("")
print(" Exit")
elseif n == 3 then
print(" Play")
print("")
print(" Market")
print("")
print(" > Exit")
end
a, i = os.pullEvent("key")
if i == keys.down and n < 3 then
n = n + 1
elseif i == keys.up and n > 1 then
n = n - 1
elseif i == keys.enter and n == 1 then
shell.run("ButtonsPressed/InGame")
break
elseif i == keys.enter and n == 2 then
shell.run("ButtonsPressed/InMarket")
break
elseif i == keys.enter and n == 3 then
shell.run("clear")
break
end
end

InGame

shell.run("clear")
print("Score:", SavedScore, " Money:", SavedMoney)
print(" -_-_ -_-_ -_-_ -_-_ -_-_ -_-_ -_-_ -_-_ -_-_ -_-_ -_-_")
print("")
print("Type the number you see")
sleep(4)
while true do
random = math.random(1,9)
shell.run("clear")
print("Score:", SavedScore, " Money:", SavedMoney)
print(" -_-_ -_-_ -_-_ -_-_ -_-_ -_-_ -_-_ -_-_ -_-_ -_-_ -_-_")
print("")
print("The Number is:", random)
a, i = os.pullEvent("key")
if i == keys.one and random == 1 then
SavedMoney = SavedMoney + MoneyEarnedperKey
SavedScore = SavedScore + 1
elseif i == keys.two and random == 2 then
SavedMoney = SavedMoney + MoneyEarnedperKey
SavedScore = SavedScore + 1
elseif i == keys.three and random == 3 then
SavedMoney = SavedMoney + MoneyEarnedperKey
SavedScore = SavedScore + 1
elseif i == keys.four and random == 4 then
SavedMoney = SavedMoney + MoneyEarnedperKey
SavedScore = SavedScore + 1
elseif i == keys.five and random == 5 then
SavedMoney = SavedMoney + MoneyEarnedperKey
SavedScore = SavedScore + 1
elseif i == keys.six and random == 6 then
SavedMoney = SavedMoney + MoneyEarnedperKey
SavedScore = SavedScore + 1
elseif i == keys.seven and random == 7 then
SavedMoney = SavedMoney + MoneyEarnedperKey
SavedScore = SavedScore + 1
elseif i == keys.eight and random == 8 then
SavedMoney = SavedMoney + MoneyEarnedperKey
SavedScore = SavedScore + 1
elseif i == keys.nine and random == 9 then
SavedMoney = SavedMoney + MoneyEarnedperKey
SavedScore = SavedScore + 1
elseif i == keys.q then
shell.run("ButtonsPressed/GameMenu")
break
end
end

InMarket


shell.run("clear")
print("Score:", SavedScore, " Money:", SavedMoney)
print(" -_-_ -_-_ -_-_ -_-_ -_-_ -_-_ -_-_ -_-_ -_-_ -_-_ -_-_")
print("")
print("Type the number you see")
sleep(4)
while true do
random = math.random(1,9)
shell.run("clear")
print("Score:", SavedScore, " Money:", SavedMoney)
print(" -_-_ -_-_ -_-_ -_-_ -_-_ -_-_ -_-_ -_-_ -_-_ -_-_ -_-_")
print("")
print("The Number is:", random)
a, i = os.pullEvent("key")
if i == keys.one and random == 1 then
SavedMoney = SavedMoney + MoneyEarnedperKey
SavedScore = SavedScore + 1
elseif i == keys.two and random == 2 then
SavedMoney = SavedMoney + MoneyEarnedperKey
SavedScore = SavedScore + 1
elseif i == keys.three and random == 3 then
SavedMoney = SavedMoney + MoneyEarnedperKey
SavedScore = SavedScore + 1
elseif i == keys.four and random == 4 then
SavedMoney = SavedMoney + MoneyEarnedperKey
SavedScore = SavedScore + 1
elseif i == keys.five and random == 5 then
SavedMoney = SavedMoney + MoneyEarnedperKey
SavedScore = SavedScore + 1
elseif i == keys.six and random == 6 then
SavedMoney = SavedMoney + MoneyEarnedperKey
SavedScore = SavedScore + 1
elseif i == keys.seven and random == 7 then
SavedMoney = SavedMoney + MoneyEarnedperKey
SavedScore = SavedScore + 1
elseif i == keys.eight and random == 8 then
SavedMoney = SavedMoney + MoneyEarnedperKey
SavedScore = SavedScore + 1
elseif i == keys.nine and random == 9 then
SavedMoney = SavedMoney + MoneyEarnedperKey
SavedScore = SavedScore + 1
elseif i == keys.q then
shell.run("ButtonsPressed/GameMenu")
break
end
end

Splash


shell.run("clear")
textutils.slowPrint("( _ -_-_ -_-_ -_-_ -_-_ -_-_- ) Done!")
sleep(1)
shell.run("ButtonsPressed/GameMenu")

Say You earn 100 point Score and 500 cash, i want that to be saved as SavedScore and SavedMoney
but every time i reboot the computer, it wont save the Variables and the Money and Score will pop up as nil…

Thanks
Lupus590 #2
Posted 25 March 2016 - 07:30 PM
you need to use the filesystem API: http://computercraft.info/wiki/Fs_%28API%29