Can you not use delta outside of the process function?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Bleenx
:warning: Old Version Published before Godot 3 was released.

I wanted to create a function specifically for my gravity and use delta within it. After the code, I’d call the function under _fixed_process(delta): but Godot doesn’t seem to recognize delta anywhere outside of the process function.

Is this something I always have to use in the _process function no matter what? I like to keep my code clean with separate functions and call them in the _process, but if I can’t do that with anything pertaining to delta, then I have no choice but to write it all out under that function.

This is my code:

extends KinematicBody2D

export var playerSpeed = 0
export var force = 0 

func _ready():
func _fixed_process(delta):
func Movement():
	var left = Input.is_action_pressed("leftButton")
	var right = Input.is_action_pressed("rightButton")
	if (left):
		move(Vector2(-playerSpeed, 0))
	elif (right):
		move(Vector2(playerSpeed, 0))
		move(Vector2(0, 0))
func Gravity():
	move(Vector2(0, force))

I want to multiply force by delta but just not sure how to do it this way…

Bleenx | 2016-05-15 04:38

:bust_in_silhouette: Reply From: tiernich

Hou about this:

func _fixed_process( delta ):
     Gravity( delta )

func Gravity( deltavalue ):

from Godot docs: “The delta parameter describes the time elapsed (in seconds, as floating point) since the previous call to _process().”

But, let me ask something… What kind of function it will be? why do you need the delta value?

Thanks but I can’t seem to get that to work. I commented on my OP with the code I have so far. Maybe you can see what I’m talking about.

Bleenx | 2016-05-15 04:39

try this:

func _fixed_process( delta ):
    Gravity( delta )

func Gravity( deltavalue ):
    move(Vector2( 0, force * deltavalue ))

but, the gravity options in the editor like mass and gravity scale dont work for your needs?

tiernich | 2016-05-15 04:43

That doesn’t work either. I get this error message: Invalid call to function ‘Gravity’ in base ‘KinematicBody2D (’. Expected 0 arguments.

KinematicBody2D doesn’t have gravity scale and such in the editor. And since most people say use KinematicBody2D instead of a RigidBody2D for a character, I was trying to do it this way.

But yeah, I can’t add a delta value inside the func gravity.

Bleenx | 2016-05-15 04:46

oh, i see, its a KinematicBody2D

see if you dont type nothing wrong, capital letters and such, it’s working here, just tested, i can pass the value delta like is said. Try also change to RigidBody and see if solve your problem.

tiernich | 2016-05-15 04:49

Hmm can you copy the code for me so I can compare and see where I’ve messed up?

Bleenx | 2016-05-15 04:50

func usedelta ( deltavalue ):
    print (deltavalue)

func _fixed_process( delta ):
    usedelta( delta )

this prints 0.01666 infinitely

its a diferent function but the process is the same

tiernich | 2016-05-15 04:55

Thanks for your help, but it doesn’t seem to help me with what I’m trying to do. I appreciate it though

Bleenx | 2016-05-15 04:56

ok, no problem,
i can see by the gravity function that is something like falling from a platform or something, if you try to use a RigidBody2D or Characters for a character with more complexity i think the result will be better, like the doc say: “kinematic bodies are meant to be user-controlled. They are not affected by physics at all…”

tiernich | 2016-05-15 05:02

:bust_in_silhouette: Reply From: Gokudomatic2

I’ll try to explain it to you simply. I hope you don’t mind if it looks a bit like patronizing.

delta is not a magic word that can be used anywhere. It’s a variable that is given as a parameter to the _process function. And it doesn’t have to be delta. It’s the name you give to the parameter, and you can change it. Example:

func _process(aaa):
  print("delta is now aaa and its value is ",aaa)

See? Now it’s not delta anymore but aaa. And if you try to use delta in my example, like:

func _process(aaa):
  print("delta is now aaa and its value is ",aaa)
  print("delta = ",delta)

You’ll get an error because delta is not known.

Second, since this is a parameter, it is only known in the function and not outside. We say that the scope of the parameter is within the function. And that’s the same for any variable you declare in the function. Example:

func _process(delta):
  var my_var1=5
  # delta is known here, and my_var1 too, but not aaa nor my_var2

func _some_function(aaa):
  var my_var2="hello"
  # a is known here, and my_var2 too, but not delta nor my_var1

If you want to have a variable whose scope covers the whole file, you must create a field. Example:

var myfield1="hi"
func _fixed_process(delta):
  print(myfield1) # this works
  myfield="hello" # you can change it too
func some_function():
  print(myfield1) # this works too

Now you can guess that what you seek is to set the delta of the parameter to a field.

var mydelta=0
func _fixed_process(delta):
func some_function():
  print(mydelta) # print the delta of the last fixed_process run

Of course, you can also pass the parameter to another function, like suggested by tiernich.

I understand most of that. My problem is that I thought delta could be used anywhere like Unity’s time.deltaTime, so I could multiply whatever number or variable I create by delta and it would function the same. I do believe I’ve confused the two programs and how they use delta.

Bleenx | 2016-05-15 17:49

I don’t know unity, but I heard it uses C# instead of scripting.
What didn’t you understand?

Gokudomatic2 | 2016-05-15 18:58

In Unity, you don’t have to create a variable for time.deltaTime… it just works anywhere in the code. I thought you could do the same with delta in Godot but you can’t. I just need to get more familiar with Godot and not rely on what I know in Unity. Thanks for the help.

Bleenx | 2016-05-15 19:03

:bust_in_silhouette: Reply From: Arnklit

I know this is an old thread, but since it’s the first search result that shows up and I ended up here myself looking for the answer, I thought I’d answer it here in case anyone else is looking for this since I found it now:

You can call these functions outside _process and _physics_process to get the delta time.


how do you use those in code

like that

getprocessdeltatime( delta )

while i == 0
health = health - damage * delta

i want to limit use of my function to once a second .

fol | 2022-06-05 14:25

The real answer

dezboyle | 2022-11-15 19:47