Skip to main content
GameDev.net gamedev.net
Using GameDev.net for your class this semester?
Learn more →
🔒 Locked

[UNITY3D] ACTORS - The component based framework

Started by Pixeye May 29, 2018 at 8:23 AM 4 replies 4.7k views
Original Post
Pixeye
Pixeye

logo_framework.png

Get%20on-Github-orange.png discord-join%20channel-brightgreen.png twitter-%40dimmPixeye-blue.png


ACTORS is a small framework explicitly built for Unit3d. It is used to ease the pain of decoupling data from behaviors without tons of boilerplate code. It relies on Unity3d scripting monobehavior concept but without unnecessary overhead.
All game related entities are called Actors - they inherit from monobehavior and take role of container of data and behavior components.
Like that :

  public class ActorPlayer : Actor, ITick
{

	[FoldoutGroup("Setup")] public DataMove dataMove;
	 
    	     protected override void Setup()
		{
			Add(dataMove);
			Add<BehaviorInput>();
		}
 
}


	[System.Serializable]

	public class DataMove : IData
	{
		public float x;
		public float y;

		public void Dispose()
		{
		}
	}


		public class BehaviorInput : Behavior, ITick
	{

		[Bind] private DataMove dataMove;


		public override void OnTick()
		{
			dataMove.x = Input.GetAxis("Horizontal");
			dataMove.y = Input.GetAxis("Vertical");
		}
	}

The framework includes:

  • Time management
  • Custom Update Management.

    While Monobehavior update method can be used only with inherited mono components, you can use framework Update method in ANY script.

    
    public class MyCustomClass : ITick{
    
    public MyCustomClass(){
       ProcessingUpdate.Default.Add(this);
    }
    
    public void Tick(){
        
    }
    
    }


  • Object pooling both for game objects and plain c# objects
  • Signal-event system
  • Toolbox-singleton ( a simple servie locator )
  • Simple ECS for actors
  • Unity additive scenes solution
  • Blueprints - scriptable objects to define common actors data
  • Extended tags system
  • Timers for delayed action
  • 
    Timer.Add(0.75f, () => actor.tags.Remove(Tag.StateImmortal));

    FoldGroup attribute to make your inspector look more organized:

    68747470733a2f2f692e6779617a6f2e636f6d2f

  • Bind attribute for lazy initialization of components inside of behaviors
    
    [Bind] private DataMove dataMove;

    Currently I'm working on documentation and example projects. I use framework in my working projects and always keep it updated.

indie game developer.  Currently I'm working on game called Battlecruiser.
Pixeye
Pixeye

Added documentation for game object pooling pattern and timers.

Timers are delayed actions that can be cached.


Timer.Add(0.1f, actor.HandleDestroyGO); 

Caching timers and reuse them.


private Timer timerBlink;
 timerBlink = new Timer(BlinkFinish, 0.15f);
 
 void Blink(){
 timerBlink.Restart();
 }

Or add ID to manipulate with group of timers that belong to one object.


 Timer.Add(0.1f, actor.HandleDestroyGO).AddID(actor);
var timers = Timer.FindAllTimers(actor);
     
    if (timers != null)
     for (var i = 0; i < timers.Count; i++)
        {
        timers[i].timeScale = 0.5f;
        }


indie game developer.  Currently I'm working on game called Battlecruiser.
Pixeye
Pixeye

Added descriptions for Blueprints.
Blueprints are scriptable objects for sharing common data among similar objects.

Example of pulling data from blueprint:


var weaponData = Get<DataBlueprint>().Get<DataWeapon>();

Example of blueprint:


[CreateAssetMenu(fileName = "BlueprintCreature", menuName = "Blueprints/BlueprintCreature")]
   public class BlueprintCreature : Blueprint
   {
    [FoldoutGroup("Setup")]
    public DataWeapon dataWeapon;
    
    [FoldoutGroup("Setup")]
    public DataDeathAnimations dataDeathAnimations;
	    public override void Setup()
    {
     Add(dataWeapon);
     Add(dataDeathAnimations);
    }
   }
indie game developer.  Currently I'm working on game called Battlecruiser.
Pixeye
Pixeye

Added documentation for ECS approach.
Simple ECS pattern for working with actors. My approach can be used only with actor classes at the current moment and is far less powerful than clean ECS approaches and it's used more for structuring than gaining performance boost.



public class ProcessingCameraFollow : ProcessingBase, ITick, IMustBeWipedOut{
[GroupBy(Tag.GroupPlayer)]
[GroupExclude(Tag.StateDead)]
private Group groupPlayers;
	   public ProcessingCameraFollow()
   {
      groupPlayers.OnGroupChanged += OnGroupPlayersChanged;
   }
   void OnGroupPlayersChanged()
    {  
      for(var i=0;i<group.length;i++){
         Debug.Log("Actor: " + group.actors[i]);
      }
    }
    
   public void Tick()
   {
         for(var i=0;i<group.length;i++){
         DoSomething(group.actors[i]);
            }
    }
    
    void DoSomething(Actor a){
    }
    
}


indie game developer.  Currently I'm working on game called Battlecruiser.
Pixeye
Pixeye
Added descriptions for tags
Tags are the glue for your game: You can identify your actors with tags or use them as arguments for your signals to check game logic. Tags are simple cont INT variables.

// Add stun marker from the mighty hammer of doom.
tags.Add(Tag.Stunned);
// Add stun marker from falling off the tree.
tags.Add(Tag.Stunned);
// remove effect caused by the mighty hammer of doom.
tags.Remove(Tag.Stunned);
// we are still stunned because we added two stun effects and removed only one
bool condition_stunned = tags.Contain(Tag.Stunned);
Tags can be used in the Inspector window.

  {
  [TagField(categoryName = "Weapons")] public const int WeaponGun = 9000;
  [TagField(categoryName = "Weapons/BigGuns")] public const int WeaponLaser = 9001;
    }

[TagFilter(typeof(Tag))] public int tag;	
68747470733a2f2f692e6779617a6f2e636f6d2f
indie game developer.  Currently I'm working on game called Battlecruiser.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.