hi, well im not a programmer and have a little time for working on my project so i couldn't figure out this problem.
for performance issues i did a lot of things and finaly reach the fixed 60 and above fps for pc, but i cant get the same performance from mobile (middle end device, not that bad).
so, i know a lot of people say that object pooling its not necessity for godot, but there are also people that show results (with data) that some times pooling give you performance you need. and project i working on it have a lot of bullet create and death cycle.
around 100-150 per second bullets are created and freed from tree. and its good, but only at pc.
so even tho i know i will strugle i start to object pooling part, its my last resort, seriously, i dont know what to do anymore for get the 60 fps from a mobile device. ( i did a lot of optimization work, generaly on the project. collisions, mesh sizes, reduce the computing stuff etc. )
any way, this is what im came up with:
- i have a auotoloaded script named
Global
, that include the function that instance the bullets at the start of the runtime.
- and another script that become parent of the bullets and handle the number of the bullets that should be instanced.
- and another script that fire the bullets, and array stuff.
this is auotoloaded script Global:
var muzzle_1_fire_bullet_array :Array = []
var muzzle_1_fire_active_bullet : Array = []
var muzzle_1_fire_b
func muzzle_1_fire_b_instance(count,add_child_node):
for instance in range (count):
muzzle_1_fire_b = preload("res://scenes/bullet.tscn").instance()
add_child_node.add_child(muzzle_1_fire_b)
muzzle_1_fire_b.global_transform.origin = Vector3(instance * 3 ,4,instance * 6)
muzzle_1_fire_bullet_array.append(muzzle_1_fire_b)
muzzle_1_fire_b.shoot = false
this one for bullet handler and parent of the instanced bullets:
extends Spatial
func _ready():
Global.muzzle_1_fire_b_instance(20,self)
and this one is belong the player script, the one that shoot the already instanced but inactive bullets:
## muzzle 1
func _on_fire_delay_timeout():
if Input.is_action_pressed("fire"):
var summoned_bullet = Global.muzzle_1_fire_bullet_array.pop_back()
summoned_bullet.global_transform.origin = muzzle1.global_transform.origin
Global.muzzle_1_fire_active_bullet.push_back(summoned_bullet)
var active_bullet = Global.muzzle_1_fire_active_bullet[-1]
active_bullet.shoot = true
active_bullet.set_process(true)
active_bullet.look_at(transform.origin,Vector3.UP)
this is works fine like this:
problem is append the bullets that gone outside of the screen,
for that i did add a VisibilityNotifier
node to bullet scene and use the signal
named screen_exited
.
but no mater what i did, i couldn't reach the bullet that gone to outside of the screen.
i mean, exactly bullet that inside of the active bullet array(muzzle_1_fire_active_bullet
) gone the outside of the screen, i need to reach that bullet and remove it from active bullet array (muzzle_1_fire_active_bullet
), after that add the same bullet the deactive bullet array again(muzzle_1_fire_bullet_array
). you guys know the drill.
i mean i dont know how to work with arrays with more efficient way, like i said im not a programmer, so this maybe a noob question, but i need help. i know the problem, and solition but dont know the syntax i suppose.