Extending the Editor with Plugins in Godot
- Getting Started
- Plugin Overview
- Creating Your First Plugin
- Taking a Closer Look
- Handling Node Selection
- Adding the Button
- Developing an Advanced Plugin
- Creating the Menu Scene
- Loading and Unloading the Menu
- Getting Selected Nodes
- 2D Physics in Godot
- Starting and Stopping Physics
- Custom Physics Spaces
- Physics Frames
- Adding Quality of Life Features
- Where to Go From Here?
The Simulate 10 physics frames button will simulate 10 physics frames at a time and then stop. This is nice to slightly move your nodes without having to have perfect timing to hit the stop button.
You already have most of the logic in place, so adding the needed code is trivial. Replace the
pass keyword in the
_on_ten_frames_pressed() function with the following:
for i in range(10):
This function starts up the physics simulation, waits for 10 physics frames to pass and then stops the simulation. Here’s a rundown of the code:
- Start the physics simulation.
- This for-loop uses the
awaitkeyword to wait for the
physics_framesignal ten times. This will stall the function for ten physics frames before moving on.
- Deactivate the custom physics space and stop the physics simulation.
Now save the script, return to the 2D screen and click the Simulate 10 physics frames button while you have some nodes selected. You should see the nodes simulate for a short while before stopping.
Adding Quality of Life Features
While playing around with the plugin, you might have noticed that stopping the physics doesn’t mean that the velocities of the nodes get reset. If you make one of the balls roll to the side for example, and then put them on one of the platforms, it’ll keep its momentum and starts rolling. This is not always desirable.
To fix this potential issue, you need a quick way to reset the velocity of the selected nodes. To do this, replace the
pass keyword in the
_on_reset_velocity_pressed() function with:
var selected_nodes = _get_selected_nodes()
for selected_node in selected_nodes:
if selected_node is RigidBody2D:
selected_node = selected_node as RigidBody2D
selected_node.linear_velocity = Vector2.ZERO
selected_node.angular_velocity = 0
This gets all selected nodes and loops through them. If the node is a
RigidBody2D, it zeroes out the linear and angular velocity. Here’s the code in detail:
- Get all selected nodes and start iterating over them.
- If the selected node is a
- Use the node as a
RigidBody2D. This line isn’t crucial, but doing casts like these is a good idea as you’ll get better code completion from the code editor.
- Reset the linear and angular velocity of the node to
Vector2.ZEROand 0 respectively.
With this, you can reset the velocity of selected nodes with ease. Save the script and give it a shot! Make one of the ball move sideways by colliding with another node, then stop the simulation and reset the velocity. If you now the place the ball on a flat surface, it will stay put.
Where to Go From Here?
You can download the project files at the top or bottom of this tutorial by clicking the Download materials link.
Congratulations on making it to the end of the article. You’re now ready to start developing your own editor plugins and speed up your workflows. This is a useful skill to master as it can save you and your teammates a lot of time. I’ve only scratched the surface of plugins here, as you can make full-blown custom tools in Godot.
To find out more about creating plugins, check out Godot’s official documentation on the subject.
Now go out there and create your own plugins!
Thanks for reading. If you have any questions or feedback, please join the discussion below.