I have an infinite scroller game, where birds come near the one you play as, and in doing so, i have to make them come from the outside of the screen to a position near the player. Since the player is moving, i cannot use a tween to normally interpolate the position value of the coming birds and i was wondering how i could achieve an effect like this, where an node position can be interpolated to another moving position in a certain amount of time
tweens are for creating simple animations for things like UI, not for enemies.
for enemies, put a script on the scene of the enemy and have it find the player and follow it.
an alternative would be to use tween_method, which allows you to run a function, but this will only work on one instance.
put a script on your enemy, this isn’t unity! each node is an object in memory and adding a script is just extending the class by adding functions and variables. there shouldn’t be any performance differences to using a tween. (if that’s the reason you are using a tween for this)
you don’t explain what you are using this for either, so another thing I can think of is this is an effect.
in that case use a particle system.
or maybe this is like a bunch of coins that have to follow the player and be picked up. no reason not to have a script on the coins here either.
I don’t think that would help. I think he wants the thingies to follow the current position of the player. but tween_property takes a variant, and using a Vector2 you pass a copy and not a reference, because that’s how Vector2 works. as_relative as explained in the docs seems to add the values to the property.
Wherever your player happens to be, will be your birds target position. You can then move your birds at whatever speed you choose towards the target position.
Note: It is also worth noting that you should set what to do should your player die and the node not be available. So test if it is valid before using it: Something like:
if is_instance_valid(PlayerNode):
# now you can safely use your PlayerNode reference.
Hope that helps.
PS @jesusemora was correct, you should not be using tweens for actor movements like this. Tweens are amazing and powerful and I use them all the time, for what they were intended for, not for actor movements as you have described.
This is what i was trying to achieve. To do that i actually just used a tween, but i made the bird, once instantiated, a child of the player so that the bird would move with it
I came from unity too. Ok so there could be performance issues as always but what I mean is that a Tween is also an object, that would be linked to your node and run every frame for every node. extending a class on the other hand would in theory just add new methods and variables.
godot already does this for all nodes, they all inherit from Node and each adds properties and methods. If you have a node for an enemy, that is already using memory and is part of the tree, so godot will iterate through every single one and run process, physics process, etc.
I did some quick research, apparently overriding the process method puts the node in a list of nodes that will run process, so overriding it on multiple nodes could lead to some issues maybe probably. Of course we won’t know if it affects performance in any meaningful way without some testing.
so another way could be to have a “commander” node that iterates through a bunch of references to your nodes (in an Array) and updates them. again, this is also premature optimization. test your game with all the “entities” you need and see if FPS is good enough. 2D is very lightweight and scripts more so.
I’ve actually put scripts on bullets to make them move forward and there was no difference to FPS.
I suppose it is fixed then? I should’ve watched the video before posting.
That is a good enough way to do it, but these are not that many nodes, you can’t fit many more birds on the screen, so there would be no difference to performance.