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

Couple of questions

Started by ReBraLaCC, 11 June 2016 - 03:07 PM
ReBraLaCC #1
Posted 11 June 2016 - 05:07 PM
Okay I don't know if it is possible, but because you're the pro, i am going to ask it

Is there any way to get a get this to work

function getData(table,saveTo)
to = fs.open(tostring(saveTo),"w")
to.write(table[1])
end

With that i mean that i can get a table from the input in the function

And does this also work with functions?

like:

function test(func,inp)
func(inp)
end

I would love to know if any of these work!

Reason im asking this: I will need this from my API PaintU
KingofGamesYami #2
Posted 11 June 2016 - 05:50 PM
It's entirely possible to pass a table - or any other variable - as an argument. However, you should close the file and use local variables.
ReBraLaCC #3
Posted 11 June 2016 - 06:26 PM
However, you should close the file

This is not the code i am using, i just need to know how to be able to do it, because the way i did it, it didnt work :(/>
Wergat #4
Posted 11 June 2016 - 06:38 PM
[…]
This is not the code i am using, i just need to know how to be able to do it, because the way i did it, it didnt work :(/>

You can use the textutils-API to convert data from a table to a string (serialize) and then convert it back when you want to read it with unserialize. It will only convert data like numbers and strings, no functions.

To pass a function to another function, you need to pass the function itself.

foo = function() end -- Foo is a function
foo() -- Calls the function foo
bar(foo()) -- Passes the return value of foo() to the function bar as argument
bar(foo) -- Passes the function foo as an argument, it can be executed by function bar
Dragon53535 #5
Posted 11 June 2016 - 06:58 PM
To put it in laymen's terms, yes it's possible.

If you have a table, just pass the table through, it will keep all data.

If you have a function just pass the function pointer, basically do what the last line of Wergat's code did.

Anything that is a variable can be passed. Just do try to avoid recursion.

To use your code snippets as examples:


local tbl = {1,2,3,4,5,6}
getData(tbl,"Myfile")


test(print,"Hi")
Edited on 11 June 2016 - 05:00 PM
ReBraLaCC #6
Posted 11 June 2016 - 09:58 PM

foo = function() end -- Foo is a function
foo() -- Calls the function foo
bar(foo()) -- Passes the return value of foo() to the function bar as argument
bar(foo) -- Passes the function foo as an argument, it can be executed by function bar

But then you do like

function test()
 return
end

local function bar(function)
 parallel.waitForAll(function,test)
end

If im still wrong im dumb probs, or im not reading correctly ;)/>
Bomb Bloke #7
Posted 12 June 2016 - 02:17 AM
But then you do like

… and why would you bring the parallel API into it? What are you expecting it to achieve?
Lyqyd #8
Posted 12 June 2016 - 03:23 AM
You should post the actual code you're having trouble with.