Godot-proposals: Add a `cycle()` method to Array

Created on 22 Oct 2020  路  1Comment  路  Source: godotengine/godot-proposals

Describe the project you are working on:
I am making a sokoban-like with rubik's cube type mechanics

Describe the problem or limitation you are having in your project:
I am using the place elements have in my array to denote its order for game mechanics, and I cycle these elements in a variety of ways. There isn't a built in way to cycle them

Describe the feature / enhancement and how it helps to overcome the problem or limitation:
add a method that takes in how many times (and in which direction) you want to cycle an array and outputs resulting from it

Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams:
here is what I have defined in my singleton which you can base C++ code around:

func cycle(times : int, arr : Array):
    times = posmod(times, arr.size())
    var temp_arr = []
    temp_arr.resize(arr.size())
    for i in arr.size():
        temp_arr[(i + times) % arr.size()] = arr[i]
    return temp_arr

or alternatively:

func cycle(times : int, arr : Array):
    times = times % arr.size()
    if abs(times) > arr.size() / 2:
        times = -sign(times) * (arr.size() - abs(times))
    if times > 0:
        for i in times:
            arr.push_front(arr.pop_back())
    elif times < 0:
        for i in times:
            arr.push_back(arr.pop_front())

If this enhancement will not be used often, can it be worked around with a few lines of script?:

it can be worked around using the code above in a singleton, but they might not have thought to optimize it as much as mine. Seems like it could see use similar to other functions in the class

Is there a reason why this should be core and not an add-on in the asset library?:
Just seems like it would fit in with the other functions in the class

core

Most helpful comment

For a Sokoban-like game, the cycle() method is likely not performance-critical :slightly_smiling_face:

I think implementing it in GDScript is fine.

>All comments

For a Sokoban-like game, the cycle() method is likely not performance-critical :slightly_smiling_face:

I think implementing it in GDScript is fine.

Was this page helpful?
0 / 5 - 0 ratings