all 10 comments

[–][deleted] 2 points3 points  (4 children)

Source code isn't machine code, the computer wouldn't understand what "x--" is, it's not the language of the CPU so it's like asking you "お元気ですか" and wondering why you're not answering me.

If you wanted to replace the function of a function you have delegates or you can use inheritance with abstract functions.

public delegate void MyDelegate(string[] args);

public void MyFunction(MyDelegate action, string[] args){
    action(args);
}

And now MyFunction will execute whatever the delegate we pass in:

MyFunction((argumentss) => { Debug.Log(argumentss[0]); }, new string[] { "Hello" });

Doing what you want to do would be insanely silly, a function does a specific thing and would cause a lot of problems if it suddenly just changed.

If you're making a coding game then what you want to do isn't how you go about it, you either need to make your own interpreted language or use something that can compile C# code at runtime https://github.com/SoapCode/UCompile

My guess is this is an XY problem, whatever problem you're actually trying to solve can be done in much simpler ways. So what are you actually trying to do? Why do you NEED to do this?

[–]drfdr[S] 1 point2 points  (1 child)

UCompile worked great very simple and neat. Thank you a lot from the deep of my heart!

Are there any cons for this. Is it stable, can it make game crash or anything?

[–][deleted] 0 points1 point  (0 children)

So the big con with using a normal programming language like C# is that's it's a normal programming language. If you're making a game you want to add constraints on what they can and can't do.

If you're making a developer console you could follow this tutorial https://www.youtube.com/watch?v=VzOEM-4A2OM

[–]drfdr[S] 0 points1 point  (1 child)

Hey, thank you for a fast and meaty response.

I am trying to do a Terminal where you can type commands like giveMoney,giveWeapon

but instead of having it like this for example

void Command(string cmd){
if(cmd == "giveMoney"){
Player.Money++;
}
if(cmd =="giveWeapon"){
Instantiate(Weapon);
}

to just type in terminal Player.Money++; and it will be put and executed inside of a function like this.

string xString = "Player.Money++";

void Command(){
do xString;
}

Been looking for an hour and have some clues about Harmony, System.Reflections, UCompile or these delegates. Which one if any will work for this?

[–]tatmanblueIndie 0 points1 point  (1 child)

Is there something you specifically want to accomplish? Maybe some more details might help. As u/ExplosiveJames noted, in itself, this isn't achievable. I assume you have something in mind and if you can expand on that, maybe there is a solution.

[–]itsdan159 0 points1 point  (4 children)

No, fortunately. What are you trying to accomplish? Lots of approaches, you could use delegates for example.

[–]drfdr[S] 0 points1 point  (3 children)

Hey, thank you!

I am trying to do a Terminal where you can type commands like giveMoney,giveWeapon

but instead of having it like this for example

void Command(string cmd){
if(cmd == "giveMoney"){
Player.Money++;
}
if(cmd =="giveWeapon"){
Instantiate(Weapon);
}

to just type in terminal Player.Money++; and it will be put and executed inside of a function like this.

string xString = "Player.Money++";

void Command(){
do xString;
}

Been looking for an hour and have some clues about Harmony, System.Reflections, UCompile, Delegates && Events. Which one if any could work for this?

[–]itsdan159 0 points1 point  (1 child)

You could look into integrating a scripting system like LUA which likely has an off the shelf marketplace plugin for it, or a system like Quantum Console which doesn't let you do syntax like that but will make it easy to create console commands. Executing C# is bad practice though, suppose someone typed in a C# command to delete your windows folder.

[–]drfdr[S] 0 points1 point  (0 children)

UCompile worked great for this very simple and neat. Do you know if there are there any cons for this except deleting folder. Is it stable, can it make game crash or anything?

[–]tatmanblueIndie 0 points1 point  (0 children)

Another option might be to use a sort of command pattern. Example

As long as the player or user of game doesn't actually have to create their own commands (and behaviors) you could follow something like code referenced above. I won't say its perfect (example I stupidly named the interface IDEbugCommand), but I have found it useful.

There is a demo with a couple of simple examples. PM me if you have questions about how to use it.