How to: make a physics wall that's resizable, and filled with colour?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By confused

Is it possible, in the editor, to make a resizable wall object that has a coloured and editable fill colour, and physics body, both of which scale accurately with any editor resizing of the wall occurs?

If so, how?

In the Editor:

I have:

   Child = Sprite
      Child = CollisionShape2D - a rectangle

I can scale the Sprite, this works in the Editor, as the CollisionShape2D scales with the Sprite. But GODOT doesn’t like it because the CollisionShape2D is supposed to be a child of the StaticBody2D, not a child of the Sprite.

So how am I supposed to build a wall component that I can visually scale/distort the shape of in the editor, and have that change the shape of the physic object of this wall component, too?

Do you want to do this in 2D or 3D?

Zylann | 2018-03-12 16:05

2D walls only.

confused | 2018-03-12 16:08

:bust_in_silhouette: Reply From: Footurist

First, you have some luck, because with RigidBodies scaling can be a bit annoying, since they reset their scaling at runtime, but static ones work fine for this. What you’re looking for is called tool mode. If you write tool at the very top of your script, you can see editor changes in realtime. You can use the keyword setget to set up setter functions, which will do the trick. It looks like this:


extends StaticBody2D

var ready_called = false
export (Vector2) var my_scale setget set_my_scale

func _ready():
    ready_called = true

func set_my_scale(value):
    my_scale = value
    if ready_called:
        scale = my_scale

I think you don’t need to use setters for in-built properties, but I wanted to show you how it works. The flag test is there, because it prevents Godot from calling the setter when the object’s properties aren’t yet set when you run the project.

Can we rewind a little bit?

Firstly, I can’t even seem to make a wall that I can scale in one direction, and has colour (filled).

My stack seems to be wrong. Or I’m misunderstanding something about how to build a wall object.

confused | 2018-03-12 18:10

btw, I mean to be able to edit this in the editor, with the handles… kind of just like how this might be done in any other visual editor, from Unity to 3ds Max to Photoshop and everything in between.

Then change the colour of some of these wall segments. etc. Just as though this was a 2D visual editor.

confused | 2018-03-12 18:13

edited question to better explain what I’m trying to do.

confused | 2018-03-12 18:29

The correct hirarchy for a “wall object”, which can collide, is as follows:


Then, you can select the selection tool (arrow) and drag the vertices. However, this is not always advised, because this way you scale the object, that is selected in the hirarchy, which in your case is fine. All CanvasItems have a property “modulate”. Since a lot of nodes inherit from CanvasItem, you can change the modulate of most nodes to change the color. You’ll find this property in the inspector of the node.

Btw: The children do not have to be in a certain order in that specific case.

Footurist | 2018-03-12 20:58