❔ Can C# "compile" C# on the go

Hey, i was wondering if C# is capable of "compiling" and executing another C# script in the same directory on runtime
30 Replies
333fred
333fred17mo ago
You can depend on the roslyn compiler and do this if you wish, but it's not the simplest thing in the world
The Fog from Human Resources
Honestly as long as it works it doesn't rly depend on how complex it is
333fred
333fred17mo ago
It's also important to not think of another C# file as a "script"
The Fog from Human Resources
I mean im talking abt a different .cs file
333fred
333fred17mo ago
Yes. It's another file Not a script A script implies that there's a single entry point that will execute automatically
The Fog from Human Resources
Ah Yes yes
Mayor McCheese
Mayor McCheese17mo ago
what about dotnet script tooling? ( this is a very loaded question )
333fred
333fred17mo ago
A csx file is not C# It is very similar to C# And those do, in fact, have a single entry point that will execute automatically But it is not another .cs file
Mayor McCheese
Mayor McCheese17mo ago
huh; I thought a csx file was just c# with some csproj syntax fluff and a separate file extension by convention
MODiX
MODiX17mo ago
333fred
REPL Result: Success
int i;
i.ToString();
int i;
i.ToString();
Compile: 460.950ms | Execution: 27.298ms | React with ❌ to remove this embed.
333fred
333fred17mo ago
Legal csx. Not legal C#
The Fog from Human Resources
Another question regarding my root question Can I call / invoke functions of a seperate file and can I extract data from it (if it's a string function for example)
333fred
333fred17mo ago
@fabiogaming what is your actual goal here? What problem are you trying to solve?
The Fog from Human Resources
I'm actually not quite sure yet but lately I've been getting into graphics libraries and I thought it would be kinda cool to have smth like unity where instead of hardcoding everything, I could make the main program act sort of like a game engine instead of a game embedded into the engine Just playing around a bit yknow
Mayor McCheese
Mayor McCheese17mo ago
do you want to build a plugin model?
The Fog from Human Resources
if that's what it's called
Mayor McCheese
Mayor McCheese17mo ago
plugin's are usually compiled bits of code that fulfill a predefined contract; and include a method to load them at run-time.
333fred
333fred17mo ago
Yeah: rather than including .cs files, I would look at including dlls
The Fog from Human Resources
Essentially what I want in the end is that I could have functions like Start and Update like in unity and then have my logic there For each script that Id want to attach to it
Mayor McCheese
Mayor McCheese17mo ago
unity does a lot of things for unity purposes, and replicating them to solve normal programming problems might be a bit of an anti pattern. so are you trying to build something that replicates what unity is doing because you know unity?
The Fog from Human Resources
Nah I'm trying to make a little game "engine" where I instead of having to hard code all the entities can actually call on the existing components and stuff It's hard to SCcrying it could also be useful for other porpuses other than a small engine
The Fog from Human Resources
Looks like what I SCcattokiss2
Mayor McCheese
Mayor McCheese17mo ago
assembly load context can be challenging to work with; I'd recommend starting small and building up
The Fog from Human Resources
Is that plugin stuff also how games allow for mods or?
Mayor McCheese
Mayor McCheese17mo ago
it depends
The Fog from Human Resources
Yee I'll make some tests and small things and when I understand the process I'll try bigger
Mayor McCheese
Mayor McCheese17mo ago
I don't write any games, but yeah, basically most plugins fulfill a simple interface, where you take in a game object/state and have the opportunity to inspect or mutate game objects/state depending on the requirements of the plugin. there are more complex subsystems for mods, but at the core you're inspecting some game object state and providing modification to that object or state.
The Fog from Human Resources
I see Thanks
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server