Hey everyone! I'm new to Godot and still quite the beginner to programming, but have messed around a lot with programming over the last 12 years if they explains at all how well I've invested myself into something so fascinating! :) lol
Anyway I was watching a 5 year old video by HeartBeast on YouTube about making a well known game Breakout. I've been improving his code as I go a long because I have often times found he does things the long way that I'd consider inefficient just to be slightly more readable. I respect this, but from a performance standpoint something stood out to me as being a very poor coding decision, but I'm here to find out if I'm wrong and I can see how I might be wrong since the way I thought it maybe should be done, definitely isn't a whole lot better.
HeartBeast Code:
func physics_process(delta): # Originally back in day of video was func fixed_process(delta):
var bodies = get_colliding_bodies()
for body in bodies:
# Further irrelevant code
The code I'm curious if less intensive on processing:
func _physics_process(delta):
if get_colliding_bodies():
for body in get_colliding_bodies():
Further irrelevant code
My thought here is the for loop is gonna try to run even without any colliders detected and so it's gonna find if there are any and then try at least once to run against it. With my code it seems first if there are any colliders detected and then only if there is it moved forward with execution. I also know I have it detecting if there there are any colliding bodies twice rather then setting a variable and then detecting if and that's because there would still be slightly more work for detecting and because collisions are rare why worry? Yes in the moment of the collision more work is being done and I regret that a little.
Now for the REAL question because even the code above can be mixed to be more efficient if say get_colliding_bodies() returning blank would negate even attempting to run the for statement in the below example:
func _physics_process(delta):
for body in get_colliding_bodies():
Further irrelevant code
I was hoping to find something that just tells me if it's colliding with something to use an actual boolean to determine, but I was struggling to find something and got it working with the if statement and it works as expected.