0 votes

I want to change the gravity scale property of a rigidbody, but somehow it does not seem to work.

The rigidBody is its own scene and is part of a group I called block. The only function it has is this:

add_gravity():    
     gravity_scale = 1

And by default gravity_scale is 0.

But the problem now is that whenever I call that group with

get_tree("Block","add_gravity")

Nothing happens. I put a test print statements into the addgravity function and it does get called, and I also printed the gravityscale property in the process function and after running the addgravity function gravity_scale is set to 1; but the block is still not falling.

And when I set gravity_scale to in the _ready function it works perfectly fine.

Can someone help out?

in Engine by (249 points)

1 Answer

+1 vote
Best answer

First, I assume your code looks like this instead of what you posted above?

get_tree().call_group("Block", "add_gravity")

If so, generally, that should work fine (and does work fine here, in a test project).

Here's a screenshot of my working script and the scene tree. What's different in your case?

enter image description here

by (12,477 points)
selected by

sorry for the duplicate, must have clicked one too many times and Godot does not let me delete it.

And yes, the code looks similar and it does work when I set the gravity scale in the ready part (be it by calling a group or by directly setting gravityscale to 1).

What I am ultimately trying to do is to have a button in another scene that sends a signal and the receiver of that signal calls the group with the blocks and activates gravity.

So my full setup is

For the level scene:

Node (that I named 'Level')
- Button node with a signal connected to the node

and that Level node I have the following code:

func _on_Button_pressed():
    get_tree().call_group("Block","add_gravity")

and then in another scene I have the block with the rigidbody, and on that rigidbody I have the following code:

func add_gravity():
print("gravity")
gravity_scale = 1

When the button is pressed, gravity is printed to the console, so I know the function triggers, and when I print the gravity_scale it is set to 1. But the object is not falling.

Sorry, should have explained the full thing straight from the start; I thought I could keep it a little more isolated. And thank you so much for the help!

So, after I more accurately replicated your setup (based on your most recent description), I saw the same issue you reported.

After some playing, I discovered my issue was that the RigidBody2D was sleeping by the time I changed the gravity_scale value (via a button press). And, while asleep, it doesn't recognize the gravity_scale change.

I was able to fix it here by unchecking the Can Sleep property on the RigidBody2D node in the inspector.

Hopefully, that's your issue too.

Yes, that fixed it! Thank you so much!

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.