Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix instance pooling called multiple times #156

Merged
merged 1 commit into from
Nov 24, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func instance(config_callback:Callable = Callable())->Node:
var _node:Node = _create_instance()
if config_callback.is_valid():
config_callback.call(_node)
assert(_node.get_parent() == null)
parent_reference_resource.node.add_child.call_deferred(_node)
return _node

Expand All @@ -74,9 +75,14 @@ func _erase(node:Node)->void:
active_list.erase(node)
updated.emit()

## Apparently, physics thread or other reason, this can be called more than once
func _return_to_pool(node:Node)->void:

assert(node.get_parent() == parent_reference_resource.node)
_handle_return.call_deferred(node)

func _handle_return(node:Node)->void:
assert(node.get_parent() == parent_reference_resource.node)

node.tree_exiting.connect(pool_list.append.call_deferred.bind(node), CONNECT_ONE_SHOT)
parent_reference_resource.node.remove_child(node)
pool_list.append(node)
15 changes: 13 additions & 2 deletions addons/great_games_library/resources/InstanceResource/PoolNode.gd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class_name PoolNode
extends Node

signal pool_requested
signal pool_requested()

## Mark nodes to trigger _ready() every time scene is added to the tree
@export var ready_nodes:Array[Node]
Expand All @@ -12,13 +12,24 @@ signal pool_requested
## GPUParticles2D that has to be reset every time entering tree and remove previous particles from memory
@export var particle2d_list:Array[GPUParticles2D]

var pool_was_requested:bool

func _ready()->void:
pool_was_requested = false

func pool_return()->void:
if pool_was_requested:
## For some stupid reason (Physics thread or something else), can call multiple times before removed from tree
return
pool_was_requested = true
request_ready()

for _animation_player:AnimationPlayer in animation_player_list:
_animation_player.stop()
for _particle:GPUParticles2D in particle2d_list:
if !_particle.tree_entered.is_connected(_particle.restart):
_particle.tree_entered.connect(_particle.restart)
for _node:Node in ready_nodes:
_node.request_ready()

pool_requested.emit()
8 changes: 0 additions & 8 deletions kanban_tasks_data.kanban
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"e0f8ddfd-5b24-46ad-9552-14eb3e22e82a",
"a5b31bad-1666-4183-9126-e94fe512886c",
"720dc01a-3934-425a-931a-a1f86298c9ca",
"d356a173-8caa-48f2-80f2-213bfec73bc3",
"93adc641-53c0-4d53-833e-64874674d9ec",
"a5896df6-3b93-44d2-b2c7-a23320379a72",
"b3d2c00b-d634-441e-8532-5cec9d7b630e",
Expand Down Expand Up @@ -49,13 +48,6 @@
"category": "3c019f8f-7018-401f-89e7-6ddedafd24e2",
"steps": []
},
{
"uuid": "7a7fed89-413e-42e1-8c0e-7674e1214fcc",
"title": "Weapon sprite positions",
"description": "",
"category": "3c019f8f-7018-401f-89e7-6ddedafd24e2",
"steps": []
},
{
"uuid": "720dc01a-3934-425a-931a-a1f86298c9ca",
"title": "Weapon UI representation",
Expand Down