Skip to content

Instantly share code, notes, and snippets.

@dragonslaya84
Last active September 11, 2022 21:36
Show Gist options
  • Save dragonslaya84/88e6bb998eda8fe4ee912f01d67feec9 to your computer and use it in GitHub Desktop.
Save dragonslaya84/88e6bb998eda8fe4ee912f01d67feec9 to your computer and use it in GitHub Desktop.
Automcatic way to control svelto engine order without having to pass in a string using attributes
using System;
using System.Collections.Generic;
using Svelto.Common;
using Svelto.DataStructures;
using Svelto.ECS.SveltoOnDOTS;
using UnityEngine;
namespace Aeradona.ECS.Systems
{
/// <summary>
/// The order of the engines found in the enginesOrder array is the order of execution of engines
/// </summary>
public struct SequenceEngineOrder : ISequenceOrder
{
private string[] Setup()
{
var engineList = new FasterList<string>();
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
for (int i = 0; i < assemblies.Length; i++)
{
if (!assemblies[i].GetName().Name.Contains("Aeradona.ECS.Systems")) continue;
var types = assemblies[i].GetTypes();
for (int j = 0; j < types.Length; j++)
{
var attributes = Attribute.GetCustomAttributes(types[j]);
foreach (var attribute in attributes)
{
if (attribute is not SequencedAttribute foundAttribute) continue;
if (foundAttribute.priority > engineList.count)
{
engineList.IncrementCountBy((uint)(foundAttribute.priority - engineList.count));
if (engineList.count <= foundAttribute.priority)
engineList[foundAttribute.priority - 1] = foundAttribute.name;
else
{
engineList[foundAttribute.priority] = foundAttribute.name;
}
}
else
{
if (!string.IsNullOrEmpty(engineList[foundAttribute.priority]))
{
#if UNITY_EDITOR
Debug.Log(
"Enginesort: Overriding list. Please check sequence numbers for duplicate numbers.");
#endif
}
engineList[foundAttribute.priority] = foundAttribute.name;
}
}
}
}
return engineList.ToArray();
}
public string[] enginesOrder => Setup();
}
public class EngineSort: SortedJobifiedEnginesGroup<IJobifiedEngine, SequenceEngineOrder>
{
public EngineSort(FasterList<IJobifiedEngine> engines) : base(engines)
{
}
}
}
using System;
namespace Svelto.ECS.Extensions
{
public class SequencedAttribute : Attribute
{
public SequencedAttribute(string name, uint priority) { this.name = name; this.priority = priority; }
public string name { get; }
public uint priority { get; }
}
}
@dragonslaya84
Copy link
Author

dragonslaya84 commented Feb 27, 2022

Usage

[Sequenced(nameof(EnemySpawnSystem), 1)] // The number represents order it will be added to engine and executed in engine
[Svelto.Common.Sequenced(nameof(EnemySpawnSystem))]
public class EnemySpawnSystem : BaseJobSystem<EnemySpawnSystem>
{
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment