(Better) Object Pooling - I Didn't Like My Old Solution

  Рет қаралды 14,705

One Wheel Studio

One Wheel Studio

Күн бұрын

Пікірлер: 53
@OneWheelStudio
@OneWheelStudio 2 жыл бұрын
Object Pool Blog Post: onewheelstudio.com/blog/2022/1/17/object-pooling-20 Object Pool Code On Github: github.com/onewheelstudio/Adventures-in-C-Sharp/tree/main/Object%20Pool%202
@ahmay
@ahmay 2 жыл бұрын
I have been developing games with unity for over 4 years and never seen this channel before. Precisely edited and explained semi-advanced topics as quick as possible. I appreciate the effort put into these videos.
@OneWheelStudio
@OneWheelStudio 2 жыл бұрын
Your description is exactly the goal. Hope the videos are useful!
@jakubstacho9199
@jakubstacho9199 2 жыл бұрын
Great channel, so underrated. I've been creating games in Unity for 4 years, but there is still so much to learn, thank you for the videos!!!
@goehlergamedev
@goehlergamedev Жыл бұрын
Stellar tutorial! Really enjoyed it. One small optimization tip: Using the transform.SetPositionAndRotation function is a bit more performant than setting .position and then .rotation
@MarekNijaki
@MarekNijaki 2 жыл бұрын
Your channel is a hidden gem! Hope you get more subscribers fast 😃
@OneWheelStudio
@OneWheelStudio 2 жыл бұрын
Keeping it hidden - part of my master plan :P I hope there's some useful stuff here for you! Cheers!
@mrvastayan
@mrvastayan 2 жыл бұрын
Glad to see you're still making videos, I recently started teaching myself actual C# got some decent progress going on a project now :)
@OneWheelStudio
@OneWheelStudio 2 жыл бұрын
Yep still making them. They come in bursts. In a bit of a dry spell at the moment. C# eh? Nice. How's that going for you? Happy to help if I can.
@mrvastayan
@mrvastayan 2 жыл бұрын
@@OneWheelStudio cheers I appreciate that! Yeah its going really well so far. Bolt gave me a really good foundational understanding to work from :)
@TheKr0ckeR
@TheKr0ckeR 2 жыл бұрын
I literally yesterday understood Old Object Pool totally and used it a lot! :D Now here is a new one to understand
@OneWheelStudio
@OneWheelStudio 2 жыл бұрын
Just trying to keep you on your toes! 😉
@ZenryokuFGC
@ZenryokuFGC Жыл бұрын
Concise and clear explanation in the video with no fluff, excellent code with example I'm using in my project right now - one of the easiest likes + subscribes I've ever done. Thank you
@lee1davis1
@lee1davis1 2 жыл бұрын
Added to my Unity must watch Playlist. Thanks for sharing.
@jeffreyhersh908
@jeffreyhersh908 2 жыл бұрын
What an interesting implementation of object pooling. I am definitely going to adapt this for my projects.
@Inkognitohaloramics
@Inkognitohaloramics 8 ай бұрын
Thanks for the video! This is super helpful
@ewwitsantonio
@ewwitsantonio 2 жыл бұрын
Great explanation. I really like this structure a lot. I'll have to test it out. Thanks for sharing this video with us :)
@cvnvr
@cvnvr 2 жыл бұрын
another great video. thanks for putting this together! one small thing... i noticed at 4:51 your second constructor example seems to be missing the same 'Spawn' call as the first? the numToSpawn var isn't being used at all in this one. considering both are seemingly doing the same thing, the second just includes the additional Actions being passed in, it could call : this(pooledObject, numberToSpawn) to avoid unwanted inconsistencies in behaviour/duplicating code. unless i misunderstood the purpose of the second constructor, then apologies and please ignore!
@OneWheelStudio
@OneWheelStudio 2 жыл бұрын
That was just a mistake - I wondered how long it would take for someone to notice ;) I didn't catch it until after the video was released. I fixed it in the GitHub repo (link in the description).
@devforfun5618
@devforfun5618 2 жыл бұрын
if i have multiple game objects using the same script in my case Projectile will this treat any projectile interchangeably regardless of gameobject ? do i need to create a subclass for each projectile in this case ?
@OneWheelStudio
@OneWheelStudio 2 жыл бұрын
If the projectile class is being used on multiple prefabs and the projectile class is the owner of the object pool then yes this object pool solution will treat all projectiles the same and a solution would be to subclass each type of projectile. What I'd recommend instead is that whatever object is shooting the projectile is the object that owns the object pool. Assuming each object only shoots one type of projectile it would solve the problem. If that doesn't work you can look at the code for my spawner (from the demo at the beginning of the video). In there I have 3 objects pools, one for each of the prefab shapes. Github Link: github.com/onewheelstudio/Adventures-in-C-Sharp/tree/main/Object%20Pool%202 To go a step further, in my personal project all of the projectile information (damage, speed, prefab, explosion prefab) is on a scriptable object. This way I just assign the SO to the object shooting AND the SO itself (one instance for each type of projectile) owns the object pool.
@devforfun5618
@devforfun5618 2 жыл бұрын
@@OneWheelStudio im using scriptable objects too, except for the visuals of the projectiles, but i guess making the owner of the object control the pool works better this solves another problem, the local player will be able to instantiate their projectiles instead of sending a request to the host to do do that, since the pool is per player, not per type of projectile and knowing which weapons a player has, and the fire rate, i know how exactly how many projectiles i need to spawn at the start thanks for the help
@ducnguyen-y4q
@ducnguyen-y4q 10 ай бұрын
can you have more example for objects that can be implement the IPoolable interface, and what the effective of this interface when we use it
@OneWheelStudio
@OneWheelStudio 10 ай бұрын
Come join the OWS discord. Happy to chat. I just created a class that implemented IPoolable yesterday! The main reason I did that is it stores the component rather than the game object which means I don't have to do GetComponent each time I pull an object out of the pool.
@TheKr0ckeR
@TheKr0ckeR 2 жыл бұрын
It's really cool! I have a one question. In this example, we spawn over time. So we dont have a pool at start. But how would be achieve something like that we create 50 objects, and pull from that pool? Should i just basically spawn in a loop, and push them to the pool, then i pull what i want?
@OneWheelStudio
@OneWheelStudio 2 жыл бұрын
Should be even easier if you're using the full code! The constructors have an optional parameter for a start count. You can simply pre-spawn objects when you create the pool. That said, the spawning is not done over multiple frames so depending on the object creating 50 in one frame may cause a lag spike. If the built-in system does create a lag spike, then I'd use a coroutine that over a handful of frames pulls whatever number of objects you need and them turns them all off to return them to the pool.
@somehappyballs6637
@somehappyballs6637 2 жыл бұрын
I like the "public T pull (vector3 position, quaternion values)" function ! Great idea
@TheKr0ckeR
@TheKr0ckeR Жыл бұрын
I am using this for a long time. I have generally problems at similar objects. What approach should we look if we have "Prefab variants" for example different enemy types, Should i create pool for every enemy type or what would be your apporach, like creating a dictionary or smth? Can you give a little information about that? Every time a new enemy added, I need to create a new pool. Instead I will be able to add it enemy pool and check it with LINQ or smth.
@OneWheelStudio
@OneWheelStudio Жыл бұрын
Hmm. That's an interesting question. My first thought would be to create a new pool for each variant and just use the system that way. But your suggestion of using a dictionary could also work in a couple different ways. You could make a dictionary that has a key of some type of enum (for example) with an item for each variant and then the value of the dictionary could be an object pool. Or you could flip that and create a new kind of object pool by implementing IObjectPool and put the dictionary inside the pool - this is probably more work on your part. I'd probably go for the first option if your enemies are the only use case if you have more use cases it might be useful to take the time to setup the new type of pool. Not sure if that helps.
@rashidfarhan6223
@rashidfarhan6223 2 жыл бұрын
hi! I'm kinda confused how should I approach the problem where I have multiple prefabs with a script(say : projectile) that is inherited from PoolObject.
@rashidfarhan6223
@rashidfarhan6223 2 жыл бұрын
public ObjectPool bulletPool; my pool was static. that's why. it works now as intended!
@AlexJrDev
@AlexJrDev Жыл бұрын
Hey, I am currently trying toadd pool to my game for an endless runner 3d and my character does not move in the z axis. My issue is that I can't spawn the roads as new ones gets deleted as I am unable to get the last activated tile from the pool and spawn it with an offset. Could someone help me?
@OneWheelStudio
@OneWheelStudio Жыл бұрын
I’m not sure if exactly what the problem is. Are you actually deleting the roads? For the object to go back to the pool it should just be disabled not turned off. If that’s not the issue it may be a bit more complicated - feel free to jump over to the OWS discord it might be easier to help there.
@themrfj
@themrfj Жыл бұрын
How is your ObjectPool different from the built in ObjectPool?
@OneWheelStudio
@OneWheelStudio Жыл бұрын
It’s been a while since I made it and don’t remember all the ins and outs of the built in system - it’s likely been updated too? I remember reading complaints of memory leaks too. 🤷‍♂️ At the end of the day the biggest different is with my own system I can adapt and adjust to code. I can also pool any type of object (can’t remember if the built in system will do that).
@shivamanand25
@shivamanand25 Жыл бұрын
For anyone getting problem ,If its giving Null reference error on line 43 attach "PoolObject" Script to Poolobject or Prefabs that u wanna spawn.
@OneWheelStudio
@OneWheelStudio Жыл бұрын
On mobile atm, is it possible the objects are getting destroyed instead of disabled?
@shivamanand25
@shivamanand25 Жыл бұрын
@@OneWheelStudio no, they're all getting disabled, I checked
@OneWheelStudio
@OneWheelStudio Жыл бұрын
Hmm. Not sure I’d try a break point and check what is null. I use this system in my own project so it should work.
@shivamanand25
@shivamanand25 Жыл бұрын
@@OneWheelStudio can I use this system for multiple gameobjects ? I mean there are multiple things to instantiate like coins, particles, bullets etc.
@OneWheelStudio
@OneWheelStudio Жыл бұрын
@@shivamanand25 Sure. Create a new pool for each type of object and then pull from which ever pool you need. If you want to combine multiple types into a single pool. That could be done, but I probably wouldn't go that direction.
@mohokhachai
@mohokhachai Жыл бұрын
Game object is s data type
@samuelsatiro2576
@samuelsatiro2576 2 жыл бұрын
I tried to use your pooling but I couldn't because it's giving null reference error -_- (Unity 2020.3)
@OneWheelStudio
@OneWheelStudio 2 жыл бұрын
Hmm. Strange. Can you be more specific as to what line is null or the use case?
@samuelsatiro2576
@samuelsatiro2576 2 жыл бұрын
@@OneWheelStudio The error is in the ObjectPool script (Line 43). For some reason the T is returning null.
@OneWheelStudio
@OneWheelStudio 2 жыл бұрын
@@samuelsatiro2576 The only reason I can think of for why that line would be null is if the object was destroyed. If I had to guess an object was put back in the pool and then destroyed? If you can't figure out the cause, you could put in a null check just above line 43 - check if T is null. If it is then "return Pull()" This would effectively discard the object and pop the next one out of the stack.
@samuelsatiro2576
@samuelsatiro2576 2 жыл бұрын
@@OneWheelStudio I made the modification you said, my Unity crashed lol
@OneWheelStudio
@OneWheelStudio 2 жыл бұрын
That is very odd. If you jump over to the OWS Discord. It would be helpful to see the code. For me the question is why is the object null? What is destroying the object?
@alexproskunov4001
@alexproskunov4001 2 жыл бұрын
1. no example of using. 2. over-engineering code. 3. Stack.
@everlastingmedia
@everlastingmedia 2 жыл бұрын
1. no example of an example you'd like used 2. failure to explain how code was over-engineered 3. "Stack" what?
Fast & Efficient Spawning with Object Pooling - Unity and C#
11:23
One Wheel Studio
Рет қаралды 23 М.
OYUNCAK MİKROFON İLE TRAFİK LAMBASINI DEĞİŞTİRDİ 😱
00:17
Melih Taşçı
Рет қаралды 13 МЛН
From Small To Giant Pop Corn #katebrush #funny #shorts
00:17
Kate Brush
Рет қаралды 72 МЛН
Every parent is like this ❤️💚💚💜💙
00:10
Like Asiya
Рет қаралды 18 МЛН
Man Mocks Wife's Exercise Routine, Faces Embarrassment at Work #shorts
00:32
Fabiosa Best Lifehacks
Рет қаралды 6 МЛН
Generics in C# and Unity - Do More with Less Code
12:40
One Wheel Studio
Рет қаралды 13 М.
Unity Game Dev - Why I Wrote My Own Object Pool Manager
16:57
Midnite Oil Software LLC
Рет қаралды 1,2 М.
20 Advanced Coding Tips For Big Unity Projects
22:23
Tesseract
Рет қаралды 187 М.
OBJECT POOLING in Unity
17:23
Brackeys
Рет қаралды 431 М.
Object Pooling (in depth) - Game Programming Patterns in Unity & C#
29:56
Jason Weimann (GameDev)
Рет қаралды 59 М.
Object Pooling in Unity 2021 is Dope AF
18:10
Tarodev
Рет қаралды 121 М.
Unity Coroutines - What? Why? How?
11:03
One Wheel Studio
Рет қаралды 45 М.
The Power of Scriptable Objects as Middle-Men
17:41
samyam
Рет қаралды 126 М.
OYUNCAK MİKROFON İLE TRAFİK LAMBASINI DEĞİŞTİRDİ 😱
00:17
Melih Taşçı
Рет қаралды 13 МЛН