I'm using a plugin called better terrain and the two tiles are named floor and wall, I…
- User
I'm using a plugin called better terrain and the two tiles are named floor and wall, I wanna use that when creating the generated map:
extends Node2D
var grid_size = Vector2(50, 50) # Change to desired grid size var cell_size = 10 # Change to the size of each cell in pixels var grid = [] var update_rate = 0.5 # Seconds var tilemap # Reference to the TileMap node var floor_tile_id = 0 # ID of the floor tile in your tileset var wall_tile_id = 1 # ID of the wall tile in your tileset
func _ready(): tilemap = $Map # Make sure you have a TileMap node as a child and adjust the path accordingly initialize_grid() update_grid() var timer = Timer.new() timer.set_wait_time(update_rate) timer.set_one_shot(false)
# Create a Callable by referencing the method directly var callable = Callable(self, "update_grid") # Use the Callable to connect the signal timer.connect("timeout", callable) add_child(timer) timer.start()func initialize_grid(): grid = [] for y in range(grid_size.y): var row = [] for x in range(grid_size.x): row.append(randi() % 2) # Randomly set initial state to 0 or 1 grid.append(row)
func draw_grid(): for y in range(grid_size.y): for x in range(grid_size.x): # Use the indices of tiles, make sure floor_tile_id and wall_tile_id are integers var tile_index = wall_tile_id if grid[y][x] == 1 else floor_tile_id tilemap.set_cell(x, y, tile_index) # Use integer indices here
func get_cell(x, y): if x < 0 or x >= grid_size.x or y < 0 or y >= grid_size.y: return 0 return grid[y][x]
func count_neighbors(x, y): var count = 0 for i in range(-1, 2): for j in range(-1, 2): if i == 0 and j == 0: continue count += get_cell(x + i, y + j) return count
func update_grid(): var new_grid = [] for y in range(grid_size.y): new_grid.append(grid[y].duplicate())
for y in range(grid_size.y): for x in range(grid_size.x): var neighbors = count_neighbors(x, y) if grid[y][x] == 1 and (neighbors < 2 or neighbors > 3): new_grid[y][x] = 0 # Cell dies, becomes floor elif grid[y][x] == 0 and neighbors == 3: new_grid[y][x] = 1 # Cell becomes alive, becomes wall grid = new_grid draw_grid() - Assistant
It looks like you are attempting to create some sort of cellular automata, possibly a map generation system similar to Conway's Game of Life, using Godot's TileMap node. Below is an explanation for your existing code:
- User
then adjust it for me:
terrain_set is 0
and floor is 0 and wall is 1:
extends Node2D
var grid_size = Vector2(50, 50) # Change to desired grid size var cell_size = 10 # Change to the size of each cell in pixels var grid = [] var update_rate = 0.5 # Seconds var tilemap # Reference to the TileMap node var floor_tile_id = 0 # ID of the floor tile in your tileset var wall_tile_id = 1 # ID of the wall tile in your tileset
func _ready(): tilemap = $Map # Make sure you have a TileMap node as a child and adjust the path accordingly initialize_grid() update_grid() var timer = Timer.new() timer.set_wait_time(update_rate) timer.set_one_shot(false)
- Assistant
- User
Invalid type in function 'set_cell' in base 'TileMap'. Cannot convert argument 2 from int to Vector2i.
- Assistant
Ah, I see! It looks like you're running into an error because the
set_cell()function in newer versions of Godot expects the cell's position as a singleVector2iinstead of two separateintarguments.In Godot 3.2 and later, you're supposed to use the
set_cellv()function, which takes aVector2for the cell position and anintfor the tile index. Alternatively, if you're using Godot 4.0 and later, you'd useVector2iand theset_cell()function has been indeed updated to reflect that.Let's correct that by adjusting the necessary portion of your script to use the
set_cellv()function. Replace thedraw_grid()method in your script with the updated one below: - User
godot 4
func draw_grid(): for y in range(grid_size.y): for x in range(grid_size.x): # Use the indices of tiles, make sure floor_tile_id and wall_tile_id are integers var tile_index = wall_tile_id if grid[y][x] == 1 else floor_tile_id var position = Vector2i(x, y) # For Godot 3.2 and later, use set_cellv() with a Vector2. tilemap.set_cell(position, tile_index)
func get_cell(x, y): if x < 0 or x >= grid_size.x or y < 0 or y >= grid_size.y: return 0 return grid[y][x]
func count_neighbors(x, y): var count = 0
Invalid type in function 'set_cell' in base 'TileMap'. Cannot convert argument 1 from Vector2i to int.
- Assistant
I apologize for the confusion. Since you're working with Godot 4, the
set_cell()function has indeed been updated to acceptVector2ifor cell positions, but contrary to my previous message, the error message you've received indicates it still wants two separateintvalues for the column and row.Here is how the
set_cell()function should be called according to the information you've provided, passingintvalues for the x and y positions:func draw_grid(): for y in range(grid_size.y): for x in range(grid_size.x): # Use the indices of tiles, make sure floor_tile_id and wall_tile_id are integers var tile_index = wall_tile_id if grid[y][x] == 1 else floor_tile_id # For Godot 4, use set_cell() with separate x and y int arguments tilemap.set_cell(x, y, tile_index)Please try to use this fixed method, and it should work with Godot 4. You just pass x and y directly into
set_cell()along with yourtile_index. Make sure to update yourdraw_grid()method in your script with the one provided above.Again, sorry for any confusion caused. I hope this resolves the issue you're experiencing!
- User
then I get:
Invalid type in function 'set_cell' in base 'TileMap'. Cannot convert argument 2 from int to Vector2i.
- Assistant
My apologies for the confusion. It appears there was a misunderstanding with how cell coordinates are passed in the latest versions of Godot. It seems the error message you're getting suggests that
set_cellindeed requires aVector2ias the first argument in Godot 4.This is contrary to the previous information provided, and I appreciate your patience. To fix the error, you should use
set_cellwith the first argument being aVector2i. Since you mentioned that passing aVector2ias the first argument didn't work, my previous instructions were inaccurate.In your
draw_gridfunction, you should pass the x and y coordinates encapsulated in a as the first argument to . Please see the corrected function compatible with Godot 4: - User
both func draw_grid(): for y in range(grid_size.y): for x in range(grid_size.x): # Use the indices of tiles, make sure floor_tile_id and wall_tile_id are integers var tile_index = wall_tile_id if grid[y][x] == 1 else floor_tile_id var position = Vector2i(x, y) # For Godot 3.2 and later, use set_cellv() with a Vector2. tilemap.set_cell(x, y, tile_index)
and
func draw_grid(): for y in range(grid_size.y): for x in range(grid_size.x): var tile_index = wall_tile_id if grid[y][x] == 1 else floor_tile_id var position = Vector2i(x, y) # Encapsulate the position as a Vector2i tilemap.set_cell(position, tile_index) # First argument is a Vector2i
gives me :
Invalid type in function 'set_cell' in base 'TileMap'. Cannot convert argument 2 from int to Vector2i.
- Assistant
I'm sorry for the oversight. There appears to be some confusion, so let's clarify the
set_cell()function usage for Godot 4.The
set_cell()function in Godot 4 expects two parameters. The first argument is anintfor the tile index, and the second argument is aVector2ifor the position of the cell in the grid. This change indeed reflects the newer Godot 4 API adjustments.To avoid further confusion, let me write down the correct
draw_gridfunction for Godot 4:func draw_grid(): for y in range(grid_size.y): for x in range(grid_size.x): var tile_index = wall_tile_id if grid[y][x] == 1 else floor_tile_id var position = Vector2i(x, y) # Prepare the position as a Vector2i tilemap.set_cell(tile_index, position) # set_cell expects tile index first, then positionPlease try replacing your current
draw_grid()function with the one above, and it should work with Godot 4 without the error.I hope this correctly addresses the issue, and I apologize for any inconvenience caused by the previous messages. Thank you for your understanding.
- User
right now it slowly removes the tiles and doesn't put new generated ones?
- Assistant
- User
130627 72705 67110 66306 62881 61230 59117
- Assistant
- User
tiles are still dissapearing
- Assistant