+1 vote

Let's say I have a Sprite and a custom-drawn line (with draw_line):

Drawn stuff

I want to specify some area, like the area in red, below:

Area to draw in

Then, have these things only appear in the specified area:

Drawn stuff restricted to area

Is there a convenient way to accomplish this?

in Engine by (156 points)

1 Answer

+1 vote
Best answer

Control nodes can help doing this.
A Control is a rectangular node and has a clip_content property, which will achieve the effect you want within its rectangle. http://docs.godotengine.org/en/3.1/classes/class_control.html#class-control-property-rect-clip-content
Then everything drawn from it or child of it will be clipped. This is used originally to make scroll areas, but other uses like yours could work too.

If you want to do it from code, another way is with custom drawing in _draw, calling VisualServer:

VisualServer.canvas_item_set_custom_rect(get_canvas_item(), true, your_rect2)
VisualServer.canvas_item_set_clip(get_canvas_item(), true)

This is what Control does in the engine code: https://github.com/godotengine/godot/blob/aadbb66dc23d547393bc220e1f36b56cf10f5cca/scene/gui/control.cpp#L619

by (29,090 points)
selected by
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.