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

Computer Is Looping Functions With No Apparent Loop

Started by popdog15, 08 October 2013 - 07:24 PM
popdog15 #1
Posted 08 October 2013 - 09:24 PM
Not sure what's going on here.
Edit: Sorry, I forgot to mention what the program was. It's a security program that'll be using a player detector, and outputting if the player is able to access it or not via a speaker. (I've not implemented the speaker speaking yet.) The way I'm trying to make it go is, player hits player detector, program checks if their name is on a line of the "authorized" file, if it is, it'll let em' through. If not, it won't.

-- sets up stuff
-- put your allowed players here:
  allowedPlayers = {"popdog15"}
pd = peripheral.wrap("right") -- pd = playerdetector
speaker = peripheral.wrap("top")
print("test")

function checkFile()
  if fs.exists("authorized") == true then
   useFile()
   print("Authorized file existing already.")
  else
   openThat = fs.open("authorized", "w")
   openThat.close()
   print("Authorized player file created!")
  end
end

function useFile()
  loadFile = fs.open("authorized", "r")
   local getPlayers = textutils.serialize(loadFile.readAll())
   local getTable = textutils.unserialize(getPlayers)
  
   loadFile.close()
   print("Got table!")
  end
  while true do
function getEvent()
   eventData = {}
   eventData = os.pullEvent()
	if eventData[1] == "player" then
	  checkAuthority(eventData[2])
   print("Checking authority!")
end
   end


function checkAuthority(player)
	 if getTable[player] == true then
   allowIn()
   print("allowed!")
  else
   deny()
   print("Access denied!")
  end
end
  end
  checkFile()


It doesn't print anything past "test". I'll right click the player-detector, and then it simply restarts. The reason I do serialize and then unserialize is because I don't know how to read a file and directly make it a table.
nobody1717 #2
Posted 08 October 2013 - 11:17 PM
so… not sure what you're doing here with the while loop right before defining getEvent. all this is doing right now is defining functions over and over (the end to the while loop is right before the last line.) If you're trying to run that function multiple times, then you actually need to loop 'getEvent()' not 'function getEvent.' but something tells me that wont be the only error. some other errors might include not defining getTable(), deny() or allowIn(), but this is hard to fix, as I don't really know how you're going about it. For easier debugging, indent properly and line up your ends. Right now it seems you just indented however much you felt like.
popdog15 #3
Posted 08 October 2013 - 11:27 PM
so… not sure what you're doing here with the while loop right before defining getEvent. all this is doing right now is defining functions over and over (the end to the while loop is right before the last line.) If you're trying to run that function multiple times, then you actually need to loop 'getEvent()' not 'function getEvent.' but something tells me that wont be the only error. some other errors might include not defining getTable(), deny() or allowIn(), but this is hard to fix, as I don't really know how you're going about it. For easier debugging, indent properly and line up your ends. Right now it seems you just indented however much you felt like.
They're indented right in my actual code but it didn't seem to copy and paste correctly. Odd. Not sure what I was thinking with that while loop in there with the functions still being defined, either. I know deny() and allowIn() aren't defined but they don't necessarily need to, yet. I don't need them to actually point somewhere, but I'll know it's working if I get an error on that line.
popdog15 #4
Posted 08 October 2013 - 11:36 PM
New weird thing going on. It's now looping "Got table!" and "Authorized file already existing!" for no apparent reason. There's no while loop, and nothing looping them otherwise that I can see.

-- sets up stuff
-- put your allowed players here:
pd = peripheral.wrap("right") -- pd = playerdetector
speaker = peripheral.wrap("top")
print("yolo")
 
function checkFile()
  if fs.exists("authorized") == true then
   useFile()
   print("Authorized file existing already.")
  else
   openThat = fs.open("authorized", "w")
   openThat.close()
   print("Authorized player file created!")
  end
end

function useFile()
  loadFile = fs.open("authorized", "r")
   local getPlayers = textutils.serialize(loadFile.readAll())
   local getTable = textutils.unserialize(getPlayers)
  
   loadFile.close()
   print("Got table!")
   getEvent()
  end
function getEvent()
    local eventData{} = os.pullEvent()
    if eventData[1] == "player" then
	  checkAuthority(eventData[2])
   print("Checking authority!")
end
   end


function checkAuthority(player)
	 if getTable[player] == true then
   print("allowed!")
  else
   print("Access denied!")
  end
end

  checkFile()
 
popdog15 #5
Posted 09 October 2013 - 04:47 PM
-bump-
Anyone know why I'm encountering this problem?
popdog15 #6
Posted 09 October 2013 - 06:52 PM
This program, which I'm trying to use for security reasons, will also greet the players w/ a speaker later, is constantly spamming "Authorized file already existing!" and "Got table!" until I get a too long until yield error.
No idea why this is happening. If you're wondering why I'm serializing and then unserializing, it's because I don't know how to make a read file directly into a table.

-- sets up stuff
-- put your allowed players here:
pd = peripheral.wrap("right") -- pd = playerdetector
speaker = peripheral.wrap("top")
   loadFile = fs.open("authorized", "r")
   local getPlayers = textutils.serialize(loadFile.readAll())
   local getTable = textutils.unserialize(getPlayers)
  
   loadFile.close()
   print("Got table!")

function checkFile()
  if fs.exists("authorized") == true then
	getEvent()
   print("Authorized file existing already.")
  else
   openThat = fs.open("authorized", "w")
   openThat.close()
   print("Authorized player file created!")
end
end
function getEvent()
	local eventData{} = os.pullEvent()
	if eventData[1] == "player" then
	  checkAuthority(eventData[2])
   print("Checking authority!")
end
end


function checkAuthority(player)
	 if getTable[player] == true then
   print("allowed!")
  else
   print("Access denied!")
end
end

  checkFile()


If you happened to see my last topic; this is a follow-up.
Bomb Bloke #7
Posted 09 October 2013 - 07:13 PM
No. I suspect you're not posting the entirety of the code you've got in this program - what's here cannot loop on its own.

There are various other problems in there that're unrelated to that. This line for eg:

local eventData{} = os.pullEvent()

I suspect that bracket placement's not legal. You may need to make it:

local eventData = {os.pullEvent()}

You declare "getTable" as local to the "useFile()" function, which means "checkAuthority()" won't have access to it. Either declare it as global (by omitting the "local" prefix), or better yet, declare it as local before you declare your functions (meaning all functions in only this program can access it).

Ideally you'd declare all variables and functions as local. Note that local functions must be declared before you attempt to call them - you'd need to reverse the order of your function declarations in your program for it to work.

How to best load your file into a table depends on how you've arranged the data within the file. For example, assuming you've got one name written per line, you might do something like:

local getTable = {}

.
.
.

local function loadTableFromFile()
  local i = 1
  loadFile = fs.open("authorized", "r")
  while true do
	getTable[i] = loadFile.readLine()
	if getTable[i] == nil then break end  -- If the last attempt to read from the file failed, stop the "while" loop.
	i=i+1
  end
end

If you already have a table in memory, it's easier to serialise it, save the result to disk as a single line, then when you want to load it later you just read that one line and unserialise it.
popdog15 #8
Posted 09 October 2013 - 07:54 PM
Thank you nice mod who merged the threads since I am stupid and double posted.
popdog15 #9
Posted 09 October 2013 - 09:35 PM
No. I suspect you're not posting the entirety of the code you've got in this program - what's here cannot loop on its own.

There are various other problems in there that're unrelated to that. This line for eg:

local eventData{} = os.pullEvent()

I suspect that bracket placement's not legal. You may need to make it:

local eventData = {os.pullEvent()}

You declare "getTable" as local to the "useFile()" function, which means "checkAuthority()" won't have access to it. Either declare it as global (by omitting the "local" prefix), or better yet, declare it as local before you declare your functions (meaning all functions in only this program can access it).

Ideally you'd declare all variables and functions as local. Note that local functions must be declared before you attempt to call them - you'd need to reverse the order of your function declarations in your program for it to work.

How to best load your file into a table depends on how you've arranged the data within the file. For example, assuming you've got one name written per line, you might do something like:

local getTable = {}

.
.
.

local function loadTableFromFile()
  local i = 1
  loadFile = fs.open("authorized", "r")
  while true do
	getTable[i] = loadFile.readLine()
	if getTable[i] == nil then break end  -- If the last attempt to read from the file failed, stop the "while" loop.
	i=i+1
  end
end

If you already have a table in memory, it's easier to serialise it, save the result to disk as a single line, then when you want to load it later you just read that one line and unserialise it.
For the record, that was the entire code. It's a pretty short program. What you suggested work, and thank you for it.