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

Sending and saving ID's for communication

Started by TechMasterGeneral, 22 January 2014 - 01:38 PM
TechMasterGeneral #1
Posted 22 January 2014 - 02:38 PM
So i'm creating a set of programs that use computers to control an ICBM bunker. Right now I need to store and send user created ID's to a control panel computer that can store them and call on them for when a user tells a missile to launch. If anyone knows how to link a user created name and send the corresponding computer ID so that when a player calls
 launch <ID or Label>
that the computer can discern the what computer to send the launch command to.
I pushed the code i'm working on here:
https://bitbucket.org/REXMC/ccmissilecontrol/src/25f16cb3835f2763448460ef34a6f19a60a6200a/InDev/?at=develop
Thanks in advance!
Edited on 22 January 2014 - 02:06 PM
CometWolf #2
Posted 22 January 2014 - 02:49 PM
Store the ids in a table, indexed by their labels.

tIds = {
  ["derp"] = 20,
  ["derpy"] = 35
}
print(tIds["derpy"])
Prints 35
TechMasterGeneral #3
Posted 22 January 2014 - 03:41 PM
Store the ids in a table, indexed by their labels.

tIds = {
  ["derp"] = 20,
  ["derpy"] = 35
}
print(tIds["derpy"])
Prints 35

Forgive me if i'm being ignorant and not seeing how to integrate the user input aspect and transmitting with modem.transmit()… I need the ids to be sent from multiple different computers then stored with the replyChannel from the modem_message event.
CometWolf #4
Posted 22 January 2014 - 04:07 PM
So something like this then?

local tIds = {}
while true do
  local launched = false
  local _e, _side, recChan, resChan, msg = os.pullEvent"modem_message" -- get modem messages
  msg = msg:lower() -- convert to lower case
  for k,v in pairs(tIds) do -- check ids table for received label
    if msg == "launch"..k then --if the label is already stored with a id then
	  modem.transmit(v,v,"LAUNCH YO MISSLE!") -- launch message
	  launched = true
	  break --end loop
    end
  end
  if not launched then
    tIds[msg] = resChan --adds label with id to table
  end
end
TechMasterGeneral #5
Posted 22 January 2014 - 04:12 PM
So something like this then?

local tIds = {}
while true do
  local launched = false
  local _e, _side, recChan, resChan, msg = os.pullEvent"modem_message" -- get modem messages
  msg = msg:lower() -- convert to lower case
  for k,v in pairs(tIds) do -- check ids table for received label
	if msg == "launch"..k then --if the label is already stored with a id then
	  modem.transmit(v,v,"LAUNCH YO MISSLE!") -- launch message
	  launched = true
	  break --end loop
	end
  end
  if not launched then
	tIds[msg] = resChan --adds label with id to table
  end
end

That looks like what i'm looking for… i'm going to have to try that and see if it works
Thanx!
TechMasterGeneral #6
Posted 22 January 2014 - 06:25 PM
Just a quick question…
what does the k,v do/mean in the code?
CometWolf #7
Posted 22 January 2014 - 06:32 PM
k and v is the values returned by the pairs iterator in the for loop. In this case the k is the key of the table and v is the variable held in that key. The loop loops once for eveyr key in the table.
Edited on 22 January 2014 - 05:32 PM
TechMasterGeneral #8
Posted 22 January 2014 - 07:14 PM
Thank you for all the help! I'm going to add you in the credits for my project.
TechMasterGeneral #9
Posted 30 January 2014 - 10:08 AM
So… I'm still having problems with my code… I'm not quite sure why… you can get the files here and try them out… when i try and call for the id from the table it returns like this
 table: f79dbie9s 
CometWolf #10
Posted 30 January 2014 - 10:32 AM
You realize that this

modem.transmit( sChan, rChannel, "/ tostring(f.readAll()) /" )
Dosen't actually transmit your file contents, right?
How did you try calling the id btw?
TechMasterGeneral #11
Posted 30 January 2014 - 02:22 PM
You realize that this

modem.transmit( sChan, rChannel, "/ tostring(f.readAll()) /" )
Dosen't actually transmit your file contents, right?
How did you try calling the id btw?
Oh… yah… I just put it there as a shot to see if it would…
The id was something like launch1 or something like that…. i wanted it to store the rChannel variable as equal to sending the computer id
Edited on 30 January 2014 - 01:23 PM
CometWolf #12
Posted 30 January 2014 - 02:31 PM
That dosen't even make any sense… You tried to call a function within a string.
Just get rid of the quotation marks and the slashes

modem.transmit( sChan, rChannel, tostring(f.readAll()) )
TechMasterGeneral #13
Posted 03 February 2014 - 12:21 PM
Everytime i try to print out the table once it's recived it does this

