Can I change the gravity_scale of a rigid body in code based off of a condition?

Godot Version

4.2.1

Question

I am creating a 2d platformer game mainly centered around gravity and being able to change it. I have set values that change the players gravity, which are based of of whatever “selected planet” the player has selected. for example, if the have the “moon” option selected it changes the players gravity, but I also want this selection to change physics objects in the scene as well. I have created a rigidbody object which is just a box, and I want to be able to change its gravity_scale based on what the player has selected. for example if the player has “mars” selected, I want the gravity scale to be set to 0.38. I have access to the player, so the box knows what it has selected but no matter what I try it never actually changes the gravity scale once it has been set. here is a bit of the code that i tried to set it with.
var gravity = 1

func _ready():
player = get_tree().get_nodes_in_group(“player_group”)[0]

func _physics_process(delta):
gravity_scale = gravity
if player.current_planet == “earth”:
gravity_scale = 1
if player.current_planet == “jupiter”:
gravity_scale = 2.52

I am simply wondering if what I am trying is possible, and if so, how would I approach this?

Your approach seems solid. I would do it just as you did, but I think it’s a good idea to encapsulate all this gravity logic inside its own class. Then, you can change the gravity based on your logic, and other classes can simply call something like get_gravity_scale().

class_name PhysicsProps

func get_gravity_scale() -> float:
  # your logic
  return gravity_scale

By doing it this way, other classes won’t need to worry about how the gravity is calculated; they can just use it. You can update the gravity every frame or implement a setter that will call every object to update its gravity scale property when it updates.

class_name PhycisProps

var gravity_scale = 10.0 # the setter bellow is for this property
var physics_objects = [] # add your physics objects here

func set_gravity_scale(new_scale) -> void:
  self.gravity_scale = new_scale
  for obj in physics_objects:
    obj.gravity_scale = self.gravity_scale

thank you for the quick reply and sorry foe the late response. I incorporated the code you provided and I have an error saying

“Member “gravity_scale” redefined (original in native class ‘RigidBody2D’)”

I’ve tried but I’m not entirely sure how to fix this.

i think i am just somewhat confused on how to incorporate my code and conditions to what you have suggested.

i am assuming it is becuase the variable was named after an existing property so i wil modify the names and keep playing around.

there are no more errors in the code, but still no matter what i change the gravity scale will simply not change in-game. this is my code right now.

class_name PhycisProps extends RigidBody2D

var player = null

var grav_scale = 1.0
var physics_objects = ["res://engine/objects/box.tscn"] 

func _ready():
	player = get_tree().get_nodes_in_group("player_group")[0]


func get_grav_scale() -> float:
	if player.current_planet == "earth":
		gravity_scale = 1
	if player.current_planet == "jupiter":
		gravity_scale = 2.52
	if player.current_planet == "moon":
		gravity_scale = 0.165
	if player.current_planet == "sun":
		gravity_scale = 27.93
	if player.current_planet == "mars":
		gravity_scale = 0.378
	if player.current_planet == "pluto":
		gravity_scale = 0.063
	print(gravity_scale)
	print(player.current_planet)
	return gravity_scale


func set_grav_scale(new_scale) -> void:
	self.gravity_scale = new_scale
	for obj in physics_objects:
		obj.grav_scale = self.gravity_scale

never mind, thank you for the suggestions, I have figured out the issue with my code and it all works perfectly.