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

Auto-Variables

Started by rileysplan, 21 June 2014 - 08:38 PM
rileysplan #1
Posted 21 June 2014 - 10:38 PM
I am making a game program. It asks how many players there are, and then asks to make what the names are of the amount of players. I want to automatically take those names and turn them into a variable to track the score. My current code is.

term.write("How many Players: ")
local p = read()
for i = 1, p do
term.write("Player Name: ")
read()
end
Lignum #2
Posted 21 June 2014 - 10:44 PM
Your code won't work, because "p" is a string. So before looping you would need to convert it to a number:

local p = tonumber(read())

Anyway, you should insert all of the entered names into a table:

term.write("How many Players: ")
local p = tonumber(read())
local players = {} --# Create the table.

for i=1,p do
	 term.write("Player Name: ")
	 players[#players + 1] = read() --# Get input and insert the input into the table.
end

EDIT:
Since you're tracking scores, instead of inserting the name into the table like above, you should use the name as a key for the table.
E.g:

...
    term.write("Player Name: ")
    players[read()] = 0 --# Insert the player's name into the table and assign a value of 0 to it.
...

Now you can use the table like this:

players["James"] = 8 --# Set James's score to 8.
print(players["James"]) --# Print James's score.
Edited on 21 June 2014 - 08:48 PM
rileysplan #3
Posted 21 June 2014 - 10:59 PM
I'm sorry about this because this is my first time writing a code that is completely mine. Do the players names get imported through the player name entry, or do I have to manually enter them into the code itself?
rileysplan #4
Posted 22 June 2014 - 01:59 AM
Your code won't work, because "p" is a string. So before looping you would need to convert it to a number:

local p = tonumber(read())

Anyway, you should insert all of the entered names into a table:

term.write("How many Players: ")
local p = tonumber(read())
local players = {} --# Create the table.

for i=1,p do
	 term.write("Player Name: ")
	 players[#players + 1] = read() --# Get input and insert the input into the table.
end

EDIT:
Since you're tracking scores, instead of inserting the name into the table like above, you should use the name as a key for the table.
E.g:

...
	term.write("Player Name: ")
	players[read()] = 0 --# Insert the player's name into the table and assign a value of 0 to it.
...

Now you can use the table like this:

players["James"] = 8 --# Set James's score to 8.
print(players["James"]) --# Print James's score.


Can you filter the code to be easier to read. I cannot filter what is essential to the code and what are in-code comments.
Bomb Bloke #5
Posted 22 June 2014 - 02:18 AM
Anything after a – is a comment, and Lua will ignore it.

If using the names as table keys, then you don't need to include them in the code of your script. In Lignum's example, it'll ask for them when you run it, and put them into the table "on the fly".

If not referring into tables, then you would need to put the names directly into your code.
rileysplan #6
Posted 22 June 2014 - 02:36 AM
Anything after a – is a comment, and Lua will ignore it.

If using the names as table keys, then you don't need to include them in the code of your script. In Lignum's example, it'll ask for them when you run it, and put them into the table "on the fly".

If not referring into tables, then you would need to put the names directly into your code.
Is the code already importing the names into a table or do I have to add that code?
metron80 #7
Posted 22 June 2014 - 04:00 AM
It's Already There:

term.write("How many Players: ")
local p = tonumber(read())
local players = {} --# Create the table.
for i=1,p do
        term.write("Player Name: ")
		players[read()] = 0 --# Insert the player's name into the table and assign a value of 0 to it.
end

Now you can use the table like this:

players["James"] = 8 --# Set James's score to 8.
print(players["James"]) --# Print James's score.
Edited on 22 June 2014 - 02:00 AM
Bomb Bloke #8
Posted 22 June 2014 - 04:22 AM
Another way of organising things:

term.write("How many Players: ")
local p = tonumber(read())
local players = {} --# Create the table.
for i=1,p do
        term.write("Player Name: ")
        players[#players+1] = {["name"] = read(), ["score"] = 0}
end

-- Then you can do stuff like this:

for i=1,#players do
        print("Player "..tostring(i).." is called "..players[i].name.." and has a score of "..players[i].score..".")
end
metron80 #9
Posted 22 June 2014 - 05:01 AM
You can also check if what they inputed is actually a number.

term.write("How many Players: ")
local p = tonumber(read())
if p == nil then
    print("Not a number!")
    sleep(1)
    os.reboot()
end
theoriginalbit #10
Posted 22 June 2014 - 07:18 AM
You can also check if what they inputed is actually a number.

term.write("How many Players: ")
local p = tonumber(read())
if p == nil then
	print("Not a number!")
	sleep(1)
	os.reboot()
end
it is never a good idea to reboot a computer for something as simple as what a loop could replace

--# forward declaration of `p` to make it local but still usable outside the loop
local p
--# loop
repeat
  write("How many players: ")
  p = tonumber(read())
  --# if it wasn't a number
  if not p then
    print("Not a number!")
  end
--# until `tonumber`ing the input is a valid number
until p
print("Playing with "..p.." players")
RoD #11
Posted 22 June 2014 - 12:33 PM
You can also check if what they inputed is actually a number.

term.write("How many Players: ")
local p = tonumber(read())
if p == nil then
	print("Not a number!")
	sleep(1)
	os.reboot()
end
Or use:

type(p)
theoriginalbit #12
Posted 22 June 2014 - 12:40 PM
Or use:

type(p)
there is no point, the type of `p` will either return as "number" or "nil" meaning that a check like so

if p then
  --# code
end
would adequately test that the input was a number, assuming of course that `p` is defined like so

local p = tonumber(read())
metron80 #13
Posted 22 June 2014 - 07:30 PM
You can also check if what they inputed is actually a number.

term.write("How many Players: ")
local p = tonumber(read())
if p == nil then
	print("Not a number!")
	sleep(1)
	os.reboot()
end
it is never a good idea to reboot a computer for something as simple as what a loop could replace

--# forward declaration of `p` to make it local but still usable outside the loop
local p
--# loop
repeat
  write("How many players: ")
  p = tonumber(read())
  --# if it wasn't a number
  if not p then
    print("Not a number!")
  end
--# until `tonumber`ing the input is a valid number
until p
print("Playing with "..p.." players")

We'll yeah, you would put other code, but for example purposes, I put os.reboot().
Bubba #14
Posted 22 June 2014 - 07:37 PM
We'll yeah, you would put other code, but for example purposes, I put os.reboot().

That's not a good coding practice. Firstly, it's horribly inefficient. It takes several seconds for a whole reboot, whereas a loop allows the code to run as fast as Minecraft would allow it. Second, it's completely impractical and unrealistic in the real world. Would you EVER use a program which repeatedly rebooted your computer just to achieve a simple loop? I doubt it.

So yeah, don't do it. And please don't teach others to do it either.
Edited on 22 June 2014 - 05:38 PM
metron80 #15
Posted 22 June 2014 - 07:51 PM
We'll yeah, you would put other code, but for example purposes, I put os.reboot().

That's not a good coding practice. Firstly, it's horribly inefficient. It takes several seconds for a whole reboot, whereas a loop allows the code to run as fast as Minecraft would allow it. Second, it's completely impractical and unrealistic in the real world. Would you EVER use a program which repeatedly rebooted your computer just to achieve a simple loop? I doubt it.

So yeah, don't do it. And please don't teach others to do it either.

Ok, whatever. Won't do it again.
rileysplan #16
Posted 22 June 2014 - 11:34 PM
We'll yeah, you would put other code, but for example purposes, I put os.reboot().

That's not a good coding practice. Firstly, it's horribly inefficient. It takes several seconds for a whole reboot, whereas a loop allows the code to run as fast as Minecraft would allow it. Second, it's completely impractical and unrealistic in the real world. Would you EVER use a program which repeatedly rebooted your computer just to achieve a simple loop? I doubt it.

So yeah, don't do it. And please don't teach others to do it either.

Ok, whatever. Won't do it again.
Could I put the names that the player entered into a table so that a could reference later what player I am talking to?
Bomb Bloke #17
Posted 22 June 2014 - 11:54 PM
Like in the examples you've been given? Yes, you could do that.