Jump to content




How can I use I code serialized?



10 replies to this topic

#1 Joroba3

  • Members
  • 4 posts

Posted 09 December 2015 - 04:11 PM

I'm creating a rednet system and I want thye computer I send the message to to do something like this
function hello ()
print("hello")
end
seri = textutils.serialize(hello())
rednet.send(id,seri)

--And then in the other computer
receive = rednet.receive()
unse = textutils.unserialize(receive)
-- And then here I want to execute the function I just received

For exemple.
Please if you know how to do this please comment
thanks

#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,458 posts

Posted 09 December 2015 - 07:36 PM

Moved to Ask a Pro.

What are you actually trying to accomplish?

#3 Blue

  • Members
  • 309 posts
  • LocationGlass/UX/main.lua

Posted 09 December 2015 - 08:06 PM

 Lyqyd, on 09 December 2015 - 07:36 PM, said:

What are you actually trying to accomplish?
He's trying to send a function over rednet using textutils.serialize() to convert the function into a string.

textutils.serialize() only serializes tables,not functions. I don't think Lua can convert functions into strings,but there might be some 'hacky' way of doing it.

Also,
seri = textutils.serialize(hello())
'()' calls a function. So even if serialize did convert functions,it still wouldn't work.

#4 Lupus590

  • Members
  • 2,001 posts
  • LocationUK

Posted 09 December 2015 - 08:11 PM

newer versions of lua can serialize functions (and coroutines) unfortunately CC has not updated lua yet (although apparently an update is in the works)

#5 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 09 December 2015 - 08:18 PM

What about about sending a base64 of the functions string.dump() and then loadstring() it on the other end? That will break up-values, though.

#6 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,458 posts

Posted 09 December 2015 - 09:20 PM

 Glass Systems, on 09 December 2015 - 08:06 PM, said:

He's trying to send a function over rednet using textutils.serialize() to convert the function into a string.

Yeah, you missed the entire point of the question. I don't need to ask about what's plainly obvious. I was looking for information about the goals he has that led him down the path that caused him to arrive at the conclusion that he needed to serialize functions and send them over rednet. I asked the question because topics like this are usually examples of X/Y problems where finding out more about the problem at hand will allow us to suggest an alternate method that will be both easier and more robust.

#7 areuz

  • Members
  • 11 posts

Posted 09 December 2015 - 10:05 PM

The only way I can imagine sending a function via rednet and executing it on another computer is, that you would read a file containing this function using fs.readLine , and save it to a table. Serialize the table after that, send it, receive it, unserialize it, and write the table to another file on the computer. After that, just add it to the current file using os.loadAPI. With that, you can use that function in your main code file.

You can find all of the information about the functions I mentioned on the official ComputerCraft wiki:
http://www.computerc.../wiki/Main_Page

I could write the code for you, but I don't think that would help you become a better independent programmer in any way.

Edited by FireDust, 09 December 2015 - 10:10 PM.


#8 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 09 December 2015 - 11:08 PM

 FireDust, on 09 December 2015 - 10:05 PM, said:

The only way I can imagine sending a function via rednet and executing it on another computer is, that you would read a file containing this function using fs.readLine , and save it to a table. Serialize the table after that, send it, receive it, unserialize it, and write the table to another file on the computer. After that, just add it to the current file using os.loadAPI. With that, you can use that function in your main code file.

You can find all of the information about the functions I mentioned on the official ComputerCraft wiki:
http://www.computerc.../wiki/Main_Page

I could write the code for you, but I don't think that would help you become a better independent programmer in any way.

What if you can't "read" the function because it is given to your program/API/whatever at runtime, without afile reference? (e.g. os.pullEvent. This function is written in lua, but it's inside bios.lua -> you can't access it.)

I would do something like this (if you really have to send functions through modems.) NOTE: this is semi-pseudo code. You won't have all the functions mentioned available.
--The sending computer
local dumped = string.dump(os.pullEvent)
local b64 = base64(dumped)

modem.transmit(b64)

--the receiving computer

local b64 = modem.receive()
local dump = unBase64(b64)
local func = loadstring(dump)
func()

Not *entirely* sure if that will work, because, as I said, atleast up-values will be broken. Mayben even more.

Edited by H4X0RZ, 10 December 2015 - 06:33 AM.


#9 areuz

  • Members
  • 11 posts

Posted 09 December 2015 - 11:52 PM

I totally forgot that LUA had something like a dump function built in. I was kinda bored, so I already wrote the code that I described earlier, purely on the idea that the function he wants to send is either saved in a function database file, or in the main code itself. This is more effective, and should be much shorter.

Edited by FireDust, 10 December 2015 - 12:00 AM.


#10 Joroba3

  • Members
  • 4 posts

Posted 11 December 2015 - 03:21 PM

Thankyou all. Now I understand it.

#11 0099

  • Members
  • 52 posts

Posted 11 December 2015 - 09:48 PM

In case you didn't know, there is an interesting seralizer at lua-users.org, which can serialize almost anything. And by "anything" I mean that it includes very complex objects, like a table with a metatable which has a __index function which refers to a table which in turn has a metatable. Or a table which contains itself, is a key for itself in itself, is the meta of itself and the __index of itself (How's that supposed to work, actually? Crash from infinite recursion?). Unfortunately, CC doesn't have debug library active, so certain features won't work, like upvalues and cleverly hidden completely sandboxed metatables. But still, this thing is very useful.
1. Download the code from http://lua-users.org/wiki/DataDumper and upload it somehow onto your CC computer.
2. In the beginning of your sender program, call dofile() with the name of the uploaded DataDumper.
3. When you want do dump something into a string, call DataDumper() with your object as a parameter.
4. DataDumper will return a bunch of text written in Lua, which, when executed, will create an exact copy of the original object. To execute this string from your receiver program, call loadstring() with this string, and it will return a function which you then should call. This function will return a copy of your object.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users