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

saving component to variable?

Question

As the title states, I am trying to put a component in a variable for faster runtime as I assume that a single call is cheaper than calling it every time.

Unless this is irrelevant in opencomputers (of which i have no idea)

Most of this code has been copied over from computercraft but it should work. I just dont know how to save a component to variabele since what I am doing doesnt seem to work.

 

EDIT: also calling a function this way (bridge.sync) when i replaced bridge with component.openperipheral_bridge that did not work either -> attempt to index global 'component' (a nil value)

i replaced sleep with os.sleep since that seems to be the correct way to type it. I tried to replace bridge.sync with component.invoke(component.openperipheral_bridge.address, sync) but that also did not work...

I tried using the exact same code I typed in the first function in the lua interpreter and that worked, sadly in the script i wrote it doesnt...

The left was before the edit, the right is current version. (and the error message is upon running the most recent version)
I'm sorry for the messy question, I hope you can help...

 

7c82c7b9f1.pngb6b9d43d60.png549776d317.png

Share this post


Link to post
Share on other sites

10 answers to this question

Recommended Posts

  • 0

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

Share this post


Link to post
Share on other sites
  • 0

In computercraft, APIs such as "component" are not loaded in the environment by default. There are only the few global functions without namespaces (component is a namespace) that are. Also the global environment is not shared by programs.

Share this post


Link to post
Share on other sites
  • 0

The component lib must be loaded with the require statement. In CC you would call global fn peripheral.wrap. In OC...

local component = require 'component'
local modem = component.getPrimary 'modem'
--...

 

Share this post


Link to post
Share on other sites
  • 0

I am not a expert in lua, but I think tables dont have the function (?) getn, maybe something else from computercraft.

The wiki states:

Quote

http://ocdoc.cil.li/api:non-standard-lua-libs#table_manipulation

Table Manipulation

The original functions from the table library are available without alteration.

The Link goes to a lua manuel.

it list the functions .concat, .insert, .pack, .remove, .sort and .unpack.

I think what you are search for is .unpack

Quote

http://www.lua.org/manual/5.2/manual.html#6.5

table.unpack (list [, i [, j]])

Returns the elements from the given table. This function is equivalent to


     return list[i], list[i+1], ···, list[j]

By default, i is 1 and j is #list.

I hope this helped.

Share this post


Link to post
Share on other sites
  • 0

That is actually accurate, i messed up. I now have the 2 names and uuids. Now I need a way to save only the names themselves preferably in an arrray.

I tried it out in the lua interpreter and it gave me this, which is a 2D array with 2 elements I guess? Can I now somehow extract the ones that say name=string from this and store them?

11b89adf57.png 

Share this post


Link to post
Share on other sites
  • 0

damn tahts one masterful piece.

I used the second one, but i assume the #users is the amount of things in the table?

I am unfamiliar with lua but that would make sense (I am used to some C++ but i m still in school for it)

I also marked yours as best answer because if people run into the same problem your answer is most compolete and explanatory

thank you very much. Now I can send secret messages and give my friends on my server their own glasses UI of their own choosing ^^

Share this post


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

i assume the #users is the amount of things in the table?

Yep. There are some"gotchas" but for the most part its great. Thanks for the uptick :)

Share this post


Link to post
Share on other sites
  • 0

Got another issue but a bit more complicated and I can't think how it would be possible, I have the names now and I can display them, but the current problem is that its a read only thing... And I cannot personalise the HUD per person,

All I can do is get the names of the people connected to it. I cant get access and show a specific thing individually. Is this a limitation or am I missing something?

(Should this be a new topic or can I put this here? Otherwise I ll move it)

Share this post


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.