Confusion about gravity_scale on a RigidBody2D node

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

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 add_gravity function and it does get called, and I also printed the gravity_scale property in the _process function and after running the add_gravity 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?

:bust_in_silhouette: Reply From: jgodfrey

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

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 gravity_scale 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!

1izNoob | 2020-03-14 19:28

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.

jgodfrey | 2020-03-15 03:26

Yes, that fixed it! Thank you so much!

1izNoob | 2020-03-15 12:52