I cannot find how to do this or if it is even possible,
Can i specify a kind of code snippet that will be inserted into every occurrence of a placeholder at compile time? I cannot use a function because i need those snippets to contain yield statements for use within Coroutines.
Use case: a break for a Coroutine-Statemachine
I have a bool to break a state from anywhere. This is needed because yielded coroutines ( “yield returnStartCoroutine(Coroutine());” ) only return if finished or broken from within (see this post and this post). I need this block multiple times within states and without this every state would be riddled with thos blocks
private IEnumerator StateCombat(){
float meleeFocus = 1F; //1s chargetime for melee attacks
while(true){
if(distance < meleeDist){
//Charge up Melee Attack
float timer = meleeFocus;
while(timer > 0F){
timer -= Time.deltaTime;
<<<WatchForStateBreak>>>
}
StartCoroutine( Main.GenericMeleeAttack());
}else{
Approach();
}
<<<WatchForStateBreak>>>
}
}
CODE-BLOCK-PLACEHOLDER <<<WatchForStateBreak>>>{
yield return new WaitForEndOfFrame();
if(stateBreak){ stateBreak = false; yield break; }
yield return null;
if(stateBreak){ stateBreak = false; yield break; }
}
Code block does the following:
yield return new WaitForEndOfFrame();
//end of current frame, all Coroutines and Update()/LateUpdate() calls that may set stateBreak have finished by now
if(stateBreak){ stateBreak = false; yield break; }
//break State, the new “state” is already set by the function that has set “stateBreak to true”
yield return null;
//continue after next Update()
if(stateBreak){ stateBreak = false; yield break; }
//check again on resume, it may have been that a break has already occurred this frame