Horrible performance on android on nearly empty scene

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

I am very new to Godot (but I am experienced in other engines). I wanted to give Godot a go as the script is very similar to Python.

I thought, I would start with something very simply but in the end had big issues on performance on android only. Thats why I wanted to test something very simple to exclude my mistakes.

I rebuild this here as a test.

and modified the code a bit as I had some errors in Godot 3.1.2
Here is my modified code

extends Node2D

onready var grid_size = Vector2(get_viewport_rect().size.x, get_viewport_rect().size.y)

var cell_size = 64
var coord = Vector2(0, 0)
var global_x
var global_y

func _ready(): set_process_input(true)

func _input(event):
	if event is InputEventMouseMotion:
		global_x = event.global_position[0]
		global_y = event.global_position[1]
		var pos = Vector2(int(global_x/cell_size), int(global_y/cell_size))
		if pos != coord:
			coord = pos
			get_node("coord").push_color(Color(1, 1, 0))

func _draw():
	for x in range(0, grid_size.x, cell_size):
		for y in range(0, grid_size.y, cell_size):
			draw_line(Vector2(x, y), Vector2(x, y + cell_size), Color(0, 1, 0), 1.0)
			draw_line(Vector2(x, y), Vector2(x + cell_size, y), Color(0, 1, 0), 1.0)

It works flowlesly on pc but on Android its extremly laggy.
Tested on a Samsung Galaxy S8

So why is this?
Its just a simple Node2D + the text label

Can you show in a video how laggy it is on Android ? Or show FPS values ?
Also did u try on other android phones ? just to make sure it is not a specific phone issue.

GameVisitor | 2020-01-05 20:44

:bust_in_silhouette: Reply From: Adam_S

The draw_line()function is causing the lags, seems to be pretty expensive, especially on mobile.

I’m wondering why the guy in the video draws the grid that way. (2-4 fps on my phone)

I changed the code and it gave me 40-50 fps.

func _draw():
    for x in range(0, grid_size.x, cell_size):
        draw_line(Vector2(x, 0), Vector2(x, grid_size.y), Color(0, 1, 0), 1.0)
    for y in range(0, grid_size.y, cell_size):
        draw_line(Vector2(0, y), Vector2(grid_size.x, y), Color(0, 1, 0), 1.0)

But if your target are mobile devices, I would advise you to use an image (or multiple) instead of the draw_line() function.

Just wrote this function which uses sprites to create the grid, and it gives me 60 fps.

var cell_size = 64
var grid_thickness = 1
var grid_color = Color.green

func create_grid():
    var size = Vector2(get_viewport_rect().size.x, get_viewport_rect().size.y)
    var img = Image.new()
    var tex = ImageTexture.new()
    var sprite = Sprite.new()
    sprite.texture = tex

    for i in range(0,size.y,cell_size):
        var a = sprite.duplicate()
        a.global_position = Vector2(size.x/2,i)

    sprite.rotation_degrees = 90

    for i in range(0,size.x,cell_size):
        var a = sprite.duplicate()
        a.global_position = Vector2(i,size.y/2)

EDIT: I added the variables to the script above to make it clearer

Frist of all thank you for your answer.

How lucky I was that the test which I choose to test the bad performance had itself a bad performance…

Your func _draw code worked and got me also 40+ Frames.
Before the fps count didn’t show anything besides 1.

Your 2nd code with the sprites didn’t work for me but anyway this was just a test.
I will now try to debug my original code and see if I was using something expensive there.

holgerm | 2020-01-06 21:54

The second code should work too. Did you call the create_grid() function?
If not, put it in your ready() function like:

func _ready():

Otherwise did you get any error?

Adam_S | 2020-01-06 23:25