r/ComputerCraft • • Jan 24 '17

Computercraft code help

The code lies here: http://pastebin.com/FcJcPg3s

2 Upvotes

7 comments sorted by

View all comments

2

u/ValorCat Jan 25 '17

I'm not completely sure I understand what you're trying to do, but right away I see this:

input = username

This is setting the variable input to the value of username, which is nil. You want it the other way around:

username = input

Or really just:

username = read()

to save yourself a line. Same thing with setting the password.

This won't let you access the username and password variables in another file, if that's what you're going for, but it's a start. If you want to access the username and password in another file, you have to either

  • make the other file an API, then load it into this file with os.loadAPI(name), or

  • save the username and password into a file, then run have the other program access the file.

Does that make sense? Feel free to ask me questions; I'm a programmer, but it's been a little while since I've done Lua or ComputerCraft.

2

u/dragon53535 Jan 25 '17

Globals

If he wants it to be accessed by another file and he hasn't turned off the computer, he can use globals. However if he's just going to run the other program from the first, he can pass them as arguments to the second program.

1

u/ValorCat Jan 25 '17

Oh, do globals actually persist between files? Do they have to be loaded with os.loadAPI or is it built-in? TIL

3

u/dragon53535 Jan 25 '17

They persist in the global environment for the computer. Only saved during the time the computer is on.

0

u/[deleted] Jan 25 '17

[deleted]

2

u/dragon53535 Jan 26 '17 edited Jan 26 '17

Global variables DO transfer between files in Lua for as long as the lua shell is running. In the case of CraftOS, global variables exist between all programs in the computer until the computer restarts. The only time this is not applicable is if you change the environment the global variable is declared in.

What I believe you're confused about is persistance, global variables and their values only exist for the execution cycle of the computer. When the computer turns off, whatever value that was stored is gone.

Edit: To sort of just kill the debate: A program is essentially a function being run by the shell. Global variables ignore scope completely, while locals do not. Since each program is a separate function, their locals are not available to each other, but globals which ignore scope entirely are able to be read.

0

u/[deleted] Jan 26 '17

[deleted]

1

u/dragon53535 Jan 26 '17

The feature of globals? Likely something slow as it's faster to have local variables.