So I'm just starting to learn events and I've made a PeriodicTick class that inherits from a Tick class that I've written. Now I think it works okay, but I think I read earlier that it's always a bad idea to use Thread.Sleep() now I'm using it in a seperate task so there shouldn't be any problems with parallelization.
Nevertheless, my question is, in this case is it fine to use Thread.Sleep() and if not is there any other simple solution for me to achieve the same thing?
EDIT: Right! Here's the code..
class Tick
{
#region Fields & Properties
public delegate void TickHandler();
static int ctr = new int();
public readonly int ID;
public event TickHandler OnTick;
public bool isActive;
#endregion
public Tick()
{
ID = ctr++;
}
public virtual void Start()
{
OnTick();
}
}
class PeriodicTick : Tick
{
int tickTimer;
public Predicate<PeriodicTick> TickUntil { get; set; }
public PeriodicTick(int tickTimer, Predicate<PeriodicTick> tickUntil)
{
this.tickTimer = tickTimer;
TickUntil = tickUntil;
}
public override void Start()
{
Task TickTimer = Task.Factory.StartNew(
() =>
{
while (TickUntil.Invoke(this))
{
System.Threading.Thread.Sleep(tickTimer);
base.Start();
}
});
}
}
}
.NET 4.5 introduced Task.Delay(Int32), that would be the correct way to handle delays in Tasks.
thread-sleep-vs-task-delay
Related
I have a function that asynchronously loads a xml file, parses it, and adds certain values to a list. I'm using async and await for this. The issue I've run into is that after calling await the program moves on to executing code that accesses that list before the async function has finished adding all items.
My static class with async function:
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Xml.Linq;
using UnityEngine;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.AddressableAssets;
namespace Drok.Localization
{
public static class Localization
{
/// <summary>
/// The currently available languages.
/// </summary>
public static List<string> Available { get; private set; } = new List<string>();
/// <summary>
/// The currently selected language.
/// </summary>
public static string Current { get; private set; } = null;
public static async Task Initialize()
{
await LoadMetaData();
}
private static async Task LoadMetaData()
{
AsyncOperationHandle<TextAsset> handle = Addressables.LoadAssetAsync<TextAsset>("Localization/meta.xml");
TextAsset metaDataFile = await handle.Task;
XDocument metaXMLData = XDocument.Parse(metaDataFile.text);
IEnumerable<XElement> elements = metaXMLData.Element("LangMeta").Elements();
foreach (XElement e in elements)
{
string lang = e.Attribute("lang").Value;
int id = Int32.Parse(e.Attribute("id").Value);
Debug.LogFormat("Language {0} is availible with id {1}.", lang, id);
Available.Add(lang);
}
}
public static void LoadLanguage(string lang)
{
Current = lang;
throw new NotImplementedException();
}
public static string GetString(string key)
{
return key;
}
}
}
The class that initializes it and accesses the list:
using Drok.Localization;
using UnityEngine;
namespace Spellbound.Menu
{
public class LanguageMenu : MonoBehaviour
{
private async void Awake()
{
await Localization.Initialize();
}
private void Start()
{
Debug.Log(Localization.Available.Count);
}
private void Update()
{
}
}
}
I have no idea how to prevent access to that list until after all items have been added. The code I posted just collects info on what languages are available so that only the one language being used can be loaded later.
A Task<T> represents some value (of type T) that will be determined in the future. If you make your property this type, then it will force all callers to await for it to be loaded:
public static class Localization
{
public static Task<List<string>> Available { get; private set; }
static Localization() => Available = LoadMetaDataAsync();
private static async Task<List<string>> LoadMetaDataAsync()
{
var results = new List<string>();
...
results.Add(lang);
return results;
}
}
Usage:
private async Task StartAsync()
{
var languages = await Localization.Available;
Debug.Log(languages.Available.Count);
}
One possibility might be to add some logic to wait for the metadata to be loaded when returning the list from the get accessor.
One way to do this is to have a bool field that is set to true when the list is ready, and then we either return a private backing List<string> or null, depending on the value of our bool field:
public static class Localization
{
private static bool metadataLoaded = false;
private static List<string> available = new List<string>();
// The 'Available' property returns null until the private list is ready
public static List<string> Available => metadataLoaded ? available : null;
private static async Task LoadMetaData()
{
// Add items to private 'available' list here
// When the list is ready, set our field to 'true'
metadataLoaded = true;
}
}
The Awake method is async void, so there is no way for the caller to guarantee that it finishes before moving on to something else.
However, you could preserve the task and await it in the Start method to ensure that it is completed. Awaiting it twice does not harm anything.
public class LanguageMenu : MonoBehaviour
{
private Task _task;
private async void Awake()
{
_task = Localization.Initialize();
await _task;
}
private async void Start()
{
await _task;
Debug.Log(Localization.Available.Count);
}
private void Update()
{
}
}
Expanding on Rufus' comment:
Declare a bool property that's initialized to false. And in your list's getter, return the list only if the said bool property is true, and return maybe null (this depends on your requirements) if false.
public static bool IsAvailable { get; set; } = false;
private static List<string> _available;
public static List<string> Available
{
get
{
if (IsAvailable)
return _available;
else
return null;
}
set { _available = value; }
}
Finally, in your async function, when the work is done set the above property to true.
Latest when there is an Update method involved that should also wait with its execution using async and await might not be enough anyway.
Usually there is always one big alternative to using async for the Unity messages: an event system like e.g.
public static class Localization
{
public static event Action OnLocalizationReady;
public static async void Initialize()
{
await LoadMetaData();
OnLocalizationReady?.Invoke();
}
...
}
And wait for that event in any class using it like e.g.
public class LanguageMenu : MonoBehaviour
{
private bool locaIsReady;
private void Awake()
{
Localization.OnLocalizationReady -= OnLocalizationReady;
Localization.OnLocalizationReady += OnLocalizationReady;
Localization.Initialize();
}
private void OnDestroy ()
{
Localization.OnLocalizationReady -= OnLocalizationReady;
}
// This now replaces whatever you wanted to do in Start originally
private void OnLocalizationReady ()
{
locaIsReady = true;
Debug.Log(Localization.Available.Count);
}
private void Update()
{
// Block execution until locaIsReady
if(!locaIsReady) return;
...
}
}
Or for minimal better performance you could also set enabled = false in Awake and set it to true in OnLocalizationReady then you could get rid of the locaIsReady flag.
No async and await needed.
If you would move the Localization.Initialize(); instead to Start you would give other classes the chance to also add some callbacks before to Localization.OnLocalizationReady in Awake ;)
And you can extend this in multiple ways! You could e.g. together with firering the event directly also pass in the reference to Availables so listeners can directly use it like e.g.
public static class Localization
{
public static event Action<List<string>> OnLocalizationReady;
...
}
and then in LanguageMenu change the signiture of OnLocalizationReady to
public class LanguageMenu : MonoBehaviour
{
...
// This now replaces whatever you wanted to do in Start originally
private void OnLocalizationReady(List<string> available)
{
locaIsReady = true;
Debug.Log(available.Count);
}
}
If anyway LanguageMenu will be the only listener then you could even pass the callback directly as parameter to Initialize like
public static class Localization
{
public static async void Initialize(Action<List<string>> onSuccess)
{
await LoadMetaData();
onSuccess?.Invoke();
}
...
}
and then use it like
private void Awake()
{
Localization.Initialize(OnLocalizationReady);
}
private void OnLocalizationReady(List<string>> available)
{
locaIsReady = true;
Debug.Log(available.Count);
}
or as lambda expression
private void Awake()
{
Localization.Initialize(available =>
{
locaIsReady = true;
Debug.Log(available .Count);
}
}
Update
As to your question about later Initialization: Yes there is a simple fix as well
public static class Localization
{
public static event Action OnLocalizationReady;
public static bool isInitialized;
public static async void Initialize()
{
await LoadMetaData();
isInitialized = true;
OnLocalizationReady?.Invoke();
}
...
}
Then in other classes you can do it conditional either use callbacks or Initialize right away:
private void Awake()
{
if(Localization.isInitialized)
{
OnLocaInitialized();
}
else
{
Localization.OnInitialized -= OnLocaInitialized;
Localization.OnInitialized += OnLocaInitialized;
}
}
private void OnDestroy ()
{
Localization.OnInitialized -= OnLocaInitialized;
}
private void OnLocaInitialized()
{
var available = Localization.Available;
...
}
private void Update()
{
if(!Localization.isInitialized) return;
...
}
I am working with background workers to update a progress bar in a WPF UI I am working on. This background worker is getting its progress updates from multiple events that I am subscribed to, because the progress bar goes through several loading stages, and the percentages for those come from several places. here is some example/pseudo code explaining what I mean
The DoWork method of my background worker and the methods I am using to currently get some progress updates
// These are working fine
private void BwOnDoWork(object sender, DoWorkEventArgs doWorkEventArgs)
{
orderProcessing.OnOrderProgress += OrderStatus;
orderProcessing.OnStandardOrderProgress += StandardOrderStatus;
orderProcessing.CreateOrders(orders);
}
private void OrderStatus(int currentCount, int totalItems, string Message)
{
if (totalItems > 0)
bw.ReportProgress(Convert.ToInt32(((double)currentCount / (double)totalItems) * 100),
Message);
}
private void StandardOrderStatus(int currentCount, int totalItems, string Message)
{
if (totalItems > 0)
bw.ReportProgress(Convert.ToInt32(((double)currentCount / (double)totalItems) * 100),
Message);
}
Some code from my order processing class
public abstract class OrderProcessing
{
public delegate void OrderProgress(int CurrentItems, int TotalItems, string Message);
public event MasterSalesOrder.StandardOrderProgress OnStandardOrderProgress;
public event OrderProgress OnOrderProgress;
public abstract List<MasterSalesOrder> CreateOrders(List<Order> orders);
}
Some code from the class that holds the override method for CreateOrders()
public abstract class OrderProcessingFile : OrderProcessing
{
public event OrderProgress OnOrderProgress;
public override List<MasterSalesOrder> CreateOrders(List<Order> orders)
{
//Does Some Stuff
foreach(var stuff in stuffs)
{
OnOrderProgress(currentCount, totalCount, "Message");
}
}
}
Since I am clearly not explaining this well, I need to get info from the OrderProcessingFiles OnOrderProgress event via the OrderProcessing class that I create in the DoWork method.I am unsure on how to subscribe to an event when my code never directly instantiates an instance of the OrderProcessingFile class and it is never directly referred to.
I have tried looking for answers but as my title will show I am having a hard time even wording this in a way to get useful results, and I am genuinely stuck on this one. Let me know if more detail is needed, I tried to strip down my code to only the relevant parts but I feel like I'm explaining this strangely.
I would recommend that you create a thread safe singleton progress manager. Then have each of the background workers contact it with updates. The progress manager will use a DispatcherTimer (which runs on the GUI thread) to update the GUI appropriately.
Raw example:
public static class StatusReportManager
{
// Standard singleton code to create the manager and access it.
// Start/create the dispatch time as well.
private static DispatcherTimer Timer { get; set; }
private static object _syncObject = new object();
public static void ReportStatus(...)
{
lock (_syncObject)
{
// Process any states and set instance properties for reading
// by the timer operation.
}
}
private void ShowStatus() // Used by the dispatch timer
{
lock (_syncObject)
{
// Do any updates to the GUI in here from current state.
}
}
}
I have realized what it is I was really trying to do and have thus found an answer. Using the method found in this MSDN article I have implemented the follow code:
This is my UI
private void BwOnDoWork(object sender, DoWorkEventArgs doWorkEventArgs)
{
orderProcessing.OnOrderProgress += OrderStatus;
orderProcessing.CreateOrders(FanGlobal.BrandItems, FanGlobal.BrandItemMasterCustomers);
}
private void OrderStatus(object obj, OrderProcessing.OrderProgressEventArgs e)
{
if (e.totalCount > 0)
bw.ReportProgress(Convert.ToInt32(((double)e.currentCount / (double)e.totalCount) * 100),e.message);
}
This in my OrderProcessing class
public event EventHandler<OrderProgressEventArgs> OnOrderProgress;
public class OrderProgressEventArgs : EventArgs
{
public int currentCount;
public int totalCount;
public string message;
public OrderProgressEventArgs(int c, int t, string m)
{
currentCount = c;
totalCount = t;
message = m;
}
}
protected virtual void OnOrderProgressChanged(OrderProgressEventArgs e)
{
EventHandler<OrderProgressEventArgs> handler = OnOrderProgress;
if (handler != null)
{
handler(this, e);
}
}
public abstract List<MasterSalesOrder> CreateOrders(List<BrandItem> BrandItems = null, List<BrandItemMasterCustomer> BrandItemMasterCustomers = null);
and then I can use it in my child class OrderProcessingFile like so
public override List<MasterSalesOrder> CreateOrders(List<BrandItem> BrandItems = null, List<BrandItemMasterCustomer> BrandItemMasterCustomers = null)
{
//Do some Stuff
OnOrderProgressChanged(new OrderProgressEventArgs(count, totalItems, "Extracting"));
}
and everything is working like a charm. Sorry for the utterly confusing question and the apparent huge gap of knowledge I have/had, but hopefully this will help someone else in the future.
I have an enumeration prior.
Each of my scripts has a property priority of prior type. (Every script has its own class)
I have a data provider, which can send events every frame.
I want a script to subscribe only to an event which has arguments with priority equal to the script's one.
For example, a script with moderate priority should receive only events with moderate parameter of event arguments
prior has too many members to create a special event argument class for each.
Unfortunately:
a)I know only how to subscribe to a certain event type.
b)I can't make a generic class for event arguments, because elements of enum are not types
How can I do it?
The project currently looks this way:
public class TDefault:MonoBehaviour,IDefault
{
public enum prior
{
none,
...,
terminal
};
prior priority;
public virtual void apply()//For override by scripts
{
}
void Start()
{
//There should be adding a method which calls apply() when event_manager
//sends Event with a certain priority
}
public TDefault ()
{
if(essential==null)
essential=new TEssential();
}
}
public class TApplyEventParam : EventArgs
{
public TDefault.prior priority;
public TApplyEventParam(TDefault.prior _priority)
{
priority=_priority;
}
}
public class event_manager : TDefault
{
//This has fixed type
event EventHandler<TApplyEventParam> handler=new EventHandler<TApplyEventParam>();
void Update ()
{
foreach (prior p in (prior[]) Enum.GetValues(typeof(prior)))
{
handler(this,new TApplyEventParam(p));
}
}
}
The problem you're dealing with, if I understood it correctly, is that you would like to have your event subscription conditionally called depending on the event payload (the priority value inside the TApplyEventParam). That is something that you cannot do which results in you having to filter out the unwanted events inside your event handler like proposed by #Henk-Holterman
Another approach could be to skip the usage of events and maintain your own list of subscribers inside the data provider.
Based on the terminology used by you in your question (not the code example) you could do something like this:
using System;
using System.Collections.Generic;
namespace Example
{
public enum Prior
{
None,
Moderate,
Terminal
};
public abstract class ScriptBase
{
public abstract Prior Prior { get; }
public abstract void Apply();
public void Start(DataProvider dataProvider)
{
dataProvider.Subscribe(Prior, Apply);
}
public void Stop(DataProvider dataProvider)
{
dataProvider.Unsubscribe(Prior, Apply);
}
}
public class ScriptHandlingModerateEvents : ScriptBase
{
public override Prior Prior
{
get { return Example.Prior.Moderate; }
}
public override void Apply()
{
Console.WriteLine("Handling moderate event by " + GetType().Name);
}
}
public class ScriptHandlingTerminalEvents : ScriptBase
{
public override Prior Prior
{
get { return Example.Prior.Terminal; }
}
public override void Apply()
{
Console.WriteLine("Handling terminal event by " + GetType().Name);
}
}
public class DataProvider
{
private readonly Dictionary<Prior, List<Action>> _subscribersByPrior;
public DataProvider()
{
_subscribersByPrior = new Dictionary<Prior, List<Action>>();
foreach (Prior prior in (Prior[])Enum.GetValues(typeof(Prior)))
{
_subscribersByPrior.Add(prior, new List<Action>());
}
}
public void Subscribe(Prior prior, Action action)
{
_subscribersByPrior[prior].Add(action);
}
public void Unsubscribe(Prior prior, Action action)
{
_subscribersByPrior[prior].Remove(action);
}
public void DoSomethingThatTriggersPriorEvents(int someValue)
{
Prior prior = someValue % 2 == 0 ? Prior.Moderate : Prior.Terminal;
foreach (var subscriber in _subscribersByPrior[prior])
{
subscriber();
}
}
}
public static class Program
{
public static void Main()
{
DataProvider dataProvider = new DataProvider();
var scriptHandlingModerateEvents = new ScriptHandlingModerateEvents();
scriptHandlingModerateEvents.Start(dataProvider);
var scriptHandlingTerminalEvents = new ScriptHandlingTerminalEvents();
scriptHandlingTerminalEvents.Start(dataProvider);
for (int i = 0; i < 10; i++)
{
dataProvider.DoSomethingThatTriggersPriorEvents(i);
}
scriptHandlingTerminalEvents.Stop(dataProvider);
scriptHandlingModerateEvents.Stop(dataProvider);
Console.WriteLine();
}
}
}
this way the DataProvider is not aware of scripts, but if that is not an issue, you could maintain a list of ScriptBase instances and check the Prior property inside the
DoSomethingThatTriggersPriorEvents like this:
public class DataProvider2
{
private readonly List<ScriptBase> _scripts = new List<ScriptBase>();
public void Subscribe(ScriptBase script)
{
_scripts.Add(script);
}
public void Unsubscribe(ScriptBase script)
{
_scripts.Remove(script);
}
public void DoSomethingThatTriggersPriorEvents(int someValue)
{
Prior prior = someValue % 2 == 0 ? Prior.Moderate : Prior.Terminal;
foreach (var script in _scripts)
{
if (script.Prior == prior)
{
script.Apply();
}
}
}
}
I'm currently abstracting the concept of timers so that my classes that need one can use mock timers in tests or different implementations in operative mode (e.g. threadpool timers, thread-affine timers, etc.). Therefore, I created this interface:
public interface ITimer : IDisposable
{
bool IsEnabled { get; }
bool IsAutoResetting { get; set; }
TimeSpan Interval { get; set; }
void Start();
void Stop();
event EventHandler IntervalElapsed;
}
Now I want to create a wrapper that adapts the System.Threading.Timer class and implements that interface. I want to do it using test-driven development. My class currently looks somewhat like this:
public sealed class ThreadPoolTimer : ITimer
{
private readonly Timer _timer;
public bool IsEnabled { get; private set; }
public bool IsAutoResetting { get; set; }
public TimeSpan Interval { get; set; }
public ThreadPoolTimer()
{
Interval = this.GetDefaultInterval();
_timer = new Timer(OnTimerCallback);
}
public void Dispose()
{
_timer.Dispose();
}
public void Start()
{
}
public void Stop()
{
}
private void OnTimerCallback(object state)
{
OnIntervalElapsed();
}
public event EventHandler IntervalElapsed;
private void OnIntervalElapsed()
{
var handler = IntervalElapsed;
if (handler != null)
handler(this, EventArgs.Empty);
}
}
My actual question is: how would you write unit tests that describe the (soft real-time) requirements for the behavior of Start, Stop and IntervalElapsed?
In my opinion, i should use e.g. an AutoResetEvent and check if the event is raised within a certain timespan (maybe +/- 3ms). But writing that code somewhat violates the DAMP (descriptive and meaningful phrases) principle, I think. Is there an easier way to do this?
Should I make the dependency to System.Threading.Timer external and then maybe use a shim for testing purposes? Unfortunately, the .NET timers do not have a common interface (which would make my work obsolete...)
What are your thoughts on that topic? Is there any documentation that I have not found yet and that I should read?
Sorry for having actually more than one question in this post, but this testing of soft real-time requirements is quite interesting, I think.
As no one answered this question yet, I'll tell you how I approached the problem: I used the spy pattern to actually implement the code that observes the behavior of the timer. The class looks like this:
public class ThreadPoolTimerSpy : IDisposable
{
private readonly ThreadPoolTimer _threadPoolTimer;
private int _intervalElapsedCallCount;
private readonly ManualResetEvent _resetEvent = new ManualResetEvent(false);
public int NumberOfIntervals { get; set; }
public DateTime StartTime { get; private set; }
public DateTime EndTime { get; private set; }
public ThreadPoolTimerSpy(ThreadPoolTimer threadPoolTimer)
{
if (threadPoolTimer == null) throw new ArgumentNullException("threadPoolTimer");
_threadPoolTimer = threadPoolTimer;
_threadPoolTimer.IntervalElapsed += OnIntervalElapsed;
NumberOfIntervals = 1;
}
public void Measure()
{
_intervalElapsedCallCount = 0;
_resetEvent.Reset();
StartTime = DateTime.Now;
_threadPoolTimer.Start();
_resetEvent.WaitOne();
}
private void OnIntervalElapsed(object sender, EventArgs arguments)
{
_intervalElapsedCallCount++;
if (_intervalElapsedCallCount < NumberOfIntervals)
return;
_threadPoolTimer.Stop();
EndTime = DateTime.Now;
_resetEvent.Set();
}
public void Dispose()
{
_threadPoolTimer.Dispose();
_resetEvent.Dispose();
}
}
This class takes a ThreadPoolTimer and registers to its IntervalElapsed event. One can specify how much intervals the spy should wait until it stops measuring. As I'm using a ManualResetEvent to block the thread that starts the timer in the Measure method, all calls to that method are synchronous, which results in DAMP code in the actual test class, in my opinion.
A test method that uses the spy would look like this:
[TestInitialize]
public void InitializeTestEnvironment()
{
_testTarget = new ThreadPoolTimerBuilder().WithAutoResetOption(true)
.WithInterval(100)
.Build() as ThreadPoolTimer;
Assert.IsNotNull(_testTarget);
_spy = new ThreadPoolTimerSpy(_testTarget);
}
[TestMethod]
public void IntervalElapsedMustBeRaisedExactlyTenTimesAfter1000Milliseconds()
{
CheckIntervalElapsed(10, TimeSpan.FromMilliseconds(1000), TimeSpan.FromMilliseconds(100));
}
private void CheckIntervalElapsed(int numberOfIntervals, TimeSpan expectedTime, TimeSpan toleranceInterval)
{
_spy.NumberOfIntervals = numberOfIntervals;
_spy.Measure();
var passedTime = _spy.EndTime - _spy.StartTime;
var timingDifference = Math.Abs(expectedTime.Milliseconds - passedTime.Milliseconds);
Assert.IsTrue(timingDifference <= toleranceInterval.Milliseconds, string.Format("Timing difference: {0}", timingDifference));
}
If you have any questions or recommendations, please feel free to leave a comment.
Besides: the tolerance interval I have to choose to make the tests pass are relatively high. I thought that maybe 3 to 5 milliseconds might suffice, but in the end for ten intervals I figured out that the actual measured time span is up to 72ms different than the expected time of 1000ms in this case. Well, never use a managed runtime for real time applications, I guess...
I'm making a game where workers perform actions based on a current Task. Each worker will be assigned a list of tasks, in a preferred order (which is influenced by the player's decisions).
When a task is completed (e.g. take item from X to Y), the worker needs to start a new task by checking through their list of possible tasks, see if each can be performed, and if so, set their current task to that task and start it (the last task - "Wander Around" is always going to be available).
I currently have this working using a big switch statement and Enums, but now want to generalise this code to create a Task class, and give the workers a list of preferred Tasks, a GetNextTask() function, and in the worker's Update() method, call currentTask.update() (this will get the worker to do whatever he's required to do under the current task, and which will call worker.GetNextTask() when the task is complete).
What I'm unclear on is the best way to store Tasks in the worker.
Should I use:
1) Reflection. Store the possible Tasks as a list of types, then use reflection to a) call a static method public static virtual bool CanPerformThisTask() which is overridden in each subclass, and b) Create an instance of that task for the worker?
(example attempt at code for this below - but unable to test yet)
2) Instantiate all the Tasks whenever a worker needs to get a new task (probably using Activator), and check (Task)task.CanPerformThisTask() for each one - if true, do that task. Instantiating them all seems inefficient though?
3) Generics. Can this be done using generics? If so, how?
Here is a snippet of my classes to give the idea of what I'm trying to do:
Worker Class:
protected List<Point> waypoints = new List<Point>();
public bool reachedDestination { get { return waypoints.Count == 0; } }
protected Task task;
public List<Type> possibleTasks;
public Worker(Task initialTask, List<Type> initialPossibleTasks ...)
: base(...)
{
task = initialTask;
possibleTasks = initialPossibleTasks;
}
public override void Update()
{
base.Update();
if (!reachedDestination) Move();
task.Update();
}
public void GetNextTask()
{
foreach (Type t in possibleTasks)
{
//reflection code here - will this work and can we do this with generics instead?
Bool canDoT = (bool)t.GetMethod("CanPerformThisTask", BindingFlags.Static | BindingFlags.Public).Invoke(null, null);
if (canDoT)
{
task = Activator.CreateInstance(t);
return;
}
}
}
Here is some incomplete code for my base Task class (which shouldn't be instantiated):
public class Task
{
public Worker worker;
public virtual static bool CanPerformThisTask()
{
//never call this from here - always from subclasses
return false;
}
public Task()
{
//set up code here
}
public virtual void Update()
{
//make worker do relevant activities here
//call finish task when done
}
public void FinishTask()
{
worker.GetNextTask();
}
}
and here is an example of a Task the worker will have in its list of possible tasks:
public class T_WorkerWander : Task
{
public static override bool CanPerformThisTask()
{
//can always wander (other Tasks will have conditions here)
return true;
}
public T_WorkerWander()
: base()
{
}
override public void Update()
{
//make the worker wander here
if (worker.reachedDestination) FinishTask();
}
}
Update: here is the code I've now got working
Task Class:
public abstract class Task
{
//the entity holding this task
public TaskableEntity taskEntity;
public List<TaskStage> taskStages;
public TaskStage currentTaskStage { get { return taskStages[0]; } }
public Task(TaskableEntity t) { taskEntity = t; }
/// <summary>
/// the conditions for the Task to be started
/// </summary>
public virtual bool CanStart()
{
return true;
}
public void Start()
{
taskStages = new List<TaskStage>();
InitialiseTaskStages();
taskStages[0].Start();
}
public abstract void InitialiseTaskStages();
public void Update()
{
currentTaskStage.Update();
if (currentTaskStage.IsComplete()) TaskStageComplete();
}
public void TaskStageComplete()
{
taskStages.RemoveAt(0);
if (taskStages.Count == 0) taskEntity.TaskComplete();
else currentTaskStage.Start();
}
public void SetTaskStages(params TaskStage[] t)
{
taskStages = t.ToList();
}
public void Interrupt()
{
currentTaskStage.Interrupt();
}
}
TaskStage class:
public sealed class TaskStage
{
private Task task;
private List<Point> pointsToMoveTo;
public void SetPointsToMoveTo(Point p) { pointsToMoveTo = new List<Point>() { p }; }
public void SetPointsToMoveTo(params Point[] p) { pointsToMoveTo = p.ToList(); }
public void SetPointsToMoveTo(List<Point> p) { pointsToMoveTo = p; }
public Action actionToApply;
private float timeToWait;
public void SetWait(float wait) { timeToWait = wait; }
private IReservable[] itemsToReserve;
public void SetItemsToReserve(params IReservable[] items) { itemsToReserve = items; }
private IReservable[] itemsToUnreserve;
public void SetItemsToUnreserve(params IReservable[] items) { itemsToUnreserve = items; }
private Emotion emotionToSet;
public void SetEmotionToSet(Emotion e) { emotionToSet = e; }
private TaskStage _interrupt;
public void SetInterruptAction(TaskStage t) { _interrupt = t; }
public void Interrupt() { _interrupt.Start(); }
public TaskStage(Task t)
{
task = t;
}
public void Start()
{
if (actionToApply != null) actionToApply();
if (itemsToUnreserve != null) UnreserveItems();
if (itemsToReserve != null) ReserveItems();
if (pointsToMoveTo != null)
{
//this will need changing after pathfinding sorted out...
if (pointsToMoveTo.Count == 1) task.taskEntity.SetWaypoints(pointsToMoveTo[0]);
else task.taskEntity.waypoints = pointsToMoveTo;
}
if (emotionToSet != null) emotionToSet.StartEmotion();
}
public void Update()
{
if (timeToWait > 0) timeToWait -= GV.elapsedTime;
}
public bool IsComplete()
{
if (pointsToMoveTo != null && !task.taskEntity.reachedDestination) return false;
if (timeToWait > 0) return false;
return true;
}
public void ReserveItems()
{
foreach (IReservable i in itemsToReserve)
{
i.reserved = true;
}
}
public void UnreserveItems()
{
foreach (IReservable i in itemsToUnreserve)
{
i.reserved = false;
}
}
}
Example Task:
public class T_WorkerGoToBed : Task
{
public FactoryWorker worker { get { return taskEntity as FactoryWorker; } }
public T_WorkerGoToBed(TaskableEntity t)
: base(t) { }
public override bool CanStart()
{
return Room.Available<Bed>(GV.Bedrooms);
}
public override void InitialiseTaskStages()
{
Bed bedToSleepIn = Room.NearestAvailableFurniture<Bed>(GV.Bedrooms, taskEntity.X, taskEntity.Y);
//stage 1 - reserve bed and move there
TaskStage ts1 = new TaskStage(this);
ts1.SetItemsToReserve(bedToSleepIn);
ts1.SetPointsToMoveTo(bedToSleepIn.XY);
//stage 2 - sleep in bed
TaskStage ts2 = new TaskStage(this);
ts2.SetWait((worker.maxEnergy - worker.energy) / worker.energyRegeneratedPerSecondWhenSleeping);
ts2.SetEmotionToSet(new E_Sleeping(worker, false));
//stage 3 - unreserve bed
TaskStage ts3 = new TaskStage(this);
ts3.SetItemsToUnreserve(bedToSleepIn);
ts3.SetEmotionToSet(new E_Happy(worker, false));
SetTaskStages(ts1, ts2, ts3);
}
}
It sounds like you need to reverse responsibility between task and worker. Instead of asking whether the task can be performed, ask the worker if he can perform a given task:
class Worker
{
bool CanPerformTask<T>() where T : Task
{
var type = typeof(T);
// code to determine whether worker can perform the task T
}
// alternative with instance parameter
bool CanPerformTask<T>( T task ) where T : Task
{
// code to determine whether worker can perform the task passed in
}
}
This solution avoids the "instantiate all tasks or call static method" problem.
Also, consider using the built-in collection classes. Things such as queues and stacks can greatly simplify the code needed to schedule execution of things.
I think you are abusing the point of static classes. The "Task" class should be standard (not static). Your "Worker" class is not static therefore implying that there is more than one "Worker" instance. Given this paradigm, these workers can probably have the same task assigned to them.
Your "Worker" class needs to have this property modified from:
public List possibleTasks;
to
public List _possibleTasks;
You probably should not have public access to this property either. You can modify "CanPerformThisTask" as necessary.