Hi, i have a RTS which relies on Update(), followed by a function to allocate how much Construction, Production etc, can be done within the second/minute.
I'm wondering if the time would be better spent using a Co-routine which calls itself every given timeframe(be it 2 seconds or a minute), starting in Start().
var lastResource : int = 0;
var resourceLoop : int = 1;
// lastResource is the last time Update Called AllocateResources();
// resourceLoop controls the delay before AllocateResources() is called next;
function Update ()
{
if (Time.time > lastResource + resourceLoop)
{
AllocateResources();
lastResource = Time.time;
// calls functions to increase an integer that allows buildings to be constructed.
}
}
function AllocateResources()
{
constructionResources += constructionCapacity;
}
Would become :
function Start()
{
PrepareToAllocateResources();
}
function PrepareToAllocateResources()
{
yield WaitForSeconds(resourceLoop);
AllocateResources();
}
function AllocateResources()
{
constructionResources += constructionCapacity;
PrepareToAllocateResources();
}