Admer456 / halflife-adm-legacy

Advanced Development Mod - technical overhaul for Half-Life
Other
17 stars 5 forks source link

Scripting system #14

Closed Admer456 closed 3 years ago

Admer456 commented 4 years ago

A scripting system would be very beneficial for modders who don't want to compile a new DLL or can't do that. It'd be much much easier for people to change the functionality of something or create a new entity.

For this specifically, C# seems like the most ideal: it's simple and forgiving unlike C++, it's not hard to integrate it as a scripting lang, it's fast, it's powerful and its syntax is similar enough to C++.

[Entity("func_breakable_model", EntityTypes.PointEntity)]
public class FuncBreakable : BaseEntity
{
   public void Spawn()
   {
      SetModel( modelName );
   }

   public void Use( ref BaseEntity activator, ref BaseEntity caller, UseType useType, float value )
   {
      Gib();
   }

   [KeyValueString("model"), SaveRestoreString("mdl")]
   private string modelName;
   // Some extra code here for Gib() and stuff
   // ...
   // ...
}

This example could be even better, but you can already see how some things are wildly different.

So, in short, there would be .cs files inside a scripts folder. The mod would implement a custom CoreCLR host, which could load C# assemblies, which could be used to compile the C# scripts and interact with them.

At first, there will be a simple, map-wide script, where each map will be able to have its own "level script" or "mission script" if you will. Naturally, an entity that calls methods from the script will need to be written in the native DLL, a trigger_callscript if you will.

Later on, we could see custom entity classes in C#, eventually leading to custom monsters and weapons. The last would be kind of tricky to achieve due to the networking nature of weapons (clientside prediction and such). There is a big question of security concerning clientside scripts, but it'll be possible to make it at least a little safe by defining some limitations, such as "no using this library" and "no loading this DLL", and providing our own, safer replacements if needed.

The basic functionality (simple map scripts) may get implemented in 0.5.0.

Admer456 commented 3 years ago

This has been moved to CSharpScripting.md.