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

Updating variable in running script

Started by Choppie, 19 May 2012 - 08:08 PM
Choppie #1
Posted 19 May 2012 - 10:08 PM
I am just creating a small program to check what users have been created. At the top of this, I have a count (Users found: <variable>/4). However, I have not found a way to update that variable once the script runs past that point, which means if I +1 to the variable later in the script, it stays as 0/4.



local count = "0"
print("Searching for users...")
sleep(1)
print("Users found: ",count,"/4")
if user1 == "" then
 print("User 1 not found!")
 sleep(1)
else
 print("User: ",user1," found!")
 count = count + 1
 print(count)
 sleep(1)
end

The variable is called "count". I set it to 0 at the beginning, and then +1 it when it finds User 1. When I reprint the variable (for testing purposes) where it says "print(count)", it recognises that count = 1. But it doesn't update at the top, on line 4.

How do I update variables in a running script?
Luanub #2
Posted 19 May 2012 - 10:35 PM
It's due to the fact that when that line was printed count == 0. Do you have the user names stored in a table or something that you are checking against? You probably want to place it in a loop, or do the check again later and reprint the line.

Here's how to reprint. If you're checking against a table you'll need a couple of loops and I can help you with that if you need.

local count = "0"
print("Searching for users...")
sleep(1)
print("Users found: ",count,"/4")
if user1 == "" then
print("User 1 not found!")
sleep(1)
else
print("User: "..user1.." found!")
count = count + 1
print(count)
term.setCursorPos(x, y) replace x and y with where you are printing Users Found: #/4
write (""Users found: "..count.."/4")
sleep(1)
end

Here's an example to try if you're using a table

local count = "0"
local x = 1
local usernames = {} -- pretend table of user names

for x=1, 4 do -- for loop to repeat 4 times, assuming your going to have a max of 4 users.
print("Searching for users...")
sleep(1)
print("Users found: ",count,"/4")
for var, user in pairs(usernames) do  -- for loop to check the names in the table
if user == "" then
print("User "..x.." not found!")
sleep(1)
else
count = count + 1
print("User: "..usernames[count].." found!")
print(count)
sleep(1)
end
x = x + 1
end
end

Hopefully these will give you some idea's. The table code will probably need some slight changes depending on how your table is setup.
Edited on 19 May 2012 - 08:46 PM
Mads #3
Posted 29 June 2012 - 07:44 PM
You cannot do this: count = "0" , and then later try and do an arithmetic operation with that variable.
MysticT #4
Posted 29 June 2012 - 08:51 PM
You cannot do this: count = "0" , and then later try and do an arithmetic operation with that variable.
Yes you can:
Lua provides automatic conversion between string and number values at run time. Any arithmetic operation applied to a string tries to convert this string to a number, following the usual conversion rules. Conversely, whenever a number is used where a string is expected, the number is converted to a string, in a reasonable format. For complete control over how numbers are converted to strings, use the format function from the string library.