0

i'm a beginner programmer. And i want to create an Objectspawner sort of like a middleware that Process Pathfinding script in a multi threads then pass it back to Gameobject to execute. Basically each object that call for pathfinding, the calculation process will be handle by different threads. what should i learn to create this ?

Aki Jirou
  • 29
  • 2
  • 13
  • This is not a good idea to use 1 thread per object. I don't know how Unit handles threads, but creating threads cost a lot of time and it would be better for you to start less threads and process multiple paths on a single thread. – FCin May 14 '18 at 07:08
  • yeah, i just realize that. i think i use the wrong expression. – Aki Jirou May 14 '18 at 07:20
  • You can make use of a new Job System in Unity. I'm not sure if it is out yet, but if so, this would greatly simplify your task. – FCin May 14 '18 at 07:41
  • What is the main reason to multi-thread? There should be a couple of alternatives to multi-threading which may apply. – Koshux May 14 '18 at 14:01

2 Answers2

1

This is possible. You can use Unity's structures and math API's like Vector3, Vector2 and Mathf in another Thread.

Get the position of the Object, store it in a Vector3 or Vector2 then start a new thread and pass that Vector to it. You can do all your pathfinding work in your new Thread.

Whenever you need to update the Object's position or use that modified Vector from the new Thread, use the UnityThread.executeInUpdate function from my other post to do this. This function allows you to call Unity API from another Thread. You can make yours if you don't want to use mine or you can read how it is made from that post then make your own from it.

Programmer
  • 121,791
  • 22
  • 236
  • 328
0

While its possible to start seperate threads in Unity, its own API does not support calls from any other thread that its main thread, in which it executes your scripts. Instancing an object is the last thing you want to do while not in the main thread. Why do you think you need threads?

zambari
  • 4,797
  • 1
  • 12
  • 22
  • yes. i know that Unity API doesn't support multithreads. it's part of my assigment to compare single and multi threading. It's like a program that process AI scipt in multi threads and pass it to NPC object to execute. Is it possible to process the script behaviour in threads and then instantiate object in main thread ? – Aki Jirou May 14 '18 at 07:19
  • it is possible but required handling of race conditions between threads. If you app just needs to run once to pass than you can just store a list in the main thread, but keep in mind ugly things might happen when you start accesing modifying the list from multiple threads – zambari May 14 '18 at 13:26