table:c645670
So i don't think it is actually pulling anything out of the table…
is there a better way to do this…? without using a table?
I tried using a file… but i couldn't get it working either…
surferpup #14
Posted 03 February 2014 - 12:52 PM
You can't print out a table this way.

try this:


local myTable = {
  key1 = "value1";
  key2="value2";
}
for k,v in pairs(myTable) do
  print(k..":  "..v)
end

or this


for k,v in next,myTable do
  print(k..":  "..v)
end

or this


for i,v in ipairs(myTable) do
  print(i..":  "..v)
end
Edited on 03 February 2014 - 12:00 PM
TechMasterGeneral #15
Posted 05 February 2014 - 10:19 AM
You can't print out a table this way.

try this:


local myTable = {
  key1 = "value1";
  key2="value2";
}
for k,v in pairs(myTable) do
  print(k..":  "..v)
end

or this


for k,v in next,myTable do
  print(k..":  "..v)
end

or this


for i,v in ipairs(myTable) do
  print(i..":  "..v)
end

I've been doing the top one… but it doesn't work for some reason…
surferpup #16
Posted 05 February 2014 - 10:23 AM
I've been doing the top one… but it doesn't work for some reason…

Please post your code. I have tested all three. They work fine.
TechMasterGeneral #17
Posted 05 February 2014 - 10:26 AM
Here is the repository it is in… feel free to comment on it
https://bitbucket.or...Dev/?at=develop

The directory with the files i'm working on is indev

Right link:
https://bitbucket.org/REXMC/ccb/src/ca22092b11a1efcc5012f6037d45c257aa45af88/InDev/?at=master
Edited on 05 February 2014 - 09:27 AM
surferpup #18
Posted 05 February 2014 - 10:34 AM
That is unhelpful, as I have no where to begin troubleshooting. Please post relevant sections of code for us to comment on this specific issue. I will take a look at the rest if I have time.

As it is you have three lua files – which one is giving you trouble? Where?
Edited on 05 February 2014 - 09:35 AM
TechMasterGeneral #19
Posted 05 February 2014 - 10:45 AM
Its really all the files… but it think it is from the server
here is the code that doesn't work:

  local tIds = {}
while true do
  local launched = false
  local _e, _side, recChan, resChan, msg = os.pullEvent("modem_message") -- get modem messages
  msg = msg:lower() -- convert to lower case
  for k,v in pairs(tIds) do -- check ids table for received label
        if msg == "launch"..k then --if the label is already stored with a id then
            print("Launch command recieved from:"..resChan)
          modem.transmit(v,v,"launch") -- launch message
          launched = true
          break --end loop
        end
  end
  if not launched then
        msg = tostring(msg)
        tIds[msg] = resChan --adds label with id to table
        print(tIds)
  end
end      
Edited on 05 February 2014 - 09:45 AM
surferpup #20
Posted 05 February 2014 - 10:48 AM
Okay, I think you have a logic issue here.

Pay attention to your table tIds. I don't think it ever gets values.
TechMasterGeneral #21
Posted 05 February 2014 - 11:19 AM
Okay, I think you have a logic issue here.

Pay attention to your table tIds. I don't think it ever gets values.
let me run a debug using the textutils.serialize then output it to a file and see…
CometWolf #22
Posted 05 February 2014 - 11:37 AM
He's printing the table wrong. Basically he just flat out ignored you lol.
Here's the relevant section with surf's suggestion for printing the table contents.

  if not launched then
	    msg = tostring(msg) -- this is unessacary, as a msg received via rednet is always a string.
	    tIds[msg] = resChan --adds label with id to table
	    for k,v in pairs(tIds) do
		  print(k..": "..v)
	    end
  end

As for the table not ever getting any values, i can't really see why it wouldn't, unless he's not specifying a reply channel.
TechMasterGeneral #23
Posted 05 February 2014 - 12:41 PM
He's printing the table wrong. Basically he just flat out ignored you lol.
Here's the relevant section with surf's suggestion for printing the table contents.

  if not launched then
		msg = tostring(msg) -- this is unessacary, as a msg received via rednet is always a string.
		tIds[msg] = resChan --adds label with id to table
		for k,v in pairs(tIds) do
		  print(k..": "..v)
		end
  end

As for the table not ever getting any values, i can't really see why it wouldn't, unless he's not specifying a reply channel.

I'm specifying a reply channel…it is the channel it recieved the message from… and i used the textutils.serialize() to but the table in a file but it wouldn't output it to a file..
Edited on 05 February 2014 - 11:42 AM
CometWolf #24
Posted 05 February 2014 - 01:00 PM
I didn't say you were, i was just saying that was the only case i could think of where no ids would be stored in the table. Again, we can't help you with stuff that dosen't work, unless you actually post it, but forget about that for now and try surf's suggestion.
TechMasterGeneral #25
Posted 11 February 2014 - 07:16 AM
Ok… i think i have it working now… thanks for the help guys