Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
Hellreaper

individual terminal glasses (openperiherals compatibility)

Question

Hello again guys, I need some help getting the terminal glasses to do what I want. (if its possible because I don't see a way to do it)

I am trying to get a HUD using the terminal glasses from openperipherals and I'm all set up with it, the only issue is that I do not know how
to send an element of the HUD to one specific person.

f.e.: I would write the information coming from my reactor to one person, and not send that info (or not show it) to the other.

Using the getUsers() method I managed to get the name of the player (with thanks to the forums) but it seems it is read only?

Concrete question: How do I write a piece of information to a terminal glasses from a single bridge, different for 2 or more players.

http://pastebin.com/iCNfuk3Z
 

Link to post
Share on other sites

20 answers to this question

Recommended Posts

  • 0

Hello again :). I found some old openperipheral docs that should help. Beware, they might not be entirely accurate. OP bridge docs.

It seems you want to use 1 bridge and use the method 'getSurfaceByName' or 'getSurfaceByUUID'. I would suggest creating a table of players

where the keys are player names and the value is player specific data... i.e.

local users = {}

for i, v in ipairs( bridge.getUsers() ) do
	local user = {
		name = v.name,
		uuid = v.uuid, -- this might be v.UUID im not sure :/
		surface = bridge.getSurfaceByName( v.uuid ) -- check v.uuid here too :/
	}
	
	users[ i ] = user
	users[ v.name ] = user
end
--[[
	now you have a table you can use like so..
	users[1].name -- > 'HellReaper' or whatever...
	users['HellReaper'].uuid -- > Ue3x67hBV-IW8uyX0x9... some uuid...
	users[someUsernameVarString].surface.someSurfaceMethod( stuff ) -- > draw some private nudes here :p
]]

 

Link to post
Share on other sites
  • 0

Ah i c. What u r doing makes sense. Using the string in the if structure and  than sending the information to the value from the key.  What i was doing checks  using the name and only reads who is using the machine. I think thats how i messed up. 

I want the code to work but before I use it I always like to ask how exactly it works if im not sure ^^ that way i cn keep learning. Thx man, i ll try it when i get home and post the results

Link to post
Share on other sites
  • 0
5 hours ago, Hellreaper said:

Ah i c. What u r doing makes sense. Using the string in the if structure and  than sending the information to the value from the key.  What i was doing checks  using the name and only reads who is using the machine. I think thats how i messed up. 

I want the code to work but before I use it I always like to ask how exactly it works if im not sure ^^ that way i cn keep learning. Thx man, i ll try it when i get home and post the results

I more or less got it working, all I don't uite seem to get is how to call a method to draw on a surface with this, how do I add a method to users.surface? I was going to try and make a method for every if statement like I am doing, so I can organise eaily.
Basically I make a method for everything I want on the hud seperately, and call those in a method for the player that I want it to see it. I am not sure what I need to write in the first method for it to show up in the specific hud.

As a sidenote, do I need to worry about the order of methods? does a method need to be written above its call or does it not matter for lua? I know it doesnt in C# but in C++ it does so I m not sure,

EDIT: Just found out the # doesnt seem to be working...

12ff65ef9c.png

Link to post
Share on other sites
  • 0

Hi again, sorry for the delay, It's 12:35 in the afternoon here. I made a bit of an error. It can work, but it may have given you a slight headache if you weren't sure what you were looking at.

Lua can be a little strange but I love me some strange :P . First off you should know that in Lua tables can be an Array or an Object (as you understand them in other languages). Even stranger still to a C guy is that they can be a hermaphroditic structure too! Like that users table we created. See here.

local users = {}

for i, v in ipairs( bridge.getUsers() ) do
	local user = {
		name = v.name,
		uuid = v.uuid, -- this might be v.UUID im not sure :/
		surface = bridge.getSurfaceByName( v.name ) -- THIS WAS AN ERROR. SHOULD BE "v.name"!!!
	}
	
	users[ i ] = user
	users[ v.name ] = user
end

-- lets say this table just has one result.. You. It would have this structure. below is not code but an example of the users table with one 'user'
users = {
	[1] = {
		name = "TheTrueReaper",
		uuid = "Xjs92BbHj2xAA...some key",
		surface = surfaceInstance -- this should have methods like addBox and the like... I think.
	},
	["TheTrueReaper"] = {
		name = "TheTrueReaper",
		uuid = "Xjs92BbHj2xAA...some key",
		surface = surfaceInstance -- this should have methods like addBox and the like... I think.
	}
}

Note: Using the # operator may show unexpected result here.. # only shows the indexes and not the keys in a table. So the example users table above  would report just a length on 1!  print( #users ) -- > '1'

