there is a lot to unpack here.
that sounds more like you using arrays wrong, and then instead of fixing the issue switching to lambdas for no reason.
the problem with that sounds very simple, you are doing is_instance_valid(plat)
and at the same time calling a variable inside of plat.
have you tried:
if plat:
if plat.grid_position == pos:
return plat
but also this looks like redundant code since it’s so simple it should not be its own function.
also you are deleting a node but not removing it from the array, resulting in an empty element that causes problems.
you are supposed to remove the element from the array first and then use the temp to delete the node.
and don’t erase elements from an array while iterating through it. that’s why in my case I created a second array to queue elements that have to be deleted.
but also also you want to check if pos
is equal inside an element of array
. array
is a basic data type, I suggest you read a book or two on data structures, because there are better ways to handle this.
one would be by using a dictionary to store the positions with references to the nodes
var installed_platforms : Dictionary = {}
var plat : Platform = installed_platforms.get(pos, null)
if plat:
#do whatever with the plat
pass
well I don’t know what you are trying to achieve, if these things can move, but I can tell you two things:
1 - there is a billion different ways to do things and they depend on the features needed. so narrow those down and choose one.
2 - there is a reason why games have constraints, it is to make life easier. one example would be using a grid and having all elements the same size. another would be having platforms that can’t move or occupy the same space, so you can store them by position as in the example.
this is, again, premature optimization. stop.
make a prototype. if there are performance issues, you look into optimization. not the other way around.
don’t consider performance.
also there’s no difference, even if one node can hang and slow down the game, there are ways to fix it without having to rewrite everything.
I know because I did it, not in the best way but I did it.
I used await
to pause the execution of code in the node every N “steps”, it then resumed in the next frame. as a result, I was running complex algorithms in the node while the game ran smooth and animations continued.
func eval_move() -> bool:
if moves_evaluated > max_moves:
moves_evaluated = 0
return true
else:
moves_evaluated += 1
return false
func evaluate_next_move(body : ControllableUnit, enemies : Array[ControllableUnit], ai_orders : Dictionary) -> void:
...
for h in enemy_moves[k]:
if eval_move():
await body.next_frame#wait for next frame
signal next_frame
var is_selected : bool = false
func _physics_process(_delta):
if is_selected:#only works with AI_turn
next_frame.emit()
this is just an example of how I optimized the code AFTER making something that was working.
it IS very much the case in game dev, it’s just common sense. don’t call a function thousands of times per frame, unless it’s something very simple or has the proper conditionals to prevent code from being executed more than it should.
I honestly don’t understand how you can get performance issues with this. As I said before, don’t call get_children()
in process, prepare an array or something with the nodes that must be removed and remove them when a condition is met like collision with an Area3D.
if this is something like a preview for building, it’s the same thing, but you have a start where things are placed, and a condition which could be the mouse moved, when something is recalculated, and you don’t remove the preview blocks you keep them there and move them.
or a main node with the previews that holds the other blocks and that moves to the position of the mouse every physics frame, and if there is a collision with area3D for connection you enter a connected/disconnected state and use a different way of calculating the final position.
finally, don’t do this. this code is confusing and has no reason to be.
just make a prioritize function.
use inheritance, create a main named class and inherit from it and reuse the common functions.
I would say if you can’t fix the performance issues in gdscript, there is something wrong with the way you are doing things and won’t be magically fixed by moving to C++.