This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+1 vote

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.

in Engine by (32 points)

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...

3 Answers

0 votes

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?

by (429 points)
edited by

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.

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?

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.

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.

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

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

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

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..."

+2 votes

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.

by (703 points)

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.

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

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.

+13 votes

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 _physicsprocess to get the delta time.


by (34 points)

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 .

The real answer

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.