Serialize script or Function

Hi everybody !

I would like to know if there is a way to serialize a script, or a function ? And re-use it ?

Have a good day !

What do you mean by reuse it?

Add: [System.Serializable] before the function or class.

@Brathnann
I want to serialize a script, or function, to save it in a Json file, xml, or other file, and reimport it, to reuse it after.
(I’m trying to import a function from server that player can find with decompilation )

@Vryken , Sorry, i think that my explain was wrong :confused:

So you’re trying to save out a script and then load that script later?

Unfortunately, this isn’t an easy task. I know there are ways to create scripts at runtime, but the process is a pain. (did it while studying for my c# certification, not fun)

I don’t think it’s as simple as here is a .cs file, download it and it will work. Though I have honestly never tried something like this.

Now, I know there is mention of using something like lua to allow let’s say an in game editor for players to create their own content, but I’m not familiar with that.

Unfortunately, I may not be of much help here, other than you’re probably going to find it difficult (if even possible) to do what I think you are asking to do.

I remember another similar thread. Out of curiosity, what kind of code are you talking about wanting this way?

Afaik, only some platforms (may) allow you to actually create C# code on the fly…I’ve lost track of which would/wouldn’t. I’ve never done it, but glanced at a few things online talking about it. Sounds cool, but not ultimately useful to me. lol

Yeah. I think iOS is one that doesn’t, but that is due to Apple’s policies, not sure if the platform itself would let you.

I believe AOT compilation is incapable of using Reflection.Emit.

Note that downloading the code on the fly wouldn’t increase security that much. If the user has access to the computer and could decompile your program, they could also monitor the network traffic and see what it downloaded. Even if you encrypt the download, the code ultimately has to be present in memory to run.

If you want something really secure, the only way is to have it happen on a server you control, AFAIK.

If you could serialize a script to a json file to import it, it would be even less secure than already in your build. Doing what you need on the server instead of the client would be the most secure.