+1 vote

I have a function _CharacterControl and _ApplyGravity that are called every frame being:

func _process(delta):
    _CharacterControl(delta, _GetInputAxis())

func _CharacterControl(delta, inputvector):
    var velocity = Vector3()
    velocity -= _ApplyGravity(delta, gravity)
    velocity = move_and_slide(velocity, Vector3.UP, true, 1, deg2rad(45), false)

func _ApplyGravity(delta, gravity):
var gr_add = 0
if !is_on_floor():
    gr_add = gravity
else:
    gr_add = 0.1
return Vector3.UP * gr_add * delta

The character just floats in the air.
It's made that way because I wanted to not initialise the variables globally, just inside the functions where they are used, no matter what I do I can't get this code to work, I have however already done it the other way being:

var velocity = Vector3()

func _process(delta):
    _CharacterControl(delta, _GetInputAxis())

func _CharacterControl(delta, inputvector):
    _ApplyGravity(delta, gravity)
    velocity = move_and_slide(velocity, Vector3.UP, true, 1, deg2rad(45), false)

func _ApplyGravity(delta, gravity):
var gr_add = 0
if !is_on_floor():
    gr_add = gravity
else:
    gr_add = 0.1
velocity -= Vector3.UP * gr_add * delta

I have not given any other pieces of code because they work fine and the problem remains without them. Every other thing that I tried failed in the same way so this chunk of code and the output method are surely the things that are responsible.
The only way this works is when I change the velocity vector inside of the gravity function but it doesn't work if I calculate only the velocity delta inside of it and then add it to the velocity vector outside wich is exactly what I need to do as I can't use local variables of another function without using them as arguments for a function, I really don't want to litter the list of global variables with ones that are only used in one function, that's why I'm even doing this whole thing, it doesn't matter where I create the variable though, it still doesn't work with the first approach and works with the second.
I can not get the first variant to work while the second works flawlessly despite them being absolutely equivalent to each other. I tried many different ways to do this, print(_ApplyGravity(delta, gravity)) in the first case and print(Vector3.UP * gr_add * delta) in the second give the same result, is_on_floor is working correctly, all the values match up when using print() yet the character moves perfectly with the second approach and almost doesn't move with the first, the velocity doesn't exceed gravity * delta, I have been sitting there for hours changing the order of operations, rewriting everything from scratch more than 10 times, initialising variables in different places, nothing works, I tried just copying the working piece of code and changing the output method to the one needed and not changing the way it works basically completely killing the whole purpose of this function and it still didn't work because of the piece of it that shouldn't even matter, I've given up on trying to make sense out of it, I beg someone to please tell me what even is the difference there and how in hell are those two ways of doing this not equivalent to each other. I'm pulling my hair out and I know that in the end it will be because of something so stupid and simple and that I just didn't know about.

I'm sorry for my english and for that this whole question is so messy, I hope it isn't much of a bother, I'm just so tired and frustrated, it might have even just been solved by a break and a bit of sleep but I'm too angry to go to sleep without doing everything I could.

edit:
It turned out to be simpler but at the same time more confusing/
this works:

func _process(delta)
   _CharacterControl(delta, inputvector, velocity)

func _CharacterControl(delta, inputvector, velocity):
    _ApplyGravity(delta, gravity)

func _ApplyGravity(delta, gravity):
    velocity -= Vector3.UP * 9.8* delta

and this doesn't:

func _process(delta)
    _CharacterControl(delta, inputvector, velocity)

func _CharacterControl(delta, inputvector, velocity):
    velocity -= Vector3.DOWN * 9.8 * delta

that's it, my computer is haunted, I quit gamedev and any programming in general for good if this doesn't have an explanation, this is just ridiculous

anoher edit:
yes, it's even worse, I just tried and it turns out that everything works fine when I call the gravity function in _process, but it still doesn't work when I call it inside of _CharacterControl just as I described previously, and _CharacterControl is in _process too, there is literally nothing going on other than functions being called inside of other functions, I removed the rest of the code and all the other scripts in the scene, this is just _process that contains _CharacterControl that contains _ApplyGravity, and somehow it gives different results depending on where I change the velocity vector, I can't even see the pattern there

Godot version 3.4.3
in Engine by (16 points)
edited by

1 Answer

+1 vote
Best answer

Well, this question really is messy... :)

Is it about gravity not being calculated properly when using high scope variable, in comparison to local variable ?
You should always use local variables for things like these. Only with local variable You can be sure, that it is set and get every time its function is called. High scope variable, on the other hand, can become unstable when mulitple function calls in the same frame occur, even being changed inbetween code lines of function ! Generally, if You want to use high scope variable, You have to make sure, that its corresponding function is not being called too many times or by too many sources. I imagine Your gravity is not calculated properly due to function being in process and velocity being affected by all this timing...

..that is if I even understood sense of this question :P

by (5,651 points)
selected by

I am sorry for that my question is such a mess, also for that I am commenting so long after you posted the answer.
Thank you for answering, though this isn't quite what I was trying to ask, I couldn't blame you even if my question was formulated correctly because I genuinely couldn't think of anything that could cause this, after a couple of iterations of changing the code and learning new things about how this works I didn't even have any idea as to what to name this question as I couldn't see the pattern of when it does and when it doesn't work.
I just wasted a couple of hours testing how things work and if I could fix it just to write a more in depth explanation to you and it was a couple of thousands of characters long but then I tried to give unique names to each argument in all of my functions to see if it would change anything, it worked as if by magic and it felt like nothing else does in life, I didn't bother doing that before and it never really broke anything, so I assumed that this won't be a problem now, I still don't know how would that break anything as the problem didn't seem to have anything to do with wrong values.
As I said:

...I know that in the end it will be because of something so stupid and simple and that I just didn't know about.

You helped me so much, not by giving an answer but rather by making me go through that code a thousand more times, maybe even changed my life, I'm so glad I finally went to check if my question got any answers, I was doing basically nothing with my life for those past 5 days, with college stuff and looking after my little sister I can only work late at night and I sleep around 5-6 hours even when not working so I am completely drained and the only thing that kept me doing it was just pure motivation, and when I stuck with this thing it all went away, I am sorry that I told you all of that, I just want you to understand how much you've helped me, I might never have returned to the project without you.

...it might have even just been solved by a break and a bit of sleep...

Now I am going to sleep satisfied with that I finally made progress thanks to you. I can now move on to more advanced stuff that I was already doing before I decided to clean up my code, you saved weeks if not months of my time as I can only work a couple of hours a day if that, often none at all.

If you're interested I will share my progress with you when I make it and even share some of the money that my games make if that will ever happen.
Thank you so much. Now if you don't mind me asking can you tell me if I need to do something with the question after it got resolved, I don't want to break rules and really just people seeing this embarassment at all. I am sorry for my english, might be really bad at times and I am very tired right now.

Wow man, thanks, but You definetely overestimated my help :)
I would like to say " good job, keept it up", but I don't want to overfill your already huge determination ;) Instead I will honestly advise to take a break sometimes, to relax over some brainless entertainment. Many times I was stuck like this in coding and forcing myself to sit and stare into code all the time just made everything worse. Full day of guilty pleasures, good sleep, and as I was waking up next day all good sollutions started coming to my head in the morning.
Good luck :)

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 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 webmaster@godotengine.org with your username.