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

[Peripherals++ 1.7.10] Player sensor table

Started by MasterReaper, 22 July 2015 - 06:30 PM
MasterReaper #1
Posted 22 July 2015 - 08:30 PM
Hello,
I'm making a program using the player sensor from Peripherals ++,here is the piece of code that I use:
http://pastebin.com/G3nCXJvS

It works fine, giving a player associated with a distance, but in a single string, I tried to use a string.sub and some split functions but still I can't find any way to get distance and username in different variables.

Any ideas ?
KingofGamesYami #2
Posted 22 July 2015 - 11:17 PM
Can you post an example of what it gives you? I'd say use patterns, if it follows a certain formula.
Balthamel #3
Posted 23 July 2015 - 02:00 AM
string.gsub(stringToSplit,"[^%-]+",print) will print the split strings delimited by a - symbol, look up pattern matching and pick something the strings will never contain.
to overcome the limits of passing a single function I used

local function putStringInTable(stringFragment)
table.insert(table,stringFragment)
end
string.gsub(stringToSplit,"[^%-]+",putStringInTable)
This will append each string piece onto the table.
If you wrap in a for loop and assign for each key in the table the vale as a table then you can have the output structured like a 2d array where table[1][1] is the username 1 table[1][2] is distance of username 1 table[2][1] is username 2 and so on.
MasterReaper #4
Posted 23 July 2015 - 04:12 AM
This is my exact output :
When using print()
Username
Distance

When using write()
UsernameDistance

(with no space)

I already tried using patterns but it seems like it understand 'infos' as two string for example a print(d:len()) displays

12
9

(12 is the username, 9 the long distance )
Edited on 23 July 2015 - 02:20 AM
Balthamel #5
Posted 23 July 2015 - 04:16 AM
does it return in a table?
how does this peripheral work?
i would be expecting to see line 3 as

list = {p.getNearbyPlayers()}
since it seems like it is returning as a table.
try print(unpack(list))?

This is my exact output :
When using print()
Username
Distance
What exactly is inside the print()? is it print(p.getNearbyPlayers()) or print(list)?
Balthamel #6
Posted 23 July 2015 - 04:27 AM
snip
Edited on 23 July 2015 - 02:28 AM
MasterReaper #7
Posted 23 July 2015 - 04:37 AM
According to the wiki : https://github.com/austinv11/PeripheralsPlusPlus/wiki/Player-Sensor
The function getNearbyPlayers() "Returns nearby players in the form of a table, this table contains other tables with the keys "player" and "distance"


list = {p.getNearbyPlayers()}

returns the table adress (something like : table:42762e7b)


print(unpack(list))

returns the table adress,just like before

And print() is displaying the variable 'infos', obtained with the 2 for loops
KingofGamesYami #8
Posted 23 July 2015 - 04:40 AM
Well, the thing is, p.getNearbyPlayers returns a table. You put that table in another table. Printing that table will give you a table address. Unpacking the contents of that table (read: this gives you the table created by p.getNearbyPlayers) will obviously give you… another table address.

Perhaps
print(textutils.serialize(p.getNearbyPlayers()))
would work better.

Edit: Internet is derping on me, ignore any repeat posts.
Edited on 23 July 2015 - 02:41 AM
MasterReaper #9
Posted 23 July 2015 - 04:48 AM
So, here is what I got adding only this line :

print(infos:len())

Spoiler

And with

print(textutils.serialize(p.getNearbyPlayers(100)))

Spoiler
Balthamel #10
Posted 23 July 2015 - 04:48 AM
Yeah that makes sense, okay so the issue seems to be that you are using

list = p.getNearbyPlayers(100)

in this example
table = {a,b},{c,d},{e,f}
table == {a,b}

when you should use

list ={ p.getNearbyPlayers(100)}

in this example

table = {{a,b},{c,d},{e,f}}
table == {{a,b},{c,d},{e,f}}

unless your link is outdated
the way your pastebin is listed it will only store the first username/distance pair as a table instead of all k v pairs in nested tables.
Edited on 23 July 2015 - 03:06 AM
MasterReaper #11
Posted 23 July 2015 - 05:00 AM
And what is "lines" supposed to be ? Another table ?

Here is what i got :


p = peripheral.wrap("back")
lines = {a,b}
list = {p.getNearbyPlayers(100)}
  for k,v in pairs(list) do
    for l,w in pairs(v) do
	  infos = tostring(w)
   print(infos)
  end
end

I'm not understanding this time :/
Balthamel #12
Posted 23 July 2015 - 05:01 AM
sorry lines was just in my head lines is just any random variable name you can delete that line it was just supposed to show you what the lack of curly brackets was doing.
I have edited the post, i hope it makes more sense

what is it doing now?
Edited on 23 July 2015 - 03:04 AM
MasterReaper #13
Posted 23 July 2015 - 05:03 AM
So, it returns multiple tables with usernames and distances, right ?
I didn't find any explanations on the internet, only for miscperipherals, wich is outdated
Balthamel #14
Posted 23 July 2015 - 05:07 AM
That's what it sounds like. Tables in tables. Does it work now?
MasterReaper #15
Posted 23 July 2015 - 05:09 AM
Nope, still returning a table adress
Balthamel #16
Posted 23 July 2015 - 05:16 AM
OH it is using


p = peripheral.wrap("back")
lines = {a,b}
list = {p.getNearbyPlayers(100)}
  for k,v in pairs(list) do    
   print(v.player) 
   print(v.distance)
  end
end

maybe?
MasterReaper #17
Posted 23 July 2015 - 05:25 AM
It returns 2 blank spaces,
I used tostring() and, well, it displays nil and nil

p = peripheral.wrap("back")
lines = {a,b}
list = {p.getNearbyPlayers(100)}
  for k,v in pairs(list) do   
   print(tostring(v.player))
   print(tostring(v.distance))
  end
end
I don't see where the problem is, eveything looks good :/

Sorry for double post, but I finally got it =), at least it works for one player.

p = peripheral.wrap("back")
lines = {a,b}
list = {}
list = p.getNearbyPlayers(100)
  for k,v in pairs(list) do  
   print(tostring(v.player))
   print(tostring(v.distance))
  end
end

It works, I can choose to display only usernames, or only distances or do whatever I want with these variables =)

I'm going to test it on a multiplayer server and see if it works

(Sorry if it sounds a little bit repetitive, but English is not my native language ^^ )
Balthamel #18
Posted 23 July 2015 - 05:26 AM
print will always tostring what is inside it.
instead of v.player try list[k].player
and you can delete line 2 that is not needed
also you do not need to define list as a table (line 3) as you then define it as table on line 4
Edited on 23 July 2015 - 03:29 AM
cheabred #19
Posted 09 October 2015 - 01:08 PM
this should so what you are looking for..

http://pastebin.com/bpFLmycK