Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this package will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [0.12.2] - 2024-11-02

**Fixed**:
- Fixed an inssue where *IPoolEntityObject<T>.Init()* wouldn't be called when spawning entities

## [0.12.1] - 2024-10-25

**Fixed**:
Expand Down
18 changes: 12 additions & 6 deletions Runtime/ObjectPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,7 @@ protected ObjectPoolBase(uint initSize, T sampleEntity, Func<T, T> instantiator)

for (var i = 0; i < initSize; i++)
{
var entity = instantiator.Invoke(SampleEntity);
var poolEntity = entity as IPoolEntityObject<T>;

poolEntity?.Init(this);
_stack.Push(entity);
_stack.Push(CallInstantiator());
}
}

Expand Down Expand Up @@ -274,7 +270,7 @@ protected virtual T SpawnEntity()

do
{
entity = _stack.Count == 0 ? _instantiator.Invoke(SampleEntity) : _stack.Pop();
entity = _stack.Count == 0 ? CallInstantiator() : _stack.Pop();
}
// Need to do while loop and check as parent objects could have destroyed the entity/gameobject before it could
// be properly disposed by pool service
Expand All @@ -287,6 +283,16 @@ protected virtual T SpawnEntity()

protected virtual void PostDespawnEntity(T entity) { }

protected T CallInstantiator()
{
var entity = _instantiator.Invoke(SampleEntity);
var poolEntity = entity as IPoolEntityObject<T>;

poolEntity?.Init(this);

return entity;
}
Comment on lines +286 to +294
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image description CodeRabbit

In the new code, the CallInstantiator() method has been modified to remove the direct invocation of _instantiator.Invoke(SampleEntity) and instead use the instantiator field. However, the logic for calling IPoolEntityObject<T>.Init() has been removed entirely from this section of code.

To fix this, you can modify the code as follows:

protected T CallInstantiator()
{
    var entity = instantiator.Invoke(SampleEntity);
    var poolEntity = entity as IPoolEntityObject<T>;

    poolEntity?.Init(this);

    return entity;
}

This change ensures that IPoolEntityObject<T>.Init() is called after creating the entity.

Overall, the changes made to Runtime/ObjectPool.cs seem to centralize the entity instantiation and initialization logic. However, it's important to ensure that IPoolEntityObject<T>.Init() is still called correctly when spawning entities.


protected void CallOnSpawned(T entity)
{
var poolEntity = entity as IPoolEntitySpawn;
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
"name": "com.gamelovers.services",
"displayName": "Services",
"author": "Miguel Tomas",
"version": "0.12.1",
"unity": "2022.4",
"version": "0.12.2",
"unity": "2022.3",
"license": "MIT",
"description": "The purpose of this package is to provide a set of services to ease the development of a basic game architecture",
"type": "library",
"hideInEditor": false,
"dependencies": {
"com.gamelovers.dataextensions": "0.6.0"
"com.gamelovers.dataextensions": "0.6.2"
}
}