- Home /
C# Custom (slower) Update function?
I have a top down 2-d game, that generates new chunks as needed, depending on how close the player is to the current edge of the map. (Pseudo code below)
//Begin Pseudo code
int ChunkSize;
//How far should I look to generate new chunks
int ChunkRadius
//The position of my current chunk
Vector3 CurrentChunk;
//A list of all my chunks
List ChunkPositions = new List();
void PlayerToChunkCoords(Vector3 playerposition)
{
//Convert the players position to a set of chunk coordinates
}
void CheckAndGenerateChunks()
{
//Get the chunk coordinates
//use for loops and the radius to check if all of the chunks in range have been generated
//if our list doesnt have a chunk within the radius, generate it and add its coordinates to the list
}
///End pseudo code
This code doesn't need to run every frame, and putting it in fixedupdate (and slowing down fixedupate) gives me a huge gain in fps. But I really want to be able to take it out of fixedupdate, and put it on its own cycle.
Use a Coroutine
http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.StartCoroutine.html
http://docs.unity3d.com/412/Documentation/ScriptReference/index.Coroutines_26_Yield.html
Also, when posting questions/answers/comments with code in them, use the 101/010 button to format it properly. Type/paste your code into your message, then highlight it all and press that button. I did it for you this time.
Answer by supernat · Apr 08, 2014 at 04:59 AM
If you need it to be consistently run at some frequency (and I mean consistent like it's always exactly every 1 second), then put it in FixedUpdate(), but wrap it in a "time to go" test like this:
float timeToGo;
void Start() {
timeToGo = Time.fixedTime + 60.0f;
}
void FixedUpdate() {
if (Time.fixedTime >= timeToGo) {
// Do your thang
timeToGo = Time.fixedTime + 60.0f;
}
}
In this example, it would run your code every 60 seconds.
You can alternatively, though less efficiently, use a coroutine like so:
void Start() {
StartCoroutine(work());
}
IEnumerator work() {
while (true) {
yield return new WaitForSeconds(60.0f);
// Do some work
}
}
This will be a bit less efficient, because you have the overhead of the coroutine mixed in with a few extra cycles to yield for waitforseconds every frame.
Or this also-> http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.InvokeRepeating.html
Or, or... or...
Thanks, that first trick helped alot, I pretty much doubled the amount of chunks I can have (without lag).
Also, if anyone reads this in the future, if you have a big list of things, use a hashtable. At least in this instance the hashtable allowed me to add double the chunks over a list.
@supernat: +1
However it makes no sense to pair FixedUpdate with Time.time since Time.time only changes between Update calls. Either use Time.fixedTime in FixedUpdate or use Time.time in Update.
Answer by spraycanmansam · Apr 08, 2014 at 10:54 AM
You could also call InvokeRepeating("CheckAndGenerateChunks", 0, x) in your Start() function to call your code every x seconds.
Good answer, probably the cleanest way to get the results. I knew there was a method to do this but couldn't remember the name of it to save my life.
Answer by Chrisdbhr · Aug 28, 2019 at 05:04 PM
Here is a simpler approach, using only one variable (the code inside Update will run every 3 frames in this example.):
private int updateInterval = 3; // in frames
void Update(){
if (Time.frameCount % this.updateInterval != 0) return;
// your code here...
}
With this, you use only you extra variable on your code, and put just one extra line on your Update() loop.
The other reason that this is better than initializing Coroutines on Start() is because when you do this and your game object gets disabled and enabled again, you Coroutine will not run. Of course, you could initialize it OnEnable(), but it's better to just use 2 extra lines with only one new variable than allocate memory and processing time with coroutines.
Answer by Statement · Apr 08, 2014 at 11:21 AM
See this method of achieving a companion Update that you can call as often as you want.
While you may not be interested in being able to perform functions step-by-step you could modify CoStart to call your custom Update function every x seconds or every x events.
Answer by MoruganKodi · Nov 01, 2016 at 08:34 AM
You can have a constant update and chunk generation entirely using coroutines:
void Awake()
{
StartCoroutine(ConstantUpdate);
}
IEnumerator ConstantUpdate()
{
while(true)
{
// come code
if(m_IsGameRunning)
{
// wait for chunks to update
yield return UpdateChunks();
}
yield return null;
}
}
IEnumerator UpdateChunks()
{
for(int i = 0; i < chunks.Count; i++)
{
// stuff
yield return null;
}
}