- Home /
Two In-game modes, one doesnt spawn all Rooms
Starting Background Info
Hello, firstly I want to say that my project here is not small or "simple" it was my Dissertation for my University Degree.
The skinny of it is there are two modes, editing and playing.
The Former has you alter the instance and rotation of the rooms that make a grid based map.
The Latter is to play this map, all the perimeter rooms create adjacent rooms to spawn NPC Zombies that come to eat you, "nom nom nom".
Image Example
Below is an image of the game, separate rooms with their own Navigation Graphs that auto-link with one-another to create one complete graph.
The Working
As mentioned before, I have a mode for editing these rooms so you can swap a room from ie a pillar to a hall-way wall design; Now in that mode all works fine and the rooms are all created.
The Not-Working
However for some reason in the Play Mode, which is the same scene but with slightly different path through the same script
When running in the Unity-Editor, the game works fine however in a build PC and Mobile-Android tested, only the first 2 rooms are created.
Problem Idea
One idea I have for my problem is in PlayMode the game has a some more to do, that is the created rooms have to load and built their navigation graph. However this mode creates only 2/70 rooms while edit mode manages to instantiate all 70.
One thought I have is to create a Coroutine system to do this process overtime, just like an Asynchronous Task in Java Android development, but I am unsure if that would help at all considering the above mention of 2/70 vs 70.
Code for your consideration
MapModeManager.Start
void Start () {
editMode = SaveMemory.isEditMode();//get Mode
Debug.Log("Edit Mode: "+editMode);
//Get SaveMemory to find Dims of save file
roomsX = SaveMemory.getDim(0); roomsZ = SaveMemory.getDim(1);//get Dimensions
MyDebug.log("Edit Mode: "+editMode);
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);
//set Room Size
Rooms = new GameObject[roomsX,roomsZ];
Room roomScript;
int roomID, roomRot;
MyDebug.log("About to read from File :");
MyDebug.log(SaveMemory.getDesiredFile(true) );
try{
using( StreamReader sr = new StreamReader( SaveMemory.getDesiredFile(true) ) ){
if( !SaveMemory.isNewFile() ){
MyDebug.log("Not new File (skipping dims)");
Debug.Log("Going to read file: "+SaveMemory.getDesiredFile(true) );
sr.ReadLine();sr.ReadLine();//skip Dimensions
}else{
MyDebug.log("New File");
Debug.Log("Should try to Read File");
}
MyDebug.log("Loop Spawn Rooms; "+roomsX+", "+roomsZ);
//initializes Rooms
for(int x=0; x<roomsX; x++){
for(int z=0; z<roomsZ; z++){
//------
MyDebug.log("Spawning Room: "+x+", "+z);
Rooms[x,z] = Instantiate( startingTemplateRoom, new Vector3( (float)x*spawnRoomSize, 0f, (float)z*spawnRoomSize ) , Quaternion.identity ) as GameObject;
roomScript = Rooms[x,z].GetComponent<Room>();
//if edit false
//get info to load room
//RoomGO,RoomID,RoomRot,X,Z
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("Create Basic Room");
roomScript.roomConfig(DifferentRooms[0],0,0, x, z);
}
}//for
//Debug.Log("Row :"+x);
}//for
}//using
}catch(IOException e){
MyDebug.log("Couldnt Read File");
MyDebug.log("Exception info: "+e.Data );
}
MyDebug.log("RM.Start finished");
finishedSpawning = true;
if( !SaveMemory.isEditMode() ){
gameObject.transform.GetComponent<AI_Manager>().enabled = true;//enable AI script
gameObject.transform.GetComponent<AI_Manager>().activate();//run startup stuff
}
//Debug.Log("SaveMemory.getDesiredFile: "+SaveMemory.getDesiredFile(true) );
//Enable the AI manager
//Rooms[0,0].GetComponent<Room>().getDirection();
}//Start
Room.roomConfig
public void roomConfig(GameObject startingRoom, int roomID, int rotation, int rX, int rZ){
//Debug.Log("Configuring Room: "+roomX+", "+roomZ);
RM = (GameObject.FindWithTag("Manager") ).GetComponent<MapModeManager>();
if( RM == null ){
//Debug.Log("Didnt manage to grab RM");
Destroy(this.gameObject);
}else{
//Debug.Log("Grabbed RM");
}
roomX = rX; roomZ = rZ;
//give starting room (used for loading)
ChangeRoom(startingRoom, roomID);
rotateRoom(rotation);//Error call 2
getScripts();
MyDebug.log("Finished Configuring Room: "+roomX+", "+roomZ);
}//
Debug Output
Edit Mode: False
About to read from File :
/storage/.../SaveFiles/ZombieHall.txt
Not new File (skipping dims)
Loop Spawn Rooms; 7, 10
Spawning Room: 0, 0
roomScript.roomConfig(DifferentRooms[3], ID:3, Rot:0, X:0, Z:0);
Finished Configuring Room: 0, 0
Spawning Room: 0, 1
roomScript.roomConfig(DifferentRooms[3], ID:3, Rot:3, X:0, Z:1);
End of Code/Output
In the coming days, I am likely going to get round to improving the scene bootup with Coroutine, but I wish to get the advice of the community to move this work forward.
Any questions or requests for anything please ask.
UA is not the correct place for "discussion/opinion"-type questions - those are best asked on the forum ins$$anonymous$$d. If you want us to help answer the question of why your build is behaving differently than in the editor we'd need to see your output log file.
Ok sorry about this not being in the right place. And I have given the Output Log, its the last Code Block.
Also you say "why your build is behaving differently than in the editor" I think your mixed up, I have a Play and Edit mode, the Edit makes all rooms, but the Play only creates 2.
These are both when "running" the game in a build PC/Android but no problems exist in the Unity Editor itself.