❔ 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
You can depend on the roslyn compiler and do this if you wish, but it's not the simplest thing in the world
Honestly as long as it works it doesn't rly depend on how complex it is
It's also important to not think of another C# file as a "script"
I mean im talking abt a different .cs file
Yes. It's another file
Not a script
A script implies that there's a single entry point that will execute automatically
Ah
Yes yes
what about dotnet script tooling?
( this is a very loaded question )
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
filehuh; I thought a csx file was just c# with some csproj syntax fluff and a separate file extension by convention
333fred
REPL Result: Success
Compile: 460.950ms | Execution: 27.298ms | React with ❌ to remove this embed.
Legal csx. Not legal C#
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)
@fabiogaming what is your actual goal here? What problem are you trying to solve?
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
do you want to build a plugin model?
if that's what it's called
plugin's are usually compiled bits of code that fulfill a predefined contract; and include a method to load them at run-time.
Yeah: rather than including .cs files, I would look at including dlls
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
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?
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 it could also be useful for other porpuses other than a small engine
Looks like what I
assembly load context can be challenging to work with; I'd recommend starting small and building up
Is that plugin stuff also how games allow for mods or?
it depends
Yee I'll make some tests and small things and when I understand the process I'll try bigger
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.
I see
Thanks
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.