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

[Question] [Lua] How to make a program restart?

Started by Kron, 14 April 2013 - 12:16 PM
Kron #1
Posted 14 April 2013 - 02:16 PM
Hey guys. I'm currently creating a 2d video game. Although the code does not scream "Professional Coder", I have to start somewhere. Also, considering that I am only the age of 13, I suppose that this is a pretty decent start, if I want to get into a coding career in my future. However, I need to be able to create a function to restart the game. I dont even know where to begin with this, but this is what I got so far.

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.
H4X0RZ #2
Posted 14 April 2013 - 02:33 PM
I think I can help you but I get an error if I want to go on a pastebin link.
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.
Mailmanq! #3
Posted 14 April 2013 - 02:43 PM
This code is very messed up. I don't even know where to start. It has so much code that makes me explode because it uses undeclared variables and such. It looks like you just made up functions that you think should exist and used them, but didn't even make them. Also giving your age is a bad idea, because people think of you differently when they know of it. This is something you can't control. It is best just not to tell.

Try to explain what it is supposed to do. If I have an idea I could probably help.

~mailmanq
Spongy141 #4
Posted 14 April 2013 - 03:09 PM
If you want your game to restart just call you main function, like this


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.
Smiley43210 #5
Posted 14 April 2013 - 04:36 PM
Okay…what the heck?

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 = space
Function definitions done wrong.
7. Learn the CC APIs
8. Relearn/review parts of Lua
Spongy141 #6
Posted 14 April 2013 - 06:59 PM
Okay…what the heck?

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 = space
Function definitions done wrong.
7. Learn the CC APIs
8. Relearn/review parts of Lua
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
  1. Thing 1
  2. Thing 2
  3. Thing 3
And it looks nicer.
Smiley43210 #7
Posted 14 April 2013 - 09:19 PM
-snip-
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
  1. Thing 1
  2. Thing 2
  3. Thing 3
And it looks nicer.
I know, but I've always preferred to do without it. Besides, it takes up more space (which makes it look nicer to some, but to me it looks…meh).
Sariaz #8
Posted 15 April 2013 - 05:07 PM
Of topic but I would personally suggest starting with other programs like making text come up with monitors etc not a game as games are quite hard especially if you are unfamiliar with coding, but kudos for deciding to try programing at your age.
theoriginalbit #9
Posted 15 April 2013 - 05:13 PM
Okay…what the heck?

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 = space
Function definitions done wrong.
7. Learn the CC APIs
8. Relearn/review parts of Lua
Additionally to this great summary by Smiley43210, read the PIL… If you want to become a programmer one day you will have to get used to reading developer documentation for languages, I do it almost every day… Sometimes its annoying, sometimes the documentation is bad, but to know the language it has to be done, and I will say that the PIL is one of the best that I have read.
Edited on 15 April 2013 - 03:14 PM
Spongy141 #10
Posted 15 April 2013 - 05:34 PM
^
I read the "PIL" and can't you do

local variable = io.read("*n")

-- Instead of?

local variable = io.read("*number")
Bubba #11
Posted 15 April 2013 - 08:48 PM
^
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.

lua> io.read("*n")
io:5: Unsupported format
lua> io.read("*number")
io:5: Unsupported format
Spongy141 #12
Posted 16 April 2013 - 03:05 AM
^
I was talking about in regular Lua.
Bubba #13
Posted 16 April 2013 - 06:29 AM
^
I was talking about in regular Lua.

Considering that this is the questions forum for ComputerCraft, not regular Lua, I thought it was best that I specify.
Zudo #14
Posted 16 April 2013 - 06:41 AM
Lol guys im ten!
Bubba #15
Posted 16 April 2013 - 07:36 AM
Lol guys im ten!

Thank you for telling us. I find it vital to know the age of everybody on these forums; maturity is unimmportant.

I'm sure that your young age signifies genius.
Spongy141 #16
Posted 16 April 2013 - 11:07 AM
You for got to do
</sarcasm>
Zee #17
Posted 16 July 2013 - 10:36 PM
This code hurts me, even looking in a small segment. I'm 12 and I know how to do this. Functions are a wonderful thing. As are loops besides for the "if" loop. Either way, I might as well help you. First, put all of your game inside ONE function. At the end of the function, add a yes/no prompt and a variable containing a boolean value. Said boolean should equal 0 if they typed "n" and 1 if it was "y" then, after the end of the function, call it. Then set up an if statement that will run the function or exit the program.

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.
theoriginalbit #18
Posted 17 July 2013 - 03:03 AM
This code hurts me, even looking in a small segment. I'm 12 and I know how to do this. Functions are a wonderful thing. As are loops besides for the "if" loop. Either way, I might as well help you. First, put all of your game inside ONE function. At the end of the function, add a yes/no prompt and a variable containing a boolean value. Said boolean should equal 0 if they typed "n" and 1 if it was "y" then, after the end of the function, call it. Then set up an if statement that will run the function or exit the program.

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.
Nope, nope, nope, nope, nope!

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()