Repository navigation
Instance Helper
InstanceHelper creates compatible classes dynamically by type name, Type, or an existing object's type.
It works well with Discoverable System, but it does not require [Discoverable].
Suppose your game defines:
public interface IEnemyFactory
{
}with several implementations.
If you only know the class name at runtime:
IEnemyFactory factory =
InstanceHelper.CreateInstance<IEnemyFactory>(
"GoblinFactory",
ignoreCase: true,
args: null);The result is null if VOID cannot find or construct a compatible type.
var factory =
InstanceHelper.CreateInstance<IEnemyFactory>(
"GoblinFactory",
true,
null);Constructor arguments:
var service =
InstanceHelper.CreateInstance<IGameService>(
"SaveService",
true,
new object[]
{
"profile-1",
3
});Activator.CreateInstance is used internally, so the supplied argument list needs to match a constructor that can be invoked.
if (InstanceHelper.TryCreateInstance<IEnemyFactory>(
"GoblinFactory",
true,
null,
out var factory))
{
// Use factory.
}This is the common pairing with DiscoverableHelper:
Type type =
DiscoverableHelper.FindSingleByName<IGameplayModule>(
"Weather");
IGameplayModule module =
InstanceHelper.CreateInstanceFromType<IGameplayModule>(
type,
null);Try version:
if (InstanceHelper.TryCreateInstanceFromType<IGameplayModule>(
type,
null,
out var module))
{
module.Initialize();
}IEnemyFactory copy =
InstanceHelper.CreateInstanceFromObject<IEnemyFactory>(
existingFactory,
null);Try version:
if (InstanceHelper.TryCreateInstanceFromObject<IEnemyFactory>(
existingFactory,
null,
out var copy))
{
// Use copy.
}CreateInstance<T>(name, ...) scans loaded non-framework assemblies for a class whose full name or simple name matches and that can be assigned to T.
Successful lookups are cached.
Failed lookups are also cached so an invalid name does not repeatedly scan every assembly.
If your own code loads another assembly at runtime, refresh the instance helper afterward:
Assembly.LoadFrom("Plugins/MyPlugin.dll");
InstanceHelper.RefreshAssemblies();
DiscoverableHelper.InvalidateCaches();That only refreshes VOID's lookup/discovery caches. It does not make arbitrary DLL loading safe.
If you build a plugin/mod system, you are responsible for:
- deciding where assemblies come from
- validating/trusting them
- loading them
- defining their lifecycle
- handling compatibility/versioning
- deciding whether unrestricted code execution is acceptable
See Mod Support.
public interface IGameplayModule
{
void Initialize();
}
[Discoverable(
Name = "Weather",
Category = "Gameplay")]
public sealed class WeatherModule : IGameplayModule
{
public void Initialize()
{
Console.WriteLine("Weather ready");
}
}
IReadOnlyList<Type> types =
DiscoverableHelper.FindAll<IGameplayModule>();
foreach (Type type in types)
{
if (InstanceHelper.TryCreateInstanceFromType<IGameplayModule>(
type,
null,
out var module))
{
module.Initialize();
}
}Home · Getting Started · Rendering · Custom Renderers · GitHub · Report an Issue
Built with VOID Engine · MIT License
- Coroutines
- State Machine
- Save System
- Pathfinding
- Logging
- Beacon Manager
- Beacon Event System
- Discoverable System
- Instance Helper
- BeaconManager Extensions
- BeaconHandle Extensions
- Enum Extensions
- String Extensions
- Int Extensions
- Float Extensions
- IEnumerable Extensions
- Random Extensions
- Sound Extensions
- Font Extensions