Network.Instantiate in a thread

Hi everyone!

I am making a FPS game where my bulletholes are instantiated throughout the network… It works perfect! When firing my shotgun however, it freezes up a bit. Not major, but I want to try and get it out. I tried putting it in a thread, with little success (I don’t think it is thread safe) is there a way that I can Network.Instantiate outside of the main thread? Or atleast run the process async?

Thanks! :smile:

Nope.

KelsoMRK,
Are you 100% sure? I have read in many places that a CoRoutine might be used. Another thing, why can a level be loaded async? Isn’t that too the instantiation of objects?

I got it working after a bit of playing around. I am using coroutines, in c# it is IEnumerable (for anyone that might need it at some point)

I needed the following things:

A Struc
internal struct BulletPos
{
internal Vector3 pos;
internal Quaternion rot;
}

A List
List BulletholesToAdd;

*Note that it is a list of my struct.

A IEnumerator function
public IEnumerator AddBulletholes()
{
for(int i=0; i<BulletholesToAdd.Count; i++)
{
Network.Instantiate(Weapons.WeaponsBulletholes[WeaponID-1],BulletholesToAdd[0].pos,BulletholesToAdd[0].rot,0);
BulletholesToAdd.RemoveAt(0);
yield return new WaitForSeconds(0.001f);
}
}

Now you add to the list as soon as a bullethole is supposed to appear

Calling the function
if(BulletholesToAdd.Count>0)
{
StartCoroutine(AddBulletholes());
}

As easy as that, now the network instantiate and normal instantiate is pretty much the same speed.

*Edit:
Firing 999 bullets simultaneously on a local machine I got sped up from 16.7 seconds to 5.6 seconds…

yield return new WaitForSeconds(0.001f);

your probably better off using:

yield return null;

it simply waits till the next frame to continue execution

Yes, I’m sure. Coroutines still run in the main thread - they simply run over multiple frames. An async level load is done explicitly via the Unity API (and I’m not even sure that it runs in a separate thread - I’ve never bothered to look). You can’t do anything that affects the game world outside the main thread (ie creating objects, manipulating transforms, etc).

@jschieck
Great that took off another 0.5 seconds on the same test. (999 bulletholes simultaneously) It is now faster than normal instantiation! I don’t think I can expect much more than that…

@KelsoMRK
Thanks for the info. I do however recommend if you are ever in a situation where you need thread-like interaction to unity engine functions, consider using coroutines.

I was just being clear that Coroutines are still running in a single thread and therefore you don’t have to worry about thread safety or queuing/marshaling results back to the main thread. All you’re doing is spreading execution out across multiple frames. Folks have been confused about this in the past, that’s all. :slight_smile:

If you do want to use threads, you could have a look at this: http://unitygems.com/threads/
Looks quiet interesting and simple… haven’t tried it though.