Jump to content




[Tough Tables] Ask A Pro [Series 2]


12 replies to this topic

#1 JustIan12

  • Members
  • 57 posts

Posted 01 September 2017 - 10:59 PM

Hi,

...


Em, so basically I am muddling about with the OpenPeripheral sensor and struggled to find any documentation online that I could understand I eventually discovered the getMethods thingy (do not know what to call this) and I found the one I was looking for.

getPlayers()

so I try to see if I could see myself in the terminal so I called the function

-- SensorCall
local sensor = peripheral.wrap("left")
print(sensor.getPlayers())

and what do I get a nice juicy table not my username :(

table: 57b7d99d

and every time I run the program the table changes, I have looked on the internet on how to convert tables to strings but I am still completely stuck.

I also should also add that this is going to be part of this bigger piece of code I am writing to be able to white list and blacklist players.

Thanks

- Ian

Edited by JustIan122, 01 September 2017 - 11:01 PM.


#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 01 September 2017 - 11:41 PM

I've made a wonderful tutorial which may help you quite a bit. I recommend checking it out!

#3 Thegameboy

  • Members
  • 21 posts

Posted 01 September 2017 - 11:54 PM

edit: you should definitely check out KingofGamesYami's tutorial, but if you want/need to understand the general concepts behind tables: A table is Lua's (the scripting language Computercraft uses) way of organizing multiple variables. In other words, the table you're getting is like getting a box. You just need to open it to get what's inside. A way to sort through each of the table's elements is this:

for key,value in pairs(tableHere) do
	print(key .. "'s value is " .. value)
end

This will iterate through each key, outputting both the key and the value to that key. I'm unaware of how OpenPeripherals stores its information on players but using that code should notify you of everything inside that table. It's entirely possible to have tables inside of tables (eg. table1.table2.table3.key) so remember to sort through those too if you need to. More on this here:
https://www.lua.org/pil/2.5.html (an introduction to tables)
https://www.lua.org/pil/4.3.5.html (the form of the for loop that is used for going through tables used in the above snippet)
(also check out the main book these writeups are under, Programming in Lua, for understanding essential concepts in Lua)

Edited by Thegameboy, 01 September 2017 - 11:58 PM.


#4 JustIan12

  • Members
  • 57 posts

Posted 02 September 2017 - 09:20 AM

View PostKingofGamesYami, on 01 September 2017 - 11:41 PM, said:

I've made a wonderful tutorial which may help you quite a bit. I recommend checking it out!

I have already read it prior to writing this and I just got " 1table " after using your code. I will go though it again to make sure I have not missed anything.

I will do what you have asked now XD

Thanks

- Ian

Edited by JustIan122, 02 September 2017 - 09:20 AM.


#5 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 02 September 2017 - 12:03 PM

That would be expected. Next, you'll want to look inside the table that was returned. To do that, use the code in my tutorial, but remove the {} on line 4.

#6 JustIan12

  • Members
  • 57 posts

Posted 02 September 2017 - 03:02 PM

I get the same thing but I have a feeling that this is going to be a derp moment...

local sensor = peripheral.wrap( 'left' )
function getInfo( ... )
  local tValues = ...
  for k, v in pairs( tValues ) do
	print( k .. type( v ) )
  end
end
getInfo( sensor.getPlayers() )

I have been trying to look inside this table for 4 hours..... XD I am not sure on how to take data from one place and putting it in another.
sorry I am still new to this better than I was 4 years ago.

Thanks!

- Ian

Edited by JustIan122, 02 September 2017 - 03:15 PM.


#7 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 02 September 2017 - 03:53 PM

That just means there's a large number of nested tables. At this point, it might be easiest to try textutils.serialize
print( textutils.serialize( sensor.getPlayers() ) )
Note: serialize will not be very helpful if the table contains functions.

Edited by KingofGamesYami, 02 September 2017 - 03:54 PM.


#8 JustIan12

  • Members
  • 57 posts

Posted 02 September 2017 - 04:05 PM

I will try that now thanks

#9 JustIan12

  • Members
  • 57 posts

Posted 02 September 2017 - 04:16 PM

http://prntscr.com/gg8db3 Hallelujah yay thank you so much XD I will try and integrate this into my code.

Edited by JustIan122, 02 September 2017 - 04:17 PM.


#10 JustIan12

  • Members
  • 57 posts

Posted 02 September 2017 - 06:07 PM

So tables are giving me issues again but differently I am trying to call from a table I have been trying to sort out the base code before adding the main loop and well. here it is...

-- Whitelist
-- whitelist = {"JustIan122", "GreenGoshawk", "GeekyDylan"} I tried calling this I think 'Global' variable at the begining of the code but I got ME 121 attempt to undex ? (a function value)
-- Bootstrapper
term.clear()
term.setTextColor(colors.cyan)
term.setCursorPos(12,4)
print("M.E Defence System")

term.setTextColor(colors.blue)
term.setCursorPos(16 ,7)
print("[Continue]")

os.pullEvent("mouse_click")

term.clear()
term.setCursorPos(15,5)
textutils.slowPrint("Activating")
sleep(1)

local x = 14
while x < 25 do
    paintutils.drawPixel(x,7, colors.green)
    sleep(0.2)
    x = x + 1
end

-- breakInterface Function
function breakInterface()

term.clear()
term.setCursorPos(12,5)
term.setTextColor(colors.white)
  write("Defence Operational")
term.setCursorPos(8,7)
  write("Code > ")

local code = read()

if code == "Safe" then
  term.clear()
  term.setCursorPos(10,7)
   print("Protection Deactivated")
  sleep(1)
  os.shutdown()

elseif code == "Vent" then
  venting()

elseif code == "List" then
  whitelist()

else
  term.clear()
  term.setCursorPos(10,7)
  printError("Command Not Recognised")
  sleep(1)
  breakInterface()
end
end
-- venting Function
function venting()
term.clear()
  term.setCursorPos(12,5)
  term.setTextColor(colors.white)
  write("Are you sure?")
  term.setCursorPos(8,7)
  write("[Y/N] > ")
 
  local code = read()
 
  if code == "Y" then
   term.clear()
   term.setCursorPos(17,5)
   textutils.slowPrint("Venting")
    for i = 0,4 do
	 rs.setOutput("back", true)
	 sleep(0.1)
	 rs.setOutput("back", false)
	 sleep(6)
    end
   term.clear()
   term.setCursorPos(1,1)
   shell.run("delete ME")
   os.shutdown()
 
  elseif code == "N" then
   term.clear()
   term.setCursorPos(12,5)
   textutils.slowPrint("Venting Stopped")
   sleep(1)
   breakInterface()
 
  else
   term.clear()
   term.setCursorPos(10,7)
   printError("Command Not Recognised")
   sleep(1)
   venting()
  
  end
end
-- whitelist Fucntion

function whitelist()
term.clear()
term.setCursorPos(2,2)
  write("Whitelisted Players [Max 3]")
sleep(1)
 
whitelist = {"JustIan122", "GreenGoshawk", "GeekyDylan"} -- When it is here it works once then it breaks and when I call List it says ME:54: attempt to call table. Also, when I write Add or Remove it says command not recognized which is untrue.
for i = 1,3 do
  term.setCursorPos(2,i+3)
  textutils.slowPrint(whitelist[i])
  sleep(0.3)
end
 
term.setCursorPos(2,8)
  write("[Add/Remove] > ")
 
  local code = read()

if code == Add then
  term.clear()
  term.setCursorPos(2,2)
   write("Insert Player IGN > ")
 
  local IGN = read()
 
  table.insert(whitelist,"IGN")

elseif code == Remove then
  term.clear()
  term.setCursorPos(2,2)
   write("Whitelisted Players [Max 3]")
  sleep(1)
 
  for i = 1,3 do
   term.setCursorPos(2,i+3)
   textutils.slowPrint(whitelist[i])
   sleep(0.3)
  end
 
  term.setCursorPos(2,8)
   write("Insert Player IGN > ")
 
  local IGN read()
 
  table.remove(whitelist,"IGN")

  else
   term.clear()
   term.setCursorPos(10,7)
   printError("Command Not Recognised")
   sleep(1)
   breakInterface()
  end
end

-- Main Loop
breakInterface()

The Main issue happens when I enter the command List I want to be able to add and remove users from the table so I can compare them with the sensor data I have been trying to figure out prior to this.

Also after the loading bar my background is green.......

it was unintentional but I cant seem to get rid of it until the code ends.

Sorry for the inconvenience again...

- Ian

#11 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 02 September 2017 - 07:11 PM

You've assigned two different values to the same memory location. Don't use the same name twice.

#12 Stekeblad

  • Members
  • 62 posts
  • LocationSweden

Posted 03 September 2017 - 07:22 AM

if code == Add then
...
if code == Remove then
I think you want this
if code == "Add" then
...
if code == "Remove" then
You want to compare the code the user entered with the words Add and Remove, not what is stored in the variable Add and Remove.

#13 JustIan12

  • Members
  • 57 posts

Posted 03 September 2017 - 06:14 PM

Yeah I saw that XD





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users