-
-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a TileMap set_cell_region
method to set a rectangular area of tiles faster
#1538
Comments
You can use a single func _ready():
var width = 6
var height = 4
# Instead of:
for i in width:
for j in height:
printt(i, j)
print()
# Use:
for i in width * height:
printt(i / height, i % height) |
set_cell_region
method to set a rectangular area of tiles faster
@Calinou var init = Vector2(0,0)
var fin = Vector2(1000,1000)
func _ready():
var start_time = OS.get_ticks_msec()
with_set_cell_double()
var time_set_cell_double = OS.get_ticks_msec()-start_time
print("Time consumed in set_cell in a double for: "+str(time_set_cell_double))
start_time = OS.get_ticks_msec()
with_set_cell()
var time_set_cell = OS.get_ticks_msec()-start_time
print("Time consumed in set_cell in a for: "+str(time_set_cell))
start_time = OS.get_ticks_msec()
with_set_cell_region()
var time_set_cell_region = OS.get_ticks_msec()-start_time
print("Time consumed in set_cell_region: "+str(time_set_cell_region))
print("set_cell_region: "+str((1-time_set_cell_region*1.0/time_set_cell)*100) +"% less than set_cell in a for.")
func with_set_cell():
var tilemap = $TileMap2
var width = int(fin.x-init.x)
var height = int(fin.y-init.y)
for i in width*height:
tilemap.set_cell(i/height,i%height,0)
func with_set_cell_double():
var tilemap = $TileMap3
for x in range(init.x,fin.x-1):
for y in range(init.y,fin.y-1):
tilemap.set_cell(x,y,0)
func with_set_cell_region():
$TileMap.set_cell_region(init,fin,0) This code is in a Node2D and have 3 tilemaps as childs with the same characteristics. The results are: Time consumed in set_cell in a double for: 3096 |
@dariomnz Feel free to open a pull request with your changes then 🙂 Make sure to open the pull request against the |
Just to not lose the already done discussion on this topic, here a reference to an previous issue (in the main repository), with same background = set_cell() performance -> #31020 |
I would like to also mention that similar functionality but not-regionalized would be very helpful. Something like I believe there's a common use case for this. Let's consider an example: a colony managing game that uses the tile map to display a top-down view. Every frame a vegetation growth system decides what plants have grown and updates the displayed tiles appropriately. This might involve many tile throughout the whole map needing to update. Another use case would be where due to the TileMap being just mostly a visual representation the actual map and it's data is being tracked as a completely separate data structure. All game systems could operate on that specific data and then at the end consolidating all changed tiles into a single list. That list then can be sent to TileMap to update in a single call. |
Any progress on this? the biggest time consumer is set_cell for me, and I can't chunk my tilemap up because base.set_cell_ex isn't thread safe Send (gdext) anyway, this is inherently a single threaded task. |
See this comment: godotengine/godot#39833 (comment) We need benchmarks to see if this really makes a significant performance difference. |
Describe the project you are working on:
2d game
Describe the problem or limitation you are having in your project:
When setting a large amount of the same tile in a tilemap inside a doble for, it takes a lot of time.
For example if you want to fill a tilemap, and then you are going to remove the tiles for creating the rooms.
Describe the feature / enhancement and how it helps to overcome the problem or limitation:
The new feature is to create a new method in the tilemap class (source code) like set_cell_region.
Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams:
Code in c++
If this enhancement will not be used often, can it be worked around with a few lines of script?:
It can be do in 3 lines of code in gdscript but take a lot of time the execution.
Is there a reason why this should be core and not an add-on in the asset library?:
I already compile the engine with this new feature and I can test it.
The results are this for area of 1000x1000 tiles:
Time consumed in set_cell in a double for: 4080 msec
Time consumed in set_cell_region: 2345 msec
set_cell_region: 42.52451% less than set_cell in a double for.
I think that there is a good new feature. I already have the source code. But because I new i don't know if I need to create a pull request or this is later.
The text was updated successfully, but these errors were encountered: