- Home /
make Update display/animate iterations of While(i<20)loop?
it's a digger function that digs tunnels in space when i press z button.
its in a while loop so if i want to dig 20 spaces, i do the while loop 20 times. this freezes unity for a moment, and then shows all 20 spaces have been dug.
I would like to see the tunnel advancing as fast as the processor can dig, i dont mind scrapping the while loop for something else, or to include a command "RefreshDisplay" in the while loop, something like that? thanks for being so amazingly helpful guys i am learning so much ;)
function Update()
{
if ( Input.GetKeyDown("z") )
{
d=0;
while (d < digSize)
{
DigRandomTunnel();
d=d+1; }
}
}
Answer by UGTools · Oct 15, 2012 at 03:09 PM
You can try something like this:
function Update()
{
if(Input.GetKeyDown("z"))
{
digsRemaining += digsPerPressedZ;
if(digsRemaining > digsMax)
{
digsRemaining = digsMax;
}
}
if(digsRemaining > 0)
{
DigRandomTunnel();
digsRemaining = digsRemaining - 1;
}
}
This way each time the key is pressed you only increase the counter (with an optional limit). Each frame DigRandomTunnel() will be called only once so that the screen is updated as quickly as possible and the process doesn't get stalled. This will happen until there is nothing left to dig (digsRemaining will be 0).
Answer by ByteSheep · Oct 15, 2012 at 02:44 PM
It would be helpful to know what you are doing in the DigRandomTunnel() function, but I think that a while loop is not what you would need in this situation. The while loop will try to execute the 20 spaces in one single frame..
How are you specifying digSize's value though? Does the user input the value? Or would you like the digSize to vary depending on the duration the user presses the z key?
If you could give us the code in your DigRandomTunnel() function it would be easier to how exactly you're making the blocks disappear in the game.. This code is assuming you are using an animation and that you would like the amount of spaces dug to depend upon the duration of the keypress:
function Update() { if ( Input.GetKeyDown("z") ) { DigRandomTunnel(); //call DigRandomTunnel() once every frame { }
function DigRandomTunnel() { digSize++; //increase digSize each time we remove a block yield WaitForSeconds(0.2); //wait for 0.2 sec before next block is removed
animation.Play(); //play animation or whatever it is you are doing here }
This is just a guess at what the code might look like, however to be able to actually give a proper answer we would have to see some more of your code :)
Calling a new coroutine each frame is not a good idea. New coroutines will be stacked upon each other.
Using coroutine is a good idea (the best in my opinion), but you need to "protect" the call, for example by using a boolean:
var digging = false;
function Update()
{
if (!digging && Input.Get$$anonymous$$eyDown("z"))
{
DigRandomTunnel();
}
}
function DigRandomTunnel()
{
digging = true;
for(var i : int = 0; i < 20; i++)
{
// code: "digging"
yield; // wait until next frame
}
digging = false;
}
thanks alot!!! i will attempt to figure out a solution from these options. here is a demo of the digging code. my first 3d program :) http://www.youtube.com/watch?v=$$anonymous$$HpIb6iDv$$anonymous$$Q&feature=youtu.be
Your answer
![](https://koobas.hobune.stream/wayback/20220613082351im_/https://answers.unity.com/themes/thub/images/avi.jpg)