Game Design Patterns: Flyweight

<p>Using the&nbsp;<em>flyweight&nbsp;</em>pattern is a great way to optimize performance in your game, especially concerning large volumes of prefabs&nbsp;<em>(reusable objects)</em>&nbsp;and script&nbsp;<em>instances</em>. When using&nbsp;<em>prefabs</em>, you can save performance by reusing a&nbsp;<em>copy&nbsp;</em>of that object. While each&nbsp;<em>instance&nbsp;</em>of that object has a&nbsp;<em>shared&nbsp;</em>material, and uses the same script, it still creates a&nbsp;<em>unique instance</em>&nbsp;of that script. When working with thousands, or tens of thousands of reusable objects with shared variables, the flyweight pattern is a good solution for&nbsp;<em>coupling&nbsp;</em>the shared properties that will remain the&nbsp;<em>same&nbsp;</em>between all instances.</p> <p>Let&rsquo;s use an&nbsp;<em>Enemy</em>&nbsp;script as an example, assuming you may have 20,000 enemies in your scene at once. They all have a&nbsp;<em>max</em>,&nbsp;<em>min&nbsp;</em>and&nbsp;<em>current&nbsp;</em>health variable, a&nbsp;<em>speed&nbsp;</em>variable, a handle to the player&rsquo;s&nbsp;<em>transform&nbsp;</em>component, and a&nbsp;<em>wait time</em>&nbsp;for a coroutine.</p> <p><a href="https://levelup.gitconnected.com/game-design-patterns-flyweight-b087cabb457"><strong>Learn More</strong></a></p>