0 votes
func sort_height_descending(a: Array, b: Array) -> bool:
    if a[1].get_height() > b[1].get_height():
        return true
    elif a[1].get_height() < b[1].get_height():
        return false
    elif a[1].get_width() > b[1].get_width():
        return true
    elif a[1].get_width() < b[1].get_width():
        return false
    else:
        return true

What do?

Godot version 3.5
in Engine by (8,526 points)

3 Answers

0 votes

I assume this is supposed to be a sort_custom() function for an Array (as documented here)?

If so, the docs show the function should be static.

Using your function as-is seems to work for me - after making it static. However, I don't really understand what you're doing.

You seem to be trying to sort an Array of Array's that contain some sort of object that has get_height() and get_width() functions (such as an Image).

Further, you're basing the sort decision on get_height() and get_width() results of the 2nd element in the array. Again, not sure of the intention here, but the above does seem to work for me (or, at least generates a modified order for my random array and does not generate the error you reported).

If the above doesn't help, perhaps a more detailed explanation, along with some more code that shows how you're setting up and calling the custom sort.

by (19,238 points)

Changing the func to static did not change anything.

I am trying to make a sprite packer. I am sorting the sprites from largest to smallest, height first then width. (The first element of the array is a TreeItem and is irrelevant.)

Can you post some code showing how the custom sort function is being called?

var sorted_sprites := []
for key in sprites:
    sorted_sprites.append([key, sprites[key][0]])
if max_size.selected == 0: # Max width, sort by height
    sorted_sprites.sort_custom(self, "sort_height_descending")
0 votes
func sort_height_descending(a: Array, b: Array) -> bool:
    if a[1].get_height() > b[1].get_height():
        return true
    elif a[1].get_height() < b[1].get_height():
        return false
    elif a[1].get_width() > b[1].get_width():
        return true
    else:
        return false

works but i don't know why

by (8,526 points)

Ah, yeah, you're correct - that static doesn't seem to be necessary. And, again, I'm not seeing the error you reported using a copy/pasted instance of your function. Perhaps my dummy array isn't exercising the function well enough...

And, in the function that works , you've just dropped the last elif clause and changed the return value of the else clause (that's what it looks like at a glance)?

yeah, since the example had return false as the last clause

0 votes

I've stepped into the same issue:

static func sort_algorithm(unit_a: Unit, unit_b: Unit) -> bool:

    var agility_a = unit_a.agility
    var agility_b = unit_b.agility

    var percentage:= randf_range(0.875, 1.125)
    percentage = snapped(percentage, 0.001)

    agility_a = (agility_a * percentage)
    agility_b = (agility_b * percentage)

    var x = randi_range(1, -1)
    var y = randi_range(1, -1)

    agility_a = agility_a + x
    agility_b = agility_b + y

    if agility_a > agility_b:
        return true
    else:
        return false

I've performed many tests and I've realized that the error triggers randomly when using randi_range(1, -1), I;ve also tried replacing that function for randi() % 2 and the problem doesn't happen if I use that once only... I've no idea why is that...

by (14 points)
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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.