Note. The table has two entries of the same data. One by #index and one by 'key'. I did this for convenience but it can be tricky. This is what I meant by hermaphroditic.. Is that even a word?? Is now.. Another note about getting or setting keys in a table... Every key in a table is a string! Event if you use a function value as a key, it is coerced to a string and used as a key! This is handy for table access like so...

local player1 = "TheTrueReaper"

local function getUserSurface( player, userList )
	return userList[ player ]["surface"] -- this can also be written with a mix of accessors.. userList[ player ].surface
end

local p1surface = getUserSurface( player1, users )
p1surface.addBox( --[[ variables n shit...]] )

Looking around I also found a gist of peripheral events from the glasses bridge. Its also kinda old so you might have to rely on in game docs. Bridge events gist .

You shouldn't need to add methods to a 'surface' but rather create functions that can operate on one, calling the surfaces' existing methods.

From what I can tell the bridge returns a global 'surface' that will update all connected glasses. Calling bridge.getSurfaceByName( playername ) will return

a private 'surface' with the same drawing methods. Hope that clears that up. Also, I believe to update whats been drawn you must call bridge.sync() to update both 

the global and private surfaces.

If you need anything else... Just ask :) .

Im also happy to shoot some ideas back and forth about how to write/structure the program if you don't mind hacking it together..

Link to post
Share on other sites
  • 0

Damn tables are confusing... To understand more or less what you are doing here I would compare the users-array to a struct holding 3 variables.

Feel free to give some lua coding standards cuz I know myself to be a messy cder when it comes to it ^^ I try to write in functions but I'm used to having everything in a separate file abnd having access to that.
Since I dont really know how to do that in Lua it leaves me with long files with a lot of functions...  I googled a bit and found I could use shell but im not sure if that is an option that is worth using.

Now I am trying to fix the first bit again because I may have made a mistake since the output of users by printing is returning an array but it is completely empty...
Maybe it is a typo but I cannot seem to see what is going wrong right now, still looking though. 

Also dont worry about the bridge.sync, its at the very bottom of my start function which in turn is calling the methods I need to run the program.

the print here is the one going nil twice

87752c8c1c.png

Link to post
Share on other sites
  • 0
21 minutes ago, Molinko said:

Also I wanted to ask..? What does you program do?? lol. Can I see all ye code?

http://pastebin.com/CZDtE8rn here ya go

Im basically trying to make a terminal glasses terminal with a hud for every player. But I have my girlfriend playing on the server and I wanna change some stuff on her hud. (I may or not have drawn a heart around her cursor xD)
Call it cheesy but its fun to do and good for a few laughs. That and poking around to see how crazy I can go with the terminal glasses interface. Been wanting to try it out and now I finally have some time to mess with it and actually get results

EDIT: I fixed the nil thing, 

users[ v.name ] = user

this was causing it to not save the variable, i removed it and it works

Link to post
Share on other sites
  • 0
58 minutes ago, Hellreaper said:

Damn tables are confusing... To understand more or less what you are doing here I would compare the users-array to a struct holding 3 variables.

Actually, the users table is more like a struct than the data inside it. A big difference is where in memory these are in comparison. When I made that I was really just showing off the flexibility of Lua tables. In reflection, that was probably just confusing, but good to know before you fuck things up.. Personally I try to not mix up my tables and use any one instance of a table as either an array* or an object*. 

Coming from a strict language like C to a variably typed language like Lua can be weird. I would really look at these two references: Lua 5.2 manualOC documentation. OC is super close to having a full* pure* implementation of Lua 5.2, with some minor exceptions. 

Something else I wanted to let you know in case you hadn't noticed is that Lua is 'block scoped'. So, something like this(below) can be a headache if you didn't realize this....

local function a()
	local function b()
		return "hi from inner 'b'"
	end
	
	print b()
end

a() -- # > "hi from inner 'b'"
b() -- # > error: attempt to call global function 'b', a nil value. -- or something like that...

Functions having local functions defined within them can be handy for things like privacy because this is the beginnings of a closure, however this doesn't seem to be necessary for your use case.

58 minutes ago, Hellreaper said:

I'm used to having everything in a separate file abnd having access to that.

Yeah, you want the require function. package lib. Use like so.

-- # module code. file: '/usr/lib/List.lua'

-- # fancy prototype inheritance.
-- # see https://www.lua.org/manual/5.2/manual.html#2.4
local List = {}
List.__index = List

-- # private library internal function. This isn't seen outside your library
local function create( t )
	return setmetatable( t, List )
end

-- # if List is called like a function despite being a table, Lua will look to the tables metatable for a __call metamethod.
-- # see https://www.lua.org/manual/5.2/manual.html#2.4
setmetatable( List, { __call = create } )

