Need a bit of help

Godot Version



how would i define a specific point for an object to move to and back

Explain what you want

i want the one i circled to run up to whichever one it chooses (i prob wont need help with rng) and attack, and then run back

mario and luigi style lol

You’ll need to define a position by each enemy, then use a tween to put the player at that location. Adding a Marker2D child for instance

func move_to_enemy(enemy: Node2D) -> void:
    var end_point: Vector2 = enemy.get_node("Marker2D").global_position
    var tween := create_tween().set_trans(Tween.TRANS_QUAD)
    tween.tween_property(self, "global_position", end_point)

moving them back will be a similar function, using a variable to keep track of their original position instead of end_point

i tried making a script and adding this code but it says the last line needs four arguments

1 Like

Sorry, the last argument it wants is duration in seconds, how long it takes to complete the tween.

ah mb, lemme test it

when i call the move to enemy function what would i put as the argument it needs?

The enemy to move towards. This enemy will have to have a Marker2D child for positioning in the example, did you change that line or are you prepared for that?

im not sure, here’s what i did with your code

It would move to the random target, if it can find them. Maybe you need to add your player’s to a group so it can select via

var player_target = get_tree().get_nodes_in_group("Players").pick_random()

Then your players need the same Marker2D name

okey lemme try it

its saying “cannot call method ‘get_nodes_in_group’ on a null value”

Everything is wrong there. move_to_enemy must be handled every frame. it has to be alive. for that it must be in the process(delta). get_node must be “SpotPH/spotter”. it must start from the top of the tree to the targeted node. and “global_position” will not be found in the code. you must write a separate code to fix this position to. like a coordinate or another marker.

you can also find a way to record the current position of the sprite and use it later.

Can’t use get_tree() before a node is ready. My example was for the ready function. Make sure your players are in a group named “targets”

func _ready() -> void:
    var player_target = get_tree().get_nodes_in_group("Players").pick_random()
  1. Tweens do not, and should not be created every frame
  2. the supplied enemy: Node2D will be “SpotPH” or “KyranPH” so searching it’s children for “spotter” (or now “targets”) is correct
  3. global_position is a valid Node2D property
  4. The child Marker2D is to be used as a coordinate reference, or a marker as you describe.

1 Like

oh wait shit the error is “invalid get index ‘global_position’ (on base : ‘null instance’)”

1 Like

Cool, you put the group on the markers themselves. This isn’t a bad idea, in fact probably better than what I recommended. The problem now is it’s trying to get a child that neither have (hence “null instance”). You can remove the get_node("Marker2D") for a shorter enemy.global_position

1 Like