Godot Version
4.2.2
Question
How can I change the gravity of the project settings via code?
Going to project settings, physics, 3D I can change the default gravity that the game and engine use
This works perfectly. The player and other objects affected by gravity fall faster when I increase it.
If I hover the mouse on the settings I get this message:
I need to change the gravity in code, so I reset the gravity in the settings menu and then put this line:
PhysicsServer3D.area_set_param(get_viewport().find_world_3d().space,PhysicsServer3D.AREA_PARAM_GRAVITY,20)
Inside the ready function of my game and nothing happened.
There’s no error, but there’s no change in gravity.
Is that line of code correct? Where do I have to put it to be able to change the gravity in code?
Thank you!
It should work. The problem is that since your game-scene’s-ready() method gets called after all children gets called ready and if these children have the code:
var gravity = project...
then they set the gravity before your game-scene changes it
Did you use the new gravity? What I mean is, if you’re using CharcterBody3D
, you’d probably write down var gravity := ProjectSettings.get_setting("physics/3d/default_gravity")
which is before you modify it.
herrspaten:
It should work. The problem is that since your game-scene’s-ready() method gets called after all children gets called ready and if these children have the code:
var gravity = project...
then they set the gravity before your game-scene changes it
Yes, that makes sense, but then how do I make that my gravity loads the first thing in my game?
You either set it before you load the scene or you have to tell every child to load the gravity again
How can I set a value before a scene is loaded?
By having a scene that runs before this scene and that changes the value