1715 posts
Location
ACDC Town
Posted 28 November 2015 - 12:54 AM
I've been trying this for quite a while, but never to any avail. I want my minimap program to broadcast its coordinates (configurable) to any other minimap client, so they can appear on the map. What I've done is have it constantly rednet.broadcast() a table of all relevant data (x,y,z,name,whatever) for other map clients to pick up and put into a file so it can be displayed. But I'm having trouble with the receiving part. It needs to receive tables from multiple sources, and not store any redundant names/coordinates. Can someone help code that part?
Here's the link. Look at lines 2223 to 2287. It will run without error, just not as intended (as of yet).
3057 posts
Location
United States of America
Posted 28 November 2015 - 01:04 AM
First thing I noticed was you making a n00b mistake on line 2281, passing the result of sleep( 3 ) to parallel. Change that line to
parallel.waitForAny( function() sleep( 3 ) end, _rawListenForMapData )
Second thing I notice is you having a sleep(0) on line 2285. Why is that there? parallel.waitForAny yields, so you couldn't possibly have put that there to prevent 'too long without yielding'… right?
Edited on 28 November 2015 - 12:05 AM
7083 posts
Location
Tasmania (AU)
Posted 28 November 2015 - 02:05 AM
Well, this seems to be what you've got right now:
Spoiler
function _rawListenForMapData()
otherWaypoints = {}
while true do
id, data = rednet.receive(mapProtocol,2)
if data.name and data.x and data.y and data.z and data.name then
local newpoint = {data.name,data.x,data.y,data.z,colors.white,"@"}
local alreadyGotOne = false
for a = 1, #otherWaypoints do
if otherWaypoints[a] == newpoint then
alreadyGotOne = true
break
end
end
if not alreadyGotOne then table.insert(otherWaypoints,newpoint) end
end
end
end
"newpoint" is a table pointer, referring to an area of memory where a specific table is stored. "otherWaypoints[a] == newpoint" checks to see if one table pointer is identical to another. They never will be, because they're two different tables (regardless as to whether their
contents are the same or not).
So it's those table contents that you want to check. But really it'd be easier to re-do the method you're using to index into otherWaypoints - how about something like this?:
Spoiler
function _rawListenForMapData()
otherWaypoints = {}
while true do
id, data = rednet.receive(mapProtocol)
if type(data) == "table" and data.x and data.y and data.z and data.name then
otherWaypoints[data.name] = {data.x,data.y,data.z,colors.white,"@"}
end
end
end
A "pairs" loops can be used if you ever need to iterate through that table.
1715 posts
Location
ACDC Town
Posted 28 November 2015 - 02:56 PM
Thanks for the help, guys.
(to KingofGamesYami) I had no idea how to pass arguments when using parallel.waitForAny…but now I do!
(to Bomb Bloke) I replaced the function in the pastebin with yours. Come to think of it, you're makes so much more sense…why didn't I think of it??
I am yet to test it, but this post will be edited once I do.
I have tested it (and modified it some more), and it works as I want. Thanks!
Edited on 28 November 2015 - 02:56 PM