- Home /
Sequential Coroutines sometimes halt
Hello, I've been working on improving my Dissertation Project from University.
This game creates a bunch of rooms, so ive been implementing Coroutines to create the map over time, rather than just in methods executed from the Start method.
Problem is majority of the time, all the map is created; dandy.
However sometimes only a percentage of it is, and its different each time.
Here is my output:
Not Desktop Application
MapModeManager.start
About to run CoR - createBlankMap
Creating Blank Room: 0, 0
Creating Blank Room: 0, 1
...
Creating Blank Room: 8, 9
Finished creating Blank Rooms
About to run CoR - createMap
About to read from File :
/storage/emulated/0/Android/data/com.IFaBb.ModularZombieSurvival/files/SaveFiles/Blarg.txt
Not new File (skipping dims)
Loop Spawn Rooms; 9, 10
roomScript.roomConfig(DifferentRooms[0], ID:0, Rot:0, X:0, Z:0);
Finished Configuring Room: 0, 0
roomScript.roomConfig(DifferentRooms[0], ID:0, Rot:0, X:0, Z:1);
...
Finished Configuring Room: 6, 0
roomScript.roomConfig(DifferentRooms[0], ID:0, Rot:0, X:6, Z:1);
Finished Configuring Room: 6, 1
roomScript.roomConfig(DifferentRooms[0], ID:0, Rot:0, X:6, Z:2);
End of Output
So the problem here, is that all the Blank Root rooms are created, but the program stopped at Room 6,1 when giving each the appropriate room.
I either need advice on how to sort out the code concept below, which is to spawn all the roots, then the rooms, and then config them. AND/OR A way to do sequential CoR excutions, that each one is able to perform 2 for loops, and stop itself and then resume at the end of each, ie:
for(){ for(){
//do stuff
} //jump out for now (yield) and resume next time CoR runs }
CODE (unimportant content removed)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class MapModeManager : MonoBehaviour {
//Variables
//==============================================================================================================
/*Use this for initialization
*/
void Start () {
MyDebug.log("MapModeManager.start");
StartCoroutine( coStart() );
}//Start
//==============================================================================================================
IEnumerator coStart(){
MyDebug.log("About to run CoR - createBlankMap");
yield return StartCoroutine( createBlankMap() );
MyDebug.log("About to run CoR - createMap");
yield return StartCoroutine( createMap() );
MyDebug.log("About to run CoR - activateNMs");
yield return StartCoroutine( activateNMs() );
if( !SaveMemory.isEditMode() ){
MyDebug.log("Activating AI Manager");
gameObject.transform.GetComponent<AI_Manager>().enabled = true;//enable AI script
gameObject.transform.GetComponent<AI_Manager>().activate();//run startup stuff
}else{
MyDebug.log("Not, Activating AI Manager");
}
yield return null;
}
//==============================================================================================================
IEnumerator createBlankMap(){
for(int x=0; x<roomsX; x++)
{
for(int z=0; z<roomsZ; z++)
{
MyDebug.log("Creating Blank Room: "+x+", "+z);
if( startingTemplateRoom == null ){
MyDebug.log("StartingTemplateRoom == Null");
}else{
Debug.Log("StartingTemplateRoom, Exists");
}
Rooms[x,z] = Instantiate( startingTemplateRoom,
new Vector3( (float)x*spawnRoomSize,0f, (float)z*spawnRoomSize ) ,
Quaternion.identity ) as GameObject;
}//for
yield return null;
}//for
MyDebug.log("Finished creating Blank Rooms");
yield return null;
}//IE createBlankMap
//==============================================================================================================
IEnumerator createMap(){
//set Room Size
Room roomScript;
int roomID, roomRot;
MyDebug.log("About to read from File :");
MyDebug.log(SaveMemory.getDesiredFile(true) );
using( StreamReader sr = new StreamReader( SaveMemory.getDesiredFile(true) ) ){
MyDebug.log("Loop Spawn Rooms; "+roomsX+", "+roomsZ);
//initializes Rooms
for(int x=0; x<roomsX; x++){
for(int z=0; z<roomsZ; z++){
//------
roomScript = Rooms[x,z].GetComponent<Room>();
if( !SaveMemory.isNewFile() ){
string Temp = (sr.ReadLine()).ToString();
//Debug.Log("First Reading as String is :"+Temp);
roomID = int.Parse( Temp );
Temp = (sr.ReadLine()).ToString();
//Debug.Log("First Reading as String is :"+Temp);
roomRot= int.Parse( Temp );
MyDebug.log("roomScript.roomConfig(DifferentRooms["+roomID+"], ID:"+roomID+", Rot:"+roomRot+", X:"+x+", Z:"+z+");");
roomScript.roomConfig(DifferentRooms[roomID],roomID,roomRot, x, z);
}else{
MyDebug.log( "Creating Basic Room" );
roomScript.roomConfig(DifferentRooms[0],0,0, x, z);
}
}//for
yield return null;
//Debug.Log("Row :"+x);
}//for
}//using
MyDebug.log("CoR createMap, Finished");
finishedSpawning = true;
yield return null;
}//IE createMap
//==============================================================================================================
IEnumerator activateNMs(){
bool activateFinished = false;
for(int x=0; x<roomsX; x++)
{
for(int z=0; z<roomsZ; z++)
{
activateFinished = Rooms[x,z].GetComponent<Room>().activateNM();
}
yield return null;
}//for
MyDebug.log("About to Spawn Player Model");
if( editMode ){
if( DesktopApplication ){
Instantiate( EditorPlayer, spawnPlayerPos.transform.position, Quaternion.identity);
}else
{
Instantiate( EditorPlayerMobile, spawnPlayerPos.transform.position, Quaternion.identity);
}
}else{
if( DesktopApplication ){
Instantiate( CombatPlayer, spawnPlayerPos.transform.position, Quaternion.identity);
}else
{
Instantiate( CombatPlayerMobile, spawnPlayerPos.transform.position, Quaternion.identity);
}
}//editMode
Destroy(tempCamera);
MyDebug.log("Have spawned Player");
yield return null;
}//activateNMs
//==============================================================================================================
// Update is called once per frame
void Update () {
}//Update
}//CLASS
CODE Ends
Any and all help is welcome, and I will answer any and all questions if there are any.
Sadly for folks with complex questions, it's unlikely you'll get any assistance when it involves debugging ~200 lines of code.
Besides which, I only halfway understand the issue: A chain of related co-routines sometimes fails to complete execution, and you haven't been able to find the cause. Is this correct?
I don't suppose you require advice about debugging or a link to co-routine documentation, but that's all anyone is likely to offer when the problem scope is difficult to communicate.
Unless each stage of the process requires that sibling objects have undergone the previous stage, then nothing is gained by splitting the process into separate stages.
In other words, could you reduce complexity by perfor$$anonymous$$g all N stages on one object at a time, rather than Stage1 on N objects, Stage2 on N objects, etc? Neither approach is superior if they're interchangeable, but perhaps you'll spot your issue while refactoring.
All co-routine functionality is exposed, so it's not as though there are any secrets. Based on the description of your desired behavior, it's possible your approach could be reworked to reduce complexity while still leveraging the benefits of co-routines.
Thanks Sunny, thing have been working strangely with an error sometimes not showing the next time.
I have this one error which is when I try to run a method on a referenced script I obtain through GetComponent, but for some reason it just doesn't exist.
I had a similar issue today where converting a bunch of nested code into Coroutines would create only half of it be executed and no error messages showing up. Really really weird.
Answer by Yinoguns · Aug 29, 2014 at 02:15 PM
Ok im going to put a closing Answer on this.
Firstly CoR are a bit odd at first I thought they were exiting early, but turns out I missed an error I had.
I have a method called "GetScripts" which as the name says, gets the PathNode and NodeManager scripts for the room.
I hadn't worked with all the code for sometime and had been working on other areas, I didnt realise that GetScripts was restricting its operation depending on gamemode.
From this I wont be forgetting or making the mistakes I had made when making the game, so no worries.
public void getScripts(){
if( !SaveMenory.isEditMode ){
Debug.Log("Getting Scripts");
if( theRoom.GetComponent<PathNode>() != null ){
PathingNode = theRoom.GetComponent<PathNode>();
//Debug.Log("Room.Grabbing PathNode");
}
if(PathingNode == null){
MyDebug.log("Room ["+roomX+","+roomZ+"] PathNode is still NULL!");
}
if( theRoom.GetComponent<NodeManager>() != null ){
NodeMan = theRoom.GetComponent<NodeManager>();
//Debug.Log("Room.Grabbing NodeManager");
}
if(NodeMan == null){
MyDebug.log("Room ["+roomX+","+roomZ+"] NodeMan is still NULL!");
}
}else{
//Debug.Log("EditMode; not grabbing Scripts");
BroadcastMessage("disablePlayModeAspects",SendMessageOptions.DontRequireReceiver);
}
}//getScripts
Your answer
![](https://koobas.hobune.stream/wayback/20220613161700im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Make a GameObject changes between 2 colors with a time wait 1 Answer
Loops without coroutine [SOLVED] 3 Answers
How to make loop with call to IEnumerator actually pause? 2 Answers
Coroutine not running 1 Answer
Can I use WWW in Start()? 1 Answer