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

Molinko

Members
  • Content Count

    451
  • Joined

  • Last visited

  • Days Won

    35

Posts posted by Molinko

  1. FYI, dunno if you already know this but, the print function is just a wrapper for io.stdout:write with a concatenated newline char. i.e. 

    function print( ... )
      local out = ""
      for i = 1, #arg do
        out = out .. type(arg[i]) == 'string' and arg[i] or tostring(arg[i])
      end
      io.stdout:write( out .. '\n' )
    end

    ... Seems the latest definition of print in OpenOS uses  a buffer like what @TYKUHN2 said. Here is the real def in the latest version of OpenOS.

    -- Other code above in file.
    -- found @ "/boot/00_base.lua"
    function print(...)
      local args = table.pack(...)
      local stdout = io.stdout
      stdout:setvbuf("line")
      for i = 1, args.n do
        local arg = tostring(args[i])
        if i > 1 then
          arg = "\t" .. arg
        end
        stdout:write(arg)
      end
      stdout:write("\n")
      stdout:setvbuf("no")
      stdout:flush()
    end

     

  2. --try loading the robot lib through the require statement. In the lua interpreter the robot lib is loaded by default.
    -- I dont think the component.robot library uses the same robot.fill function....
    -- So, either replace the top lines of your program with this...
    local robot = require 'robot'
    robot.fill(1000) -- this assumes the tank is attatched to sides.front
    
    -- Or try this which i would recommend...
    local component = require 'component'
    local sides = require 'sides'
    local robot = component.proxy( component.get('967f') )
    robot.fill( sides.front, 1000 ) -- or whatever side you want...

     

  3. 10 hours ago, mopet01 said:

    local _, _, x, y = event.pull(1,touch)

    This is the first line of your getClick() function.

    Try changing it to this... notice the quotes.

    local _, _, x, y = event.pull( 1, 'touch' )

     

  4. oh but it did work. rs.getItems() returns a list(table). I assume you have 505 of something in your storage... The list returned has data(as a table) in each index. Look at it like this...

    local function dumpSlot(n, items)
    	local info = items[ n ]
    	for k, v in pairs( info ) do
    		print( k, v )
    	end
    end
    
    -- look at details of slot 1 in list of items returned by rs.getItems()
    dumpSlot(1, rs.getItems())

     

  5. The table is being turned into a string. Lua doesn't know how to do this in a meaningful way by default. What you want are the table keys and its values. Try this out and see if its what you're looking for.

    for k, v in pairs(test) do

      print( k, tostring(v) )

    end

    Sorry for the crap formatting. Phone...

  6. I've made some small changes.. I think this is what you're looking for :)

    arg1=...
    cm=require("component")
    si=require("sides")
    s=si.east
    c=require("colors")
    rs=cm.redstone
    
    doors = {
     -- # key, value list of door id's and their respective colors
    	["s1"] = 'red',
    	["s2"] = 'green',
    	["s3"] = 'brown',
    	["s4"] = 'blue',
    	["s5"] = 'purple',
    	["s6"] = 'cyan',
    	["p1"] = 'pink',
    	["p2"] = 'lime',
    	["office"] = 'yellow',
    	["utility"] = 'orange'
    }
    
    function openDoor(id, doorlist)
      local color = doorlist[ id ]
      if color then
        rs.setBundledOutput(s, c[ color ], 255)
      else
    	print(("[%s] is not a valid door id."):format(id))
      end
    end
    
    openDoor(arg1, doors)

    Hope this helps.

  7. I'm not sure about this but I believe there is a setting in the oc config to change the default fg and BG color of a tier 1 monitor... Dig through the cfg

    The setting is in the 'client' section of 'settings.conf' in your minecraft folder. The setting is called 'monochromeColor'.

  8. 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 :) 

  9. I don't quite understand what you mean by 'run name'. But if you want to print text to the screen its pretty easy.

    In the shell type( no quotes ): 'edit test.lua'

    This will open a new file name 'test.lua' to write your basic program. Inside...

    -- # this is a comment, not code.
    print( "Hello world!" )
    
    -- # press ctrl+s to save. then press ctrl+w to exit the edit program.

    You should be back in the OpenOS shell now. Type( no quotes ): 'test.lua' to run your program.

  10. 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..

  11. 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
    ]]

     

  12. If all you want is a list of names you can do this..

    local names = {}
    local users = bridge.getUsers()
    
    for i = 1, #users do
    	names[ i ] = users[ i ].name
    end

    I prefer something kinda functional though...

    -- create a new table with the result of fn(el) @ each el
    local function map( t, fn )
    	local _t = {}
    	for i = 1, #t do _t[ i ] = fn( t[ i ] ) end
    	return _t
    end
    
    -- create a function that takes a table and returns the val of given key in said table
    local function pluck( key )
    	return function( t )
    		return t[ key ]
    	end
    end
    
    -- create a list of names by mapping over users list and plucking the name property of each user object.
    local names = map( bridge.getUsers(), pluck( 'name' ) )

    Im showing the above example just for shits really... Functional programming can be fun :)

×
×
  • Create New...

Important Information

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