20 instances lag

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Pelli

Hi, I am finishing a game, and I need some optimization tips to launch it, for example I have a power that instantiates 20 bullets, on my pc, and iphone it works fine, but on most devices like a galaxy j7 prime when the 20 bullets instances, the game freezes for a second. This also happens for example if the character grabs a point, it works well, but if he grabs three points in a row, game also freezes for a second and is very anoying to play. The bullet node is a kinematic with childs: collisionshape,2 sprites,animationplayer,particles2d,visibilitynotifier and an audiostreamplayer, when the bullet collide with and object or go screen off I delete it with queue free. The points node are similar but they are static and they queue_free when de player grab them.
So it´s normally than on many android devices I have a lot of lag? I need more hardware or I am doing in code anything wrong?

extends Area2D
onready var timer = $Timer
onready var sprite = $Sprite



func _on_Boom_body_entered(body):
	if body.is_in_group("Player"):
		var boom_fx = AudioStreamPlayer.new()
		boom_fx.set_bus("Sounds")
		self.add_child(boom_fx)
		boom_fx.stream = load ("res://Sounds/SoundFx/eggboompicked.wav")
		boom_fx.set_volume_db(-5)
		boom_fx.play()
		Global.boom += 1
		$CollisionShape2D.set_deferred("disabled",true)
		pop_up(0.2)
		timer.start()
		

func pop_up(time) -> void:
	$pop_up.interpolate_property(sprite,"position",sprite.position+Vector2(0,-10),
	sprite.position+Vector2(0,-30),time,Tween.TRANS_BACK,Tween.EASE_IN)
	$pop_up.start()
	yield($pop_up,"tween_completed")
	self.hide()
	


func _on_Timer_timeout():
	self.queue_free()

This Is the script of the kinematic bullet node, and thiss is the script inside the player that allows the power up

func _on_touch_rain():
	var rain = AudioStreamPlayer.new()
	rain.set_bus("Sounds")
	self.add_child(rain)
	rain.stream = load ("res://Sounds/SoundFx/rain.wav")
	rain.set_volume_db(-25)
	rain.play()

	var j = 0
	for i in 25:
		j += 35
		var newegg = bullet.instance()
		newegg.flag = true
		get_parent().add_child(newegg)
		newegg.global_position = Vector2((raining.global_position.x-450) +j, raining.global_position.y)

Hard to tell without code, are you preloading the bullets?

RazorSh4rk | 2020-05-25 08:18

What do you mean with preload? It’s a power up so when you do a double tap I make a for 1 to 20 and inside the loop I add_child the instance that load the scene on a var On top of the gdscript

Pelli | 2020-05-25 08:45

:bust_in_silhouette: Reply From: supper_raptor

This is because you are using particles2D , The shader need to be compiled and compilation takes time when you instance your bullet.

Fix : switch to gles 2 and use cpuParticles and you will see a huge performace increase in mobile devices.

You should not use gles 3 for mobile, its very slow , switch to gles 2

Okay , i Will do that, but for example points don´t have particles 2d and same happen when catching more than 3, Thanks a lot for taking yor time to reply

Pelli | 2020-05-25 16:08

So it worked, thanks!! no more lag when the power up is activated, the only problem that I have now, is when grabbing a 3 or more points it freezing for a second, Here is the code in case you know something

extends Area2D
onready var timer = $Timer
onready var sprite = $Sprite



func _on_Boom_body_entered(body):
	if body.is_in_group("Player"):
		var boom_fx = AudioStreamPlayer.new()
		boom_fx.set_bus("Sounds")
		self.add_child(boom_fx)
		boom_fx.stream = load ("res://Sounds/SoundFx/eggboompicked.wav")
		boom_fx.set_volume_db(-5)
		boom_fx.play()
		Global.boom += 1
		$CollisionShape2D.set_deferred("disabled",true)
		pop_up(0.2)
		timer.start()
		

func pop_up(time) -> void:
	$pop_up.interpolate_property(sprite,"position",sprite.position+Vector2(0,-10),
	sprite.position+Vector2(0,-30),time,Tween.TRANS_BACK,Tween.EASE_IN)
	$pop_up.start()
	yield($pop_up,"tween_completed")
	self.hide()
	


func _on_Timer_timeout():
self.queue_free()

Pelli | 2020-05-25 19:25