"Clean" Scheduler?

Hi all,

I am currently trying to plan for a core component for my app to control scheduling of events. The player shall be able e.g. issue build commands to build huts, factories, etc., and I would like to place the corresponding function calls (CompleteBuilding(…), etc.) together with their parameter and the timecode of their trigger in some kind of list. A scheduler object would then constantly check for items to be triggered and call the appropriate function(s) to be executed.

I was wondering if C#/Unity already features some kind of elegant way to do that, as the best idea I have is a simple stack (as array) and an object which update function simply looks at the top (soonest-to-be-executed) entry of the array and triggers the corresponding function if the current timecode is higher than the saved one and then deletes the entry from the stack. I can’t use Invoke()-functions as I want the entire stack to be saved if the player quits and have the function calls then executed when he logs back in (aka the game time seems to continue to pass for the player). Also, this has to be timecode based, not tick-/or frame-based.

Is there a better way to do that? I also checked the Asset store if somebody maybe already build a nice component for scheduling/executing something like that but couldn’t find anything so far.

Thanks for any help in advance. :slight_smile:

Jan

Sorted list and set the natural order to ascending completion time (by overriding CompareTo)
This way you allways check the first event, and when it was done, you remove it from the list to take the next event in the timeline.

THanks, Atrakeur, that’s what I thought. I am currently trying to find a good documentation online regarding Lists, especially how to use Lists that take an array as entries (e.g. trigger-timecode, object/functions to call, parameters, unique-id, etc.), and then how to sort a list like that. Maybe I am also better off by maintaining two lists, one sorted one that just holds timecode and unique id and then one that has the unique id as key and all the other data as value-array. So, any hints to good documentation are highly appreciated if you guys know any. :slight_smile:

Hm, also, thinking about it … maybe going with a SQLite-table-approach might be worth looking into. Obviously I’d need to figure out how expensive an approach like that would be versus constantly checking a List.

if you do more than of 10 query/sec, the database solution might be too slow for you, especially on low end computers.

Don’t use list with array in it! It just a bad way to program. Just create a class to hold your actions!
This way you can: create one DoAction() method in an abstract Action class, and override it in child class (BuildAction, AttackAction)
So your code’ll look like TakeNextAction().DoAction()

for documentation, what about A Priority Queue in C# - CodeProject

That’s not a List of arrays - that’s a list of a container class that holds all the information you’re talking about.

Thanks Atrakeur again! Before reading this I did a bit of reading on the web and actually tried something like

SortedList<int, int, string> slSchedule = new SortedList<int, int, string>();

before figuring out that this doesn’t work. Totally new to List/SortedList, etc. I then saw that you can have custom/container classes as elements of your list. I thought the SortedList approach would be nice in order to sort based on the first int (timecode of trigger), but I guess it’s then one variable in the custom class. No clue how I do the sorting then, and I also don’t really understand some of what you wrote, but I will read up the link (thanks for that!) and try to fight my way through it. :slight_smile:

Thanks again!

To rephrase and give few more hints on the subject, most (if not all) Sorted collections use natural order for object ordering.
Natural order is defined by Equals and Compare method of those objects.

For example, Compare on string will result in alphabetical ordering.

Object that can be compared together implements the IComparable Interface.
take a look at MSDN on this subject:
http://support.microsoft.com/kb/320727

Ah ok, understood. I guess I get the principle of the article “A Priority Queue in C#”. Looking through the code now, it’s interesting how this is being built up with nodes. I’ll try to translate that into something useful in Unity now. :slight_smile: Thanks so much for the hints, very helpful!

Unity can compile c# so there is nothing to translate.
just put in your project and use it.

Hm, after adding the script to my assets and hooking it up, I got a ton of errors (maybe I forgot to use the right packages or something), but building it up from scratch worked. Seems to be better for learning anyway. :slight_smile: The list, adding and removing nodes, the countdown, the debug-display, that’s all working already. Now figuring out how to do the actual casting of the objects/methods in a clean way.

Thanks so much for your help once again, that really made my day!