Jump to content




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


17 replies to this topic

#1 Kron

  • Members
  • 25 posts

Posted 14 April 2013 - 01: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.

#2 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 14 April 2013 - 01: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.

#3 Mailmanq!

  • Members
  • 123 posts
  • LocationI am omnipresent... DUH

Posted 14 April 2013 - 01: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

#4 Spongy141

  • Members
  • 526 posts
  • Location'Merica

Posted 14 April 2013 - 02: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.

#5 Smiley43210

  • Members
  • 204 posts

Posted 14 April 2013 - 03: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

#6 Spongy141

  • Members
  • 526 posts
  • Location'Merica

Posted 14 April 2013 - 05:59 PM

View PostSmiley43210, on 14 April 2013 - 03:36 PM, said:

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
  • Thing 1
  • Thing 2
  • Thing 3
And it looks nicer.

#7 Smiley43210

  • Members
  • 204 posts

Posted 14 April 2013 - 08:19 PM

View PostSpongy141, on 14 April 2013 - 05:59 PM, said:

-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
  • Thing 1
  • Thing 2
  • 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).

#8 Sariaz

  • Members
  • 107 posts

Posted 15 April 2013 - 04: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.

#9 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 15 April 2013 - 04:13 PM

View PostSmiley43210, on 14 April 2013 - 03:36 PM, said:

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 by theoriginalbit, 15 April 2013 - 04:14 PM.


#10 Spongy141

  • Members
  • 526 posts
  • Location'Merica

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 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 15 April 2013 - 07:48 PM

View PostSpongy141, 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

lua> io.read("*n")
io:5: Unsupported format
lua> io.read("*number")
io:5: Unsupported format


#12 Spongy141

  • Members
  • 526 posts
  • Location'Merica

Posted 16 April 2013 - 02:05 AM

^
I was talking about in regular Lua.

#13 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 16 April 2013 - 05:29 AM

View PostSpongy141, on 16 April 2013 - 02:05 AM, said:

^
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.

#14 Zudo

  • Members
  • 800 posts
  • LocationUK

Posted 16 April 2013 - 05:41 AM

Lol guys im ten!

#15 Bubba

    Use Code Tags!

  • Moderators
  • 1,142 posts
  • LocationRHIT

Posted 16 April 2013 - 06:36 AM

View PostZudoHackz, on 16 April 2013 - 05:41 AM, said:

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.

#16 Spongy141

  • Members
  • 526 posts
  • Location'Merica

Posted 16 April 2013 - 10:07 AM

You for got to do
</sarcasm>

#17 Zee

  • Members
  • 42 posts
  • LocationIndiana

Posted 16 July 2013 - 09: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.

#18 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 17 July 2013 - 02:03 AM

View PostDanJZ0404, on 16 July 2013 - 09:36 PM, said:

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






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users