1
\$\begingroup\$

My code:

public class CharacterBehaviour: MonoBehaviour {

bool isExecuting=false;
Action action;

void Start{
action=new Action();
}

void Update{
//Other methods
if(!isExecuting)
{
StartCoroutine(action.execute(out isExecuting));
}
}    

public class Action: MonoBehaviour
{
IEnumerator execute(out isExecuting)
{
return YieldReturnNull)();

//Execution

isExecuting=false;
}


public IEnumerator YieldReturnNull()
{
yield return null;
}
}

I am using IEnumerator to start a coroutine right? But I need this out value to do not start a lot of coroutines in Update, just when it finalizes I must start another. The problem it's that I need to return of yield of course in the beggining (The YieldReturnNull method is a trick because IEnumerator does not accept out or ref as parameter in a method returning yield) but it returns for me this error:

The out parameter `isExecuting' must be assigned to before control leaves the current method.

How to solve it? I need to return yield in the beggining and apply the bool in the end. Thanks. :)

\$\endgroup\$
4
  • \$\begingroup\$ Maybe you can describe what you're trying to accomplish on a higher level. You may have made some incorrect assumptions about what you actually need to do to achieve your goal. \$\endgroup\$
    – House
    Commented Aug 4, 2016 at 18:48
  • \$\begingroup\$ It's basically it. I have some methods in the Update running and these actions that are in the real code not just an class but implementations of Action interface, so I need this action being executed without interfering in update (coroutine) and a kind of pile of actions that can be manipulated by main code. Pile of RPG moviments or actions that can be canceled modified etc, and one action being executed by time that don't interfere the current execution. If you do not understand, I can send a flowchart. \$\endgroup\$
    – JamesB
    Commented Aug 4, 2016 at 18:56
  • 1
    \$\begingroup\$ A note: you should not name your custom class "Action", there is Action in .NET which is a void delegate - you might run into annoying name collisions later. \$\endgroup\$
    – wondra
    Commented Aug 4, 2016 at 19:08
  • \$\begingroup\$ @wondra Thanks, I didn't know. In this case luckily the original is in portuguese. But I will not forget it. Thanks. ;) \$\endgroup\$
    – JamesB
    Commented Aug 4, 2016 at 19:15

1 Answer 1

2
\$\begingroup\$

Why not store the isExecuting variable within the action?

public class CharacterBehaviour: MonoBehaviour {
    Action action;

    void Start{
        action=new Action();
    }

    void Update{
        //Other methods
        if(!action.isExecuting)
        {
            StartCoroutine(action.execute());
        }
    }    

    public class Action: MonoBehaviour
    {
        public bool isExecuting = false;
        IEnumerator execute()
        {
            isExecuting = true;
            yield return null;
            //Execution
            isExecuting=false;
        }
}

If you also need to access isExecuting from the CharacterBehaviour you can provide a property for that.

public isExecuting {
    get
    {
        return action.isExecuting;
    }
}
\$\endgroup\$
6
  • \$\begingroup\$ It does not work in my case because there is a list of actions not just one. It should work if it was one action and just one instance, but I have a pile of actions to be executed in a for loop protected by the 'ifExecuting bool' so I need to wait one routine to end to start another routine with another action instance do you know? \$\endgroup\$
    – JamesB
    Commented Aug 4, 2016 at 19:37
  • \$\begingroup\$ List <Action>actions =....; bool isExecuting=false; bool actionsChanged=false; Update() { actions = MethodThatCanChangeTheActions(actions, out actionsChanged); if(actionsChanged){numberOfActionInExecution=0} if(!isExecuting){ startCoroutine(action[numberOfActionInExecution].execute(out isExecuting); numberOfActionInExecution++; } } \$\endgroup\$
    – JamesB
    Commented Aug 4, 2016 at 19:38
  • \$\begingroup\$ In that case, you're just watching the "current action". i.e. if(!action[numberOfActionInExecution].isExecuting) \$\endgroup\$
    – House
    Commented Aug 4, 2016 at 19:44
  • \$\begingroup\$ ♦ I made a simplification for the forum and you solve the simplification, as you are expert in this forum how we should do I accept your answer and open a new topic, or post new example as an answer? \$\endgroup\$
    – JamesB
    Commented Aug 4, 2016 at 19:45
  • \$\begingroup\$ Ok but now looking the code I noticed that I am nothing doing this way anymore sorry it was not used anymore because it implies other erros I'm using it: StartCoroutine(executeAction(actionsForTheCharacter[0])); actionsForTheCharacter.Remove(actionsForTheCharacter[0]); Always the 0 index and after it throws it to the routine it was destroyed so I need a type of bool or other method outside the instantiation to mark that it is in execution. \$\endgroup\$
    – JamesB
    Commented Aug 4, 2016 at 19:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .