Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SpaceSocks · May 05, 2014 at 04:56 PM · loopfile-io

Screen doesnt refresh until after loop

I have some issues.. (well allot really because I'm a noob) but.

In this script i am creating / adding text line by line to a text file. and its a big loop im trying to figure out why it wont display my progress bar each time it goes thru the loop. it waits to show the scene until after the script finishes. how do i get it to show the scene and refresh it each time i go through the loop?

I'm not sure which part of the code is the problem so I'll just past the whole script(sorry its a mess)

another huge problem is that it takes forever to go through the loop. i think its because of the way im writing the file?

Also I have another issue and that is for some reason the file its outputting "world.dat" is only currently writing the last x,y pos. it was working but i screwed it up some how. the file just contains:

 10,10
 10,10

also is there a way to run my game/code one line at a time so i can watch the code in the script as it goes?

to help make sense of this madness here is how its run: first it goes to Start() and runs the CheckForFile method to see if the file and directory exists to store the data to.

then it goes to the CreateWorld method which basicly contians my main loop

once it gets there it calls TileSelector method which is where i will figure out which tile to store in the file.

the it goes to FileIO method this is where i write the current x,y pos and the tile type.

then it calls the ProgressBar method to display the overall progress of the file. here is a picture of what that looks like

link text

then in the CreateWorld method if checks to see if the file is done writing yet based on current x,y. if it is it ends the program at the DoneCreatingFile method.

anyways, here is the script.

 using UnityEngine;
 using System.Collections;
 using System;
 using System.IO;
 using System.Text;
 
 public class WorldGen : MonoBehaviour {
 public GameObject ProgBar = null; //Progress Bar Prefab
 static string fileNameCheck = "World.dat"; // name of file to store the tilemap & other varibles on each tile
 static string dirCheck = Application.dataPath + "/Data/"; // directory where to store data file
 static string pathCheck = dirCheck + fileNameCheck; // location of data file
 static uint currentXpos = 1; 
 static uint currentYpos = 1;
 public static float xSpawnSpot = -3.098634f; //starting spawn location of prog bar in scene
 public uint worldWidth = 10; // x value
 public static uint worldHeight = 10; //y value
 static float progBarCount = (worldHeight * 0.037f); // current value of total progress complete
 
 //density floats represent precentages. ex: 0.0f = 0 percent chance of that tile type to spawn. 100f = 100 percent
 public float grassDensity = 75f; 
 public float dirtDensity = 30f;
 public float waterDensity = 20f;
 public float snowDensity = 20f;
 public float snowMountainDensity = 12f;
 public float caveDensity = 3f;
 
 private string tileTypeName = ""; //name of current tile
 private int tileTypeNum = 0;  
 /* 0 = nothing
    1 = grass
    2 = dirt
    3 = water
    4 = snow
    5 = snow mountain
    6 = player house
    7 = cave
 */
 
     // Use this for initialization
     void Start () {
 
         this.CheckForFile (); //check to see if file & directory exists
         this.CreateWorld ();
     }    
 
     private void CreateWorld() { // loop that generates each tile                            
 
         this.TileSelector();
         this.FileIO();
         this.ProgressBar();    
 
         if (true & currentXpos == worldWidth && currentYpos < worldHeight)// add one to Y if its not maxed height
         {
             currentYpos += 1;
             currentXpos = 0;
         }
         if (currentXpos < worldWidth)// add one to X if not maxed width
         {
             currentXpos += 1;
         }    
         if (true & currentXpos == worldWidth && currentYpos == worldHeight)// if both meet max then finish file
         {
             //run these one last time then end file creation.
             this.TileSelector();
             this.FileIO();
             this.ProgressBar();    
             DoneCreatingFile();
         }
         else
         {
             this.CreateWorld(); // not done yet, start loop over again.
         }
     }        
 
 
 
         public static void CheckForFile() {
         if (Directory.Exists(dirCheck)) //check for dir first
         {
             Console.WriteLine("That path exists already.");            
         }
             else
             {
             // Try to create the directory.
             DirectoryInfo di = Directory.CreateDirectory(dirCheck);
             }
         if (!File.Exists (pathCheck)) 
         {   // If file does not exist then create one
             // Create a file to write to. 
             string createText = "Game World Data File." + Environment.NewLine;
 
             File.WriteAllText (pathCheck, createText);
         }
             else
             {
             File.Delete(pathCheck);
 
             }
                         
         }
 
         //Progress bar fill
         void ProgressBar() {
 
         if (currentYpos >= progBarCount) 
         {            
             ProgBar = Instantiate(Resources.Load ("Prefabs/BarPiece"),new Vector3(xSpawnSpot,0.2693692f,3f),Quaternion.identity) as GameObject;    
             progBarCount += (worldHeight * 0.037f);
             xSpawnSpot += 0.246417f;
         }
             UnityEngine.Debug.Log ("Progress Bar Location: " + progBarCount);
         }
         
         void Update() {
 
         }
 
 
         void TileSelector (){ // figures out which tile type to generate at current location based on density percent set by that varible
             UnityEngine.Debug.Log ("Tile Selector");
         }
 
         void FileIO() // write each x,y location to file World.dat
         {    
             UnityEngine.Debug.Log ("y=" + currentYpos + "; x=" + currentXpos);
             // write first lines to file
             if (currentYpos == 1 && currentXpos == 1) 
             {
                 string firstLineText = "Map tileset data file." + Environment.NewLine;
                 string lineBrakeText = "******************************************" + Environment.NewLine;
                 File.WriteAllText(pathCheck, firstLineText + lineBrakeText);
             }
             //write each x,y to file
             File.AppendAllText(pathCheck, currentYpos + "," + currentXpos + Environment.NewLine);
         
             // Open the file to read from. 
             //string readText = File.ReadAllText(pathCheck);
             //Console.WriteLine(readText);                
         }
 
         void DoneCreatingFile() {
             Application.Quit (); //end program
         }
 }


Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NoseKills · May 05, 2014 at 05:37 PM 0
Share

CreateWorld method which basicly contians my main loop

Do you mean there's a for-loop or a while-loop or such that draws the bar?

avatar image SpaceSocks · May 05, 2014 at 05:43 PM 0
Share

no inside the CreateWorld method it calls each method and then after it gets done it gets called again. so it just 'loops' until currentXPos and currentYPos = worldWidth and WorldHeight then once it does it ends. and it dosent start the scene until it finishes for some reason.

so when i click the play button nothing shows up on the screen until its done with the loop. so as soon as the game view and scene view start my progress bar just goes from 0 to 100 lol..

avatar image SpaceSocks · May 05, 2014 at 07:59 PM 0
Share

Ok I tired the Coroutine. And it isn't working. I'm not sure why.

I also put my prefab Instantiate right in the Update() just to see if it would draw. and it still doesn't! it waits till the loop is done.. wtf.. ive spent way to much time on this :(

I put it in my Progress Bar method.. thinking as soon as its done drawing it would goto update and display it.. but no

I did fix the FileIO() problem though.

here is the code for my Coroutine

 //Progress bar fill
         IEnumerator ProgressBar() {
 
         //progress bar
         if (currentYpos >= progBarCount) 
         {    
             if (progBarPieces < 27)
             {
                 ProgBar = Instantiate(Resources.Load ("Prefabs/BarPiece"),new Vector3(xSpawnSpot,0.2693692f,3f),Quaternion.identity) as GameObject;
                 UnityEngine.Debug.Log("Bar Draw");
             }    
             if (progBarPieces == 27)
             {
                 ProgBar = Instantiate(Resources.Load ("Prefabs/LastBarPiece"),new Vector3(xSpawnSpot,0.2693692f,3f),Quaternion.identity) as GameObject;
                 UnityEngine.Debug.Log("Bar Draw");
             }
             progBarCount += (worldHeight * 0.037f);
             progBarPieces += 1;
             if (progBarPieces < 27)
             {
                 xSpawnSpot += 0.246417f;
             }else 
             {
                 xSpawnSpot += 0.1232085f;
             }
             
         }
         UnityEngine.Debug.Log ("Progress Bar Location: " + progBarCount);        
         yield return null;    
     }
avatar image SpaceSocks · May 05, 2014 at 08:01 PM 0
Share

also how do i call the Render?

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Jeff-Kesselman · May 05, 2014 at 05:43 PM

Unity is a game engine.

Game engines run in an Update/Render loop where each frame Update is called, followed by Render. Render is what changes the screen contents.

Nothing will change on screen until after your Update returns and your Render gets called.

Start is actually called even before your first Update so again,. nothing gets drawn until Update is called and then Render is called.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by xortrox · May 05, 2014 at 05:46 PM

You should start CreateWorld as a Coroutine so that the game can continue while this heavy operation takes place. If CheckForFile is another heavy operation use coroutines for that as well. For more info on coroutines see this.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by SpaceSocks · May 05, 2014 at 08:10 PM

Ok I tired the Coroutine. And it isn't working. I'm not sure why.

I also put my prefab Instantiate right in the Update() just to see if it would draw. and it still doesn't! it waits till the loop is done.. wtf.. ive spent way to much time on this :(

I put it in my Progress Bar method.. thinking as soon as its done drawing it would goto update and display it.. but no

I did fix the FileIO() problem though.

here is the code for my Coroutine

 //Progress bar fill
         IEnumerator ProgressBar() {
 
         //progress bar
         if (currentYpos >= progBarCount) 
         {    
             if (progBarPieces < 27)
             {
                 ProgBar = Instantiate(Resources.Load ("Prefabs/BarPiece"),new Vector3(xSpawnSpot,0.2693692f,3f),Quaternion.identity) as GameObject;
                 UnityEngine.Debug.Log("Bar Draw");
             }    
             if (progBarPieces == 27)
             {
                 ProgBar = Instantiate(Resources.Load ("Prefabs/LastBarPiece"),new Vector3(xSpawnSpot,0.2693692f,3f),Quaternion.identity) as GameObject;
                 UnityEngine.Debug.Log("Bar Draw");
             }
             progBarCount += (worldHeight * 0.037f);
             progBarPieces += 1;
             if (progBarPieces < 27)
             {
                 xSpawnSpot += 0.246417f;
             }else 
             {
                 xSpawnSpot += 0.1232085f;
             }
             
         }
         UnityEngine.Debug.Log ("Progress Bar Location: " + progBarCount);        
         yield return null;    
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

while loop not looping 2 Answers

Help with simple scripting? 1 Answer

Endless Terrain "Loop" 1 Answer

c# loop why 2 Answers

Making an animation stop once it finishes (trouble with booleans) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges