0 votes

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:

StaticBody2D
   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?

in Engine by (20 points)
edited by

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

2D walls only.

1 Answer

0 votes

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:

tool

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.

by (840 points)

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.

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.

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

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

StaticBody2D
    -CollisionShape2D
    -Sprite

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.

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 Frequently asked questions and 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 [email protected] with your username.