Godot not taking values from inspector for @export variables

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

I’m new to the Godot engine and I’m trying to make CharacterBody3D Jump. So wrote below code to have better control on gravity for the jump so that I can make player fall faster.

const SPEED = 5.0
@export var JUMP_HEIGHT = 3
@export var GRAVITY = 20
@export var JUMP_PEEK_TIME : float
@export var JUMP_DECEND_TIME : float

var Jump_Gravity = (2 * JUMP_HEIGHT) / (JUMP_PEEK_TIME * JUMP_PEEK_TIME)
var Fall_Gravity = (2 * JUMP_HEIGHT) / (JUMP_DECEND_TIME * JUMP_DECEND_TIME)
var Jump_Velocity = sqrt(2 * GRAVITY * JUMP_HEIGHT)


# Get the gravity from the project settings to be synced with RigidBody nodes.

func get_gravity():
	
	if velocity.y > 0: 
		print("Jump Gravity:", Jump_Gravity)
		#return Jump_Gravity
	else: 
		print("Fall Gravity:", Fall_Gravity)
		#return Fall_Gravity
	
	return 10

I’m returning a constant value here just for debugging because it initially returned infinite value.

Output:
Jump Gravity:inf
Fall Gravity:inf

looks like both the variables were getting divided by 0, even though I have initialized non zero positive values in inspector (3.5 and 2.5 for jump and fall) .So I initialized the dividend values as 1

@export var JUMP_PEEK_TIME : float = 1
@export var JUMP_DECEND_TIME : float = 1

I got below output:

Output:
Jump Gravity: 6
Fall Gravity: 6

I’m not sure if I’m missing anything here or if godot is not taking inspector values for those variables.

Is there any way I can resolve this? Thanks in advance!

:bust_in_silhouette: Reply From: jgodfrey

I assume this is a timing issue. Since JUMP_HEIGHT, GRAVITY, JUMP_PEAK_TIME, and JUMP_DECEND_TIME are export vars and Jump_Gravity, Fall_Gravity, and Jump_Velocity are just standard vars, I’d guess those last ones are getting initialized BEFORE the export var values are set.

Setting those 3 var references to @onready var seems to fix things…

Hi Mate, Looks like it was as inititalization issue, @onready var worked to resolve it. I guess I need to understand how @onready var works.

Thanks for your help Jgodfrey!

AjayDas | 2023-04-14 12:46