0 votes

I don't know a good way to add limits to a map. I tried staticbody2D blocks but it is too expensive for processing.

in Engine by (673 points)

I'm currently using tilemaps.

What exacty do you mean by limit. Is it for collision with moving entities, camera or disabling some functions? If it is for collision why you can't use some tile wich is already obstacle in your tileset. Or you can add transparent tile with collision rectangle and add it on boreders of your level.

1 Answer

0 votes
Best answer

It depends on how big is your map. I'm currently building world borders, using StaticBody2D with RectangleShape2D as collision shape. It's not as expensive as you might think, the geometry of the shape is actually simple to check collision against (though I haven't really tested this yet), especially for a static body. I think it's possible to build limits/borders using TileMap, but I have little experience using it.

If you want to try building borders the way I do, take a look at this structure:

enter image description here

The sprite defines visible border area, set the texture there, and make sure to import the texture with Repeat flag in the import dock. The challenge here is to match the sprite with the actual shape's area. Here's the script that allows to texture the extents of the static body's rectangular shape as well as to change the actual size:

extends StaticBody2D

var size setget set_size

func set_size(p_size):
    size = p_size
    $sprite.region_rect = Rect2(Vector2(), size)
    # CollisionShape2D is only used in editor! 
    # Use shape_owner to retrieve the actual shape.
    shape_owner_get_shape(0, 0).extents = size / 2

Make sure to set Region Enabled property for the sprite so that the pattern can repeat. If setting the size only resizes the region rect of the sprite and not the shape, you can try to set it on the next frame (I've had this issue when trying to initialize border sizes within a setter method of an exported property):

$border.call_deferred('set_size", Vector2(1000, 50)) # or ...
$border.set_deferred('size", Vector2(1000, 50)) # Godot 3.1

Most likely you'll also want to instance many borders and set different sizes for each. But if you try to change the extents of RectangleShape2D, it will also resize the borders of all other instanced shapes! That's because the resources are created and loaded once, unlike other objects that can have many instances. To solve this, make sure to check Local To Scene property so that the resource will be duplicated for each instanced border:

enter image description here

by (1,367 points)
selected by

It's a good idea, but I used tilemap collision (I really didn't know I could use tiles with collision checks).

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.
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 webmaster@godotengine.org with your username.