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

[1.46] Run Two Things At Once & Table Help

Started by waffles05, 04 November 2012 - 01:45 AM
waffles05 #1
Posted 04 November 2012 - 02:45 AM
Hello :)/>/>

I am going to make my own GPS program in ComputerCraft. :)/>/>

Here is the code so far for the Client: :)/>/>

print("Finding Distances...")
print("|")
write("+-- Activating Modem..")
rednet.open("top") -- Opens Top Modem
print(" Done.")
print("|")
write("+-- Broadcasting Distance Request..")
rednet.broadcast("$DistanceRequest") -- Sends to all GPSs Systems a distance request
write(" Done.")
print("|")
print("+-- Waiting For Reply..")
seconds = 20
print("  |")
while seconds > 0 do
  -- Code For Recieving and Waiting
end

I need to be able to wait for 20 seconds and receive all the replies and save them to a table.

1. How do I receive rednet messages and wait at the same time?

2. How do I save messages to a table with a- Distance(part of rednet) b- Location (in message) c- ID of sender (rednet)

If this is too confusing to understand please say :D/>/>

Thanks In Advance!

waffles05 :)/>/>
Doyle3694 #2
Posted 04 November 2012 - 03:18 AM

testtable = {}
thetimer = os.startTimer(20)
while true do
   event, par1, par2, par3 = os.pullEvent()
   if event == "timer" and par1 == thetimer then
	  break
   end
   if event == "rednet_message" then
	  table.insert(testtable, {par1,par2,par3})
   end
end

I think that should work, but be my guest and test it :D/>/>
Grim Reaper #3
Posted 04 November 2012 - 06:11 AM

testtable = {}
thetimer = os.startTimer(20)
while true do
   event, par1, par2, par3 = os.pullEvent()
   if event == "timer" and par1 == thetimer then
	  break
   end
   if event == "rednet_message" then
	  testtable[#testtable+1] = {}
	  table.insert(testtable[#testtable+1], par1)
	  table.insert(testtable[#testtable+1], par2)
	  table.insert(testtable[#testtable+1], par3)
   end
end

I think that should work, but be my guest and test it :D/>/>

Don't quote me on this, but the code you provided above looks like that it will make an empty table after every message received, then proceeds to make 3 more entries for the parameters returned by os.pullEvent. I think you meant something like this:

if event == "rednet_message" then
   table.insert(testtable, {par1, par2, par3})
end
Doyle3694 #4
Posted 04 November 2012 - 07:29 AM
yeah I make a new table inside the table and then fill it up with 3 variables. But yes, didn't think of your method but it's certainly better than mine, though both will do the same thing

changeing my code to your better version
waffles05 #5
Posted 18 November 2012 - 06:38 AM
Thanks for your help guys, I'll test this code now.
I've gave you both a +1 :)/>/>

EDIT: Lots of spelling mistakes!