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

Program auto-closes

Started by Nykel, 09 October 2015 - 01:05 AM
Nykel #1
Posted 09 October 2015 - 03:05 AM
script: http://pastebin.com/cxbGCT16

So I'm trying to make a text adventure but after I type in something for male/female it auto-closes the program. Any ideas?

By the way I'm using CCEmuRedux :)/>
Dustmuz #2
Posted 09 October 2015 - 06:10 AM
that is because when you press in m/f it just returns a value, if that value is false, then it ends the program, if its true, it ends the program :)/>
so no matter what you choose, it ends the program :)/>
Nykel #3
Posted 09 October 2015 - 07:14 AM
that is because when you press in m/f it just returns a value, if that value is false, then it ends the program, if its true, it ends the program :)/>
so no matter what you choose, it ends the program :)/>

Why does returning false or true end the program?
Bomb Bloke #4
Posted 09 October 2015 - 07:18 AM
That is to say, you're wanting to capture and use the return values. Switch:

male()
female()

if male == false then  -- "male" is a function, not true or false.
  print("False.")
end

if female == false then  -- "female" is a function, not true or false.
  print("False.")
end

… to:

if male() == false then  -- Test the result of executing the male function, as opposed to what male itself is.
  print("False.")
end

if female() == false then  -- Test the result of executing the female function, as opposed to what female itself is.
  print("False.")
end

Note also that this sort of syntax:

if gender == "F" or "f" then

… translates to:

if gender is "F", or "f" is a value, then...

"f" will always be a value (because you literally wrote "f" into the line), so the result will always resolve as true. You meant to do:

if gender == "F" or gender == "f" then

… and that could be simplified using string.lower():

if gender:lower() == "f" then

Heck, you could even turn this:

function male()
  if gender:lower() == "m" then
    return true
  else
    return false
  end
end

… into this:

function male()
  return gender:lower() == "m"
end

Though frankly I'd be inclined to just ditch the male/female functions altogether.
Edited on 09 October 2015 - 05:22 AM
Nykel #5
Posted 09 October 2015 - 08:34 AM
That is to say, you're wanting to capture and use the return values. Switch:

male()
female()

if male == false then  -- "male" is a function, not true or false.
  print("False.")
end

if female == false then  -- "female" is a function, not true or false.
  print("False.")
end

… to:

if male() == false then  -- Test the result of executing the male function, as opposed to what male itself is.
  print("False.")
end

if female() == false then  -- Test the result of executing the female function, as opposed to what female itself is.
  print("False.")
end

Note also that this sort of syntax:

if gender == "F" or "f" then

… translates to:

if gender is "F", or "f" is a value, then...

"f" will always be a value (because you literally wrote "f" into the line), so the result will always resolve as true. You meant to do:

if gender == "F" or gender == "f" then

… and that could be simplified using string.lower():

if gender:lower() == "f" then

Heck, you could even turn this:

function male()
  if gender:lower() == "m" then
	return true
  else
	return false
  end
end

… into this:

function male()
  return gender:lower() == "m"
end

Though frankly I'd be inclined to just ditch the male/female functions altogether.

Wow that was really helpful, thank you!

I fixed everything but the program only runs when I reboot and run from terminal.

Well I'm kinda able to use the emulator. I edit the lua code with Notepad++ and then run from terminal in emulator. Works pretty fast.
Edited on 09 October 2015 - 08:15 PM