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

Help with tables and OpenPeripheral's "Sensor"? First project.

Started by BrandenKP, 01 June 2014 - 05:28 AM
BrandenKP #1
Posted 01 June 2014 - 07:28 AM
Hi, I'm currently writing a program for people on a server to show stats about players of interest. I was really interested in Watch_Dogs, and it seemed like a fun project to create a "profiler" that allowed you to look up a player on the fly, know some information about them, and perhaps challenge them to a duel. As you can see by the source code, however, I haven't been having much luck getting anything that would be helpful to show in a concise (and correct!) way.

Anyhow, I don't have a lot of coding experience (I've just picked up LUA a few days ago, if I can be honest) but I came into a few snares:
  • I can loop through s.getPlayerData to get EVERYTHING, and I can loop through tables WITHIN tables (s.getPlayerData.position, for instance) but I can't seem to get values within tables to display (health, for instance, will give nil if I try to do s.getPlayerData.health, although I KNOW it simply doesn't work like that..)
  • One of the drawbacks to the sensor is that it gives data relative to itself, not relative to 0,0. Typically, that's fine, but it means that the coordinates will have to be calibrated to the sensor's position. Is there any way to "grab" the information from s.getPlayerData.position, and then apply a little math to it so it spits out the right coordinates? (For example, if the sensor is at x1, y1, z1, and the target is at x10, y10, z10, the program prints x10, y10, z10 instead of x11, y11, z11.)

Edit: I should mention I am using the CrackPack's most recent version, if that helps. (/r/crackpack)
Edited by
Lyqyd #2
Posted 01 June 2014 - 08:47 AM
Please don't change the size of the text. I had to edit your post to make it readable.
Whitecatblack #3
Posted 01 June 2014 - 09:43 AM
So, as a foreword, I don't use sensors or terminal glasses much, but lets see what I can do.

BrandenKP said:
I can loop through s.getPlayerData to get EVERYTHING, and I can loop through tables WITHIN tables (s.getPlayerData.position, for instance) but I can't seem to get values within tables to display (health, for instance, will give nil if I try to do s.getPlayerData.health, although I KNOW it simply doesn't work like that..)
Well the correct way to call a value in a table is like this:

s.getPlayerData("username")["health"]
Which will return the target's current health, out of 20 (i.e. 16/20 being 8 hearts full). So you can apply that to anything you need to pull from that table.

BrandenKP said:
One of the drawbacks to the sensor is that it gives data relative to itself, not relative to 0,0. Typically, that's fine, but it means that the coordinates will have to be calibrated to the sensor's position. Is there any way to "grab" the information from s.getPlayerData.position, and then apply a little math to it so it spits out the right coordinates? (For example, if the sensor is at x1, y1, z1, and the target is at x10, y10, z10, the program prints x10, y10, z10 instead of x11, y11, z11.)
So for this, the sensor, as far as I can tell, reads out position in reference to itself. This could be useful, but from what I can guess, you sound like you want to know the true co-ordinates of the player. If the sensor itself isn't going to move or anything, then this can be done much simpler by manually entering the sensor's co-ordinates and adding the relative position of the player to it:

x = math.floor("sensorPositionX"+s.getPlayerData("playername")["position"]["x"])
x is now equal to target player's actual position in the world, rounded down to the nearest whole number. This tactic can be applied to the y and z values as well.

Hope this is what you were looking for,
Whitecatblack
Edited on 01 June 2014 - 07:56 AM
BrandenKP #4
Posted 01 June 2014 - 05:17 PM
Please don't change the size of the text. I had to edit your post to make it readable.

Sorry, I copied and pasted it from a different post I made on reddit. :(/>

So, as a foreword, I don't use sensors or terminal glasses much, but lets see what I can do.

BrandenKP said:
words
Well the correct way to call a value in a table is like this:

s.getPlayerData("username")["health"]
Which will return the target's current health, out of 20 (i.e. 16/20 being 8 hearts full). So you can apply that to anything you need to pull from that table.

BrandenKP said:
words
So for this, the sensor, as far as I can tell, reads out position in reference to itself. This could be useful, but from what I can guess, you sound like you want to know the true co-ordinates of the player. If the sensor itself isn't going to move or anything, then this can be done much simpler by manually entering the sensor's co-ordinates and adding the relative position of the player to it:

x = math.floor("sensorPositionX"+s.getPlayerData("playername")["position"]["x"])
x is now equal to target player's actual position in the world, rounded down to the nearest whole number. This tactic can be applied to the y and z values as well.

Hope this is what you were looking for,
Whitecatblack

Thank you for the quick response! I'll try what you suggested immediately.
Whitecatblack #5
Posted 01 June 2014 - 10:39 PM
BrandenKP said:
Thank you for the quick response! I'll try what you suggested immediately.
No problem. I've been fooling around a bit with your whole idea, so if something doesn't work or you are looking for a way to do something, just post a response and I'll probably be able to help.

Whitecatblack