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

Openccsensor question

Started by Beamer86, 08 July 2013 - 05:58 PM
Beamer86 #1
Posted 08 July 2013 - 07:58 PM
Title: Openccsensor question

Hiya folks, newbie programmer here…just had a question about openccsensors…

The code that I have for activating a redstone output (for a door, or a tesla coil, the two of whiuch are going to be what the code is doing, and that i also shamelessly ripped right from the openccsensor wiki entry) is more or less straightforward, but there are 2 things that i would like to ask…

1. how can i make the sensor activate ONLY for players, and to go a bit further, only for SPECIFIC players? (im looking for an answer to both of these, please and thank you)

and

2. This section

local proximity = sensor.wrap("left")
while true do
local signal = false
local targets = proximity.getTargets()
for k, v in pairs(targets) do
if distance(v.Position) < radius then
signal = true
end
end


has me slightly confused…alow me to please run down what i know here
the first line is designating the sensor name (proximity) and telling the computer where it is, thats easy…
the second line, my guess, is that its telling the computer that if the sensor is there it is to do the following…yes?
third line is setting a variable, signal, but i dont know what false is…does it just mean that its setting no players in range as a variable? dont quite understand
fourth line, straightforward, setting the target list as a variable (and im atleast half certain that this is what i would have to play around with to set only players to activate the sensor
5th line…this one is the one that got me….k? v in pairs? might as well be a foreign language for as much as i can figure this bit out.
6th line, checking if the distance between the target and the door is less than the radius i set earlier in the code, easy
7th line, setting the signal to true, letting computer know that yes, its time to activate the redstone?

any and all help with this would be appreciated, LUA seems easy enough, but the little obscure bits her are the ones that im going to have a problem with

Thanks!

P.s. if any of you are going to post code for me to see, please comment as much of it as you can, it really helps….thanks again!
Lyqyd #2
Posted 08 July 2013 - 08:14 PM
Split into new topic.

Look through the output for a player entry in ocs/programs/sensorview. There are a couple of keys that are unique to players, and one is the user name.
Beamer86 #3
Posted 08 July 2013 - 09:35 PM
Thanks for the quick response…theres 2 name options, one that's username (and is the name that appears above our heads as we play) and another that is just name, and for both my friend and i shows up jsut as "player" in sensorview…since you said unique, that automatically rules out the "name" part, but I am still confused as to how to get the sensor to check the username and use that to determine whether or not it should open the door..i tried putting mine and my friends usernames in the bracket after the proximity.getTargets function, but that didn't do anyting (not that i REALLY expected it to.

If you dont mind a little bit of pseudocode here, I'm thinking to myself that it would be something along the lines of


local = proximity.getTargets ()
	 if target = (usernames) then [Continue to rest of code]
	 else End

but like I said, I'm not really sure how i would go about getting the sensor to check the username, and check it against the "whitelist", or even just to check if its a player vs an AI

sorry if I go on a bit but i'm kind of trying to figure as much of it out myself as I can, and i figure that if i go and show what it is that im thinking here it might make it easier for hte people that know more than me to help point me in the right direction, thanks for your patience everyone
Lyqyd #4
Posted 08 July 2013 - 09:46 PM

local canPass = false
for target in pairs(proximity.getTargets()) do
  local details = proximity.getTargetDetails(target)
  if details.UserName and (details.UserName == "onePlayer" or details.UserName == "anotherPlayer" or details.UserName == "yetanother") and distanceIsWhatItShouldBe() then
    canPass = true
    break
  end
end
if canPass then
  openTheDoor()
end

You could also put all the usernames in a table, like so:


local names = {
  ["userOne"] = true,
  ["anotherUser"] = true,
}

And then instead of that block of or conditions in parentheses above, you'd just need `names[details.UserName]`.
Beamer86 #5
Posted 08 July 2013 - 10:06 PM
You are infinitely smarter than myself here, and it shows in my almost complete inability to understand that lol, I'm gonna try to break this down a bit…(sorry, i gotta take this slow in my head to make sure i understand it as much as possible

the first line designates canpass as a variable, and judging formt he last 3 lines, only when canpass is true will the rest of the code execute (the openthedoor() part), otherwise it remains false…is that the point of the canpass = false part?

the second line there brings me back to the original post and my confusion about the "in pairs" part…is this some basic LUA code that I completely missed by first learning this code in computercraft? Is there any simple way to explain that part…a quick google says that its something used for lua to be able to read form a table, or thats atleast what i picked out of it, but i still find myself slightly confused as to just how it works…I'll have to read over that page again after im done here…

the line after that is another one thats fairly straightforward, setting details as a variable containing all the target details from sensorview, but im confused as to what the target arguement is going to do…

the line below that looks as if its the one that checks the information for usernames (if detailrs.username),checks it against the whitelist (the oneplayer anotherplayer, and yetanother part), checks if the distance is ok (distanceiswhatitshouldbe pseudocode) and then (the line below that) allows the code to continue if the username is in the whitelist

As for the table, it can be as long as i want, as long as the whitelist names all appear between the { } yes?
Lyqyd #6
Posted 08 July 2013 - 10:34 PM
pairs() is pretty simple. The usual form is:


for key, value in pairs(tableName) do
  --something
end

Each iteration of the loop gets the next key/value pair from tableName and puts it in the key and value variables. In the case of the loop in my previous post, we only needed the key, so we could look up the detailed information for each target, since OCS's target tables use the target as the key for each entry.
Beamer86 #7
Posted 08 July 2013 - 11:14 PM
Ok, so in my first post, that part i was talking about (for k, v in pairs) is basically just what you have above, with key and value shortened to k and v?
I think I am probably just a little overtired,think i will sleep on it and look at this all again with a fresh mind tomorrow..thank you very much for your help so far Lyqyd, hopefully with a new day will come a little more comprehension on my part lol