[Question] [Lua] How to make a program restart?
#1
Posted 14 April 2013 - 01:16 PM
http://pastebin.com/knUNhTs4
Edit: I should add that this isn't for CC. Its a LUA project, and this is the best place to look.
#2
Posted 14 April 2013 - 01:33 PM
Please send me the code on another way (for example with the messenger or with an -mail)
To your age:
I'm almost exactly your age ^^ (I'm 12)
Please don't be so strict with me.
English is not my native language.
#3
Posted 14 April 2013 - 01:43 PM
Try to explain what it is supposed to do. If I have an idea I could probably help.
~mailmanq
#4
Posted 14 April 2013 - 02:09 PM
function main() --Do your stuff end function restart() main() end restart()Your main function should call your other functions being used in the game, but so your not calling a undefined variable make sure to defined a function before calling it. Like I did in my example.
EDIT: I just looked at your code.... dear god, you need to use functions, all your doing is "if" statements everywhere..... try to do a code clean up, and put everything that you think you need to call back later into a function, that also means all the player movements, NPCs (if you have any) ect.
#5
Posted 14 April 2013 - 03:36 PM
1. You need to end all if statements
2. Use the CC wiki to learn the CC APIs
3. You call functions with functionName()
4. You should create functions and not use so many if statements
5. Learn the CC APIs
6. This:
function on.arrowLeft = arrowleft function.on.arrowRight = arrowright function.on.spacebar = spaceFunction definitions done wrong.
7. Learn the CC APIs
8. Relearn/review parts of Lua
#6
Posted 14 April 2013 - 05:59 PM
Smiley43210, on 14 April 2013 - 03:36 PM, said:
1. You need to end all if statements
2. Use the CC wiki to learn the CC APIs
3. You call functions with functionName()
4. You should create functions and not use so many if statements
5. Learn the CC APIs
6. This:
function on.arrowLeft = arrowleft function.on.arrowRight = arrowright function.on.spacebar = spaceFunction definitions done wrong.
7. Learn the CC APIs
8. Relearn/review parts of Lua
- Thing 1
- Thing 2
- Thing 3
#7
Posted 14 April 2013 - 08:19 PM
Spongy141, on 14 April 2013 - 05:59 PM, said:
Well said, but you don't have to list what you said like there, there's an option right under Size to make a list of things, like
- Thing 1
- Thing 2
- Thing 3
#8
Posted 15 April 2013 - 04:07 PM
#9
Posted 15 April 2013 - 04:13 PM
Smiley43210, on 14 April 2013 - 03:36 PM, said:
1. You need to end all if statements
2. Use the CC wiki to learn the CC APIs
3. You call functions with functionName()
4. You should create functions and not use so many if statements
5. Learn the CC APIs
6. This:
function on.arrowLeft = arrowleft function.on.arrowRight = arrowright function.on.spacebar = spaceFunction definitions done wrong.
7. Learn the CC APIs
8. Relearn/review parts of Lua
Edited by theoriginalbit, 15 April 2013 - 04:14 PM.
#10
Posted 15 April 2013 - 04:34 PM
I read the "PIL" and can't you do
local variable = io.read("*n") -- Instead of? local variable = io.read("*number")
#11
Posted 15 April 2013 - 07:48 PM
Spongy141, on 15 April 2013 - 04:34 PM, said:
I read the "PIL" and can't you do
local variable = io.read("*n") -- Instead of? local variable = io.read("*number")
In ComputerCraft you can't do either.
Quote
io:5: Unsupported format
lua> io.read("*number")
io:5: Unsupported format
#12
Posted 16 April 2013 - 02:05 AM
I was talking about in regular Lua.
#14
Posted 16 April 2013 - 05:41 AM
#16
Posted 16 April 2013 - 10:07 AM
</sarcasm>
#17
Posted 16 July 2013 - 09:36 PM
Example:
newGame==1 function main() function game() --Game Code here end game() global newGame==0 print("New game? y/n") io.read() if io.read==("y") then newGame==1 elseif io.read==("n") then newGame==0 elseif not io.read==("y") and not read ("n") then print("Please enter y or n") end end function startGame() if newGame==1 then main() elseif newGame==0 then end end startGame()
EDIT: The "read" should be "io.read". Fixed.
#18
Posted 17 July 2013 - 02:03 AM
DanJZ0404, on 16 July 2013 - 09:36 PM, said:
Example:
newGame==1 function main() function game() --Game Code here end game() global newGame==0 print("New game? y/n") io.read() if io.read==("y") then newGame==1 elseif io.read==("n") then newGame==0 elseif not io.read==("y") and not read ("n") then print("Please enter y or n") end end function startGame() if newGame==1 then main() elseif newGame==0 then end end startGame()
EDIT: The "read" should be "io.read". Fixed.
Firstly, please don't revive the dead. Leave the topics to rest in peace.
Secondly, loops are a wonderful thing, but `if` statements are not one of them. Possible loops
--# while loop while <condition> do --# code to loop end --# repeat loop repeat --# code to loop until <condition> --# incremental for loop for i = <start>, <end>, <optional increment> do --# code to loop end --# generic for loop for <key>, <value> in <iterable structure> do --# code to loop end
Thirdly, Lua has booleans, just use those instead of calling numbers a boolean.
Lastly, your program has problems.
newGame==1 --# this will be an error, you should have a single `=` there function main() --# why an anonymous function? function game() end --# to then call it here, why not just do game code here?! game() --# there is no global keyword in Lua, I have the feeling you've come from Python? also again, wrong `=` global newGame==0 print("New game? y/n") --# you should use `read`, `io.read` uses `read` so just remove the need to perform that extra function call --# you're not storing the value in a variable? o.O io.read() --# now you're checking a function pointer against a string if io.read==("y") then --# wrong `=` newGame==1 --# again, checking function pointer... what language does this even work in? elseif io.read==("n") then --# wrong `=` newGame==0 --# checking a function pointer, and then waiting for input from a user again.... actually the "n" would cause an error elseif not io.read==("y") and not read ("n") then print("Please enter y or n") end end function startGame() if newGame==1 then main() --# no need for this elseif newGame==0 then end end startGame()
Your code fixed...
local running = true function game() --# game code here end function main() game() print("New game? y/n") local input = read():lower() while input ~= "y" or input == "n" do print("Please enter y or n") input = read():lower() end running = input == "y" end function startGame() while running do main() end end startGame()
The best (and most commonly used) code structure for a game...
local quitRequested = false local function update() --# increment any variables and such end local function render() --# render any changes that have been made end local function processInput() os.startTimer(0.1) --# allows the game to move on and not get stuck here. local e = os.pullEventRaw() --# process the event that was received if e[1] == "key" and e[2] == keys.q then quitRequested = true end end local function main() render() repeat processInput() update() render() until quitRequested end main()
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users