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

Get nil when I get an event

Started by Natars, 12 April 2016 - 12:48 PM
Natars #1
Posted 12 April 2016 - 02:48 PM
Hi everyone,

I am creating a program to manage my inventory (there is a warehouse with a computing system to sort my inventory by type, send and receive items) based on a client-server communication with a modem.

One of my function verify how many servers can communicate with this computer (as a ping function). I use a timer to define when it stops its verification and it show the results.

function ping()
local overtime = 5
local event, timer = ""
local contactedServers = {}
local frame, parameter, cuttedframe = {}
local continue = true

frame[1] = "-1" -- Every servers are contacted
frame[2] = localID -- ID of this computer
frame[3] = "ping"
os.sleep(1)

envoiframe(frame) -- Sending frame
timer = os.startTimer(overtime) -- Starting timer

repeat -- We contact every servers until the timer is overpassed
  event, parameter[1], parameter[2], parameter[3], parameter[4], parameter[5] = os.pullEvent()

  if event == "modem_message" then
   cuttedframe = split(parameter[4], ';') -- Cutting frame
   if cuttedframe[2] == tostring(IDlocal) and cuttedframe[3] == "pingAcknoledge" then -- If we receive an acknoledge from a
	table.insert(contactedServers, frame[3]) -- server, it is inserted in the table contactedServers
   end
  elseif event == "timer" and parameter[1] == timer then -- If the timer is overpassed, we show the results
   continue = false
  end

until continue == false

-- We show the results here
end

My problem is, when an event is detected, the program send an error
Index expected, got nil

Here is the line witch make an error

event, parameter[1], parameter[2], parameter[3], parameter[4], parameter[5] = os.pullEvent()

Can you help me please ?
CoderPuppy #2
Posted 12 April 2016 - 10:43 PM
The problem is this line:

local frame, parameter, cuttedframe = {}
That only sets `frame`, not `parameter` or `cuttedframe` because there's only one (utuk) value on the right of the '=' for three (umuk) variables on the left.

This is should have the effect you're after:

local frame, parameter, cuttedframe = {}, {}, {}

You can ignore "utuk" and "umuk" unless you want to learn about a number system.
Natars #3
Posted 13 April 2016 - 09:05 AM
Thank you very much. It work very well !