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

Поделиться
HTML-код
  • Опубликовано: 13 янв 2025

Комментарии • 53

  • @OneWheelStudio
    @OneWheelStudio  2 года назад +3

    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 года назад +16

    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 года назад +1

      Your description is exactly the goal. Hope the videos are useful!

  • @jakubstacho9199
    @jakubstacho9199 2 года назад +3

    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!!!

  • @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 года назад +1

      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 :)

  • @ZenryokuFGC
    @ZenryokuFGC 2 года назад

    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

  • @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! 😉

  • @MarekNijaki
    @MarekNijaki 2 года назад +1

    Your channel is a hidden gem! Hope you get more subscribers fast 😃

    • @OneWheelStudio
      @OneWheelStudio  2 года назад +1

      Keeping it hidden - part of my master plan :P
      I hope there's some useful stuff here for you!
      Cheers!

  • @goehlergamedev
    @goehlergamedev 2 года назад +1

    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

  • @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.

  • @devforfun5618
    @devforfun5618 2 года назад +3

    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 года назад +3

      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 года назад +1

      @@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

  • @hxntytube
    @hxntytube 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).

  • @ducnguyen-y4q
    @ducnguyen-y4q Год назад

    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  Год назад

      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.

  • @Inkognitohaloramics
    @Inkognitohaloramics Год назад

    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 :)

  • @AlexJrDev
    @AlexJrDev 2 года назад

    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  2 года назад +1

      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).

  • @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 года назад +1

      public ObjectPool bulletPool;
      my pool was static. that's why. it works now as intended!

  • @TheKr0ckeR
    @TheKr0ckeR 2 года назад

    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  2 года назад +2

      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.

  • @shivamanand25
    @shivamanand25 2 года назад

    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  2 года назад

      On mobile atm, is it possible the objects are getting destroyed instead of disabled?

    • @shivamanand25
      @shivamanand25 2 года назад

      @@OneWheelStudio no, they're all getting disabled, I checked

    • @OneWheelStudio
      @OneWheelStudio  2 года назад

      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 2 года назад

      @@OneWheelStudio can I use this system for multiple gameobjects ? I mean there are multiple things to instantiate like coins, particles, bullets etc.

    • @OneWheelStudio
      @OneWheelStudio  2 года назад

      @@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.

  • @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 года назад +1

      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.

  • @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?

  • @mohokhachai
    @mohokhachai Год назад

    Game object is s data type

  • @alexproskunov4001
    @alexproskunov4001 2 года назад

    1. no example of using.
    2. over-engineering code.
    3. Stack.

    • @everlastingmedia
      @everlastingmedia 2 года назад +8

      1. no example of an example you'd like used
      2. failure to explain how code was over-engineered
      3. "Stack" what?