-- # ':' operator is shorthand for calling methods without needing to pass the caller as the 1st arg.
-- # Same as List.map( listInstance, func )
function List:map( fn )
-- # self is inferred here using the colon ':' syntax
	local t = create( {} )
	local len = #self

	if len > 0 then
		for i, v in ipairs( self ) do
			t[ i ] = fn( v, i )
		end
	end

	return t
end

function List:filter( fn )
	local t, len = create( {} ), #self

	if len > 0 then
		for i, v in ipairs( self ) do
			if fn( v, i ) then table.insert( t, v ) end
		end
	end

	return t
end

-- # return our library from the require statement
return List

-------------------------------------------------------------

-- # Program code. file: '/usr/myprogram.lua'
local List = require "List"

local list = List( { 1, 2, 3, 4, 5 } )

local doubles = list:map( function( n ) return n*2 end )
local evens = doubles:filter( function( n ) return n % 2 == 0 end )

 Im gonna take a slight crack at your program and maybe you'll get some perspective on the strengths of Lua :) 

Link to post
Share on other sites
  • 0
20 hours ago, Molinko said:

Hi again, sorry for the delay, It's 12:35 in the afternoon here. I made a bit of an error. It can work, but it may have given you a slight headache if you weren't sure what you were looking at.

Lua can be a little strange but I love me some strange :P . First off you should know that in Lua tables can be an Array or an Object (as you understand them in other languages). Even stranger still to a C guy is that they can be a hermaphroditic structure too! Like that users table we created. See here.


local users = {}

for i, v in ipairs( bridge.getUsers() ) do
	local user = {
		name = v.name,
		uuid = v.uuid, -- this might be v.UUID im not sure :/
		surface = bridge.getSurfaceByName( v.name ) -- THIS WAS AN ERROR. SHOULD BE "v.name"!!!
	}
	
	users[ i ] = user
	users[ v.name ] = user
end

-- lets say this table just has one result.. You. It would have this structure. below is not code but an example of the users table with one 'user'
users = {
	[1] = {
		name = "TheTrueReaper",
		uuid = "Xjs92BbHj2xAA...some key",
		surface = surfaceInstance -- this should have methods like addBox and the like... I think.
	},
	["TheTrueReaper"] = {
		name = "TheTrueReaper",
		uuid = "Xjs92BbHj2xAA...some key",
		surface = surfaceInstance -- this should have methods like addBox and the like... I think.
	}
}

Note: Using the # operator may show unexpected result here.. # only shows the indexes and not the keys in a table. So the example users table above  would report just a length on 1!  print( #users ) -- > '1'

Note. The table has two entries of the same data. One by #index and one by 'key'. I did this for convenience but it can be tricky. This is what I meant by hermaphroditic.. Is that even a word?? Is now.. Another note about getting or setting keys in a table... Every key in a table is a string! Event if you use a function value as a key, it is coerced to a string and used as a key! This is handy for table access like so...


local player1 = "TheTrueReaper"

local function getUserSurface( player, userList )
	return userList[ player ]["surface"] -- this can also be written with a mix of accessors.. userList[ player ].surface
end

local p1surface = getUserSurface( player1, users )
p1surface.addBox( --[[ variables n shit...]] )

Looking around I also found a gist of peripheral events from the glasses bridge. Its also kinda old so you might have to rely on in game docs. Bridge events gist .

You shouldn't need to add methods to a 'surface' but rather create functions that can operate on one, calling the surfaces' existing methods.

From what I can tell the bridge returns a global 'surface' that will update all connected glasses. Calling bridge.getSurfaceByName( playername ) will return

a private 'surface' with the same drawing methods. Hope that clears that up. Also, I believe to update whats been drawn you must call bridge.sync() to update both 

the global and private surfaces.

If you need anything else... Just ask :) .

Im also happy to shoot some ideas back and forth about how to write/structure the program if you don't mind hacking it together..

So thats why lua knows empty table values with nil values -_-

If you have time, plz go and answer my question

 

Link to post
Share on other sites
  • 0
On 25-1-2017 at 10:25 PM, Molinko said:

FYI the table lib is loaded globally by default. No need to require it. Table.unpack is lua 5.2. Perhaps you're running 5.1 architecture??

 

afaik im running 5.2 but i need to require it. not sure what its about but doesnt rly matter, 

 

Link to post
Share on other sites
  • 0

I seem to be running 5.2 but im not running into issues with it, so I dont think it matters if its there or not...

Though I am running into an issue with the link u gave while I was trying to get it to work and I dont know why (error is on line 60)
Not sure how to fix it, it should be replaced with event.pull() but 

0eae4c573c.png

http://pastebin.com/WAtUwmTL

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.