0 votes

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.
https://www.youtube.com/watch?v=1PX9Tln-pc8

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
            print(coord)
            get_node("coord").clear()
            get_node("coord").push_color(Color(1, 1, 0))
            get_node("coord").push_underline()
            get_node("coord").add_text(str(coord))
            get_node("coord").pop()

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

asked Jan 5 in Engine by holgerm (15 points)

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.

1 Answer

+4 votes
Best answer

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()
    img.create(size.x,grid_thickness,false,Image.FORMAT_RGB8)
    img.fill(grid_color)
    tex.create_from_image(img)
    sprite.texture = tex

    for i in range(0,size.y,cell_size):
        var a = sprite.duplicate()
        add_child(a)
        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()
        add_child(a)
        a.global_position = Vector2(i,size.y/2)

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

answered Jan 5 by Adam_S (969 points)
edited Jan 7 by Adam_S

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.

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

func _ready():
    create_grid()

Otherwise did you get any error?

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.