1
\$\begingroup\$

Previously my items were relative to the player by node hierarchy. But for items like bombs this is not ideal so i tried to add the items to the world scene, but when i try to adjust the position of the item to the players position it doesnt work and i see the sword in the top left corner of the screen the worlds origin.

Entity Script

func use_item(item):
    var newitem = item.instance()
    newitem.own = self
    newitem.global_position = global_position

    #not setting the position of the item correctly

    newitem.add_to_group(str(newitem.get_name(), self))
    get_node("..").add_child(newitem)
    
    if get_tree().get_nodes_in_group(str(newitem.get_name(),self)).size() > newitem.maxamount:
        newitem.queue_free()

Sword Script

func _ready():
    type = own.type
    $anim.connect("animation_finished",self,"destroy")
    $anim.play(str("swing",own.spritedir))
    if own.has_method("state_swing"):
        own.state = "swing"
        
func destroy(animation):
    if own.has_method("state_swing"):
        own.state = "default"
    queue_free()

It seems like it should work but with an engine like godot there could be several reasons why a certain thing is not working.

https://github.com/MonkeyToiletLadder/wendingo

could it be that in my animation player i set the sword positions as key frames and thats why its not working?

\$\endgroup\$
6
  • \$\begingroup\$ Have you tried setting the global position at the end, rather than at the start? The order of parenting and positioning can make a difference, depending on how Godot implements its parenting function. \$\endgroup\$
    – DMGregory
    Commented Jul 9, 2020 at 14:42
  • \$\begingroup\$ I checked. Its not that. \$\endgroup\$
    – dylan
    Commented Jul 9, 2020 at 15:57
  • \$\begingroup\$ I think it has something to do with the animation player setting the position of my sword. I think if i set the area2d and the sprite position offsets instaed of the sword it will work \$\endgroup\$
    – dylan
    Commented Jul 9, 2020 at 16:26
  • \$\begingroup\$ in fact when i set my bombs position to the players position it works. My bombs dont have an animation that moves the bombs position. My players sword position gets set to the animations positions for it but its not relative to its position thatit currently has. \$\endgroup\$
    – dylan
    Commented Jul 9, 2020 at 16:38
  • \$\begingroup\$ Looks like it would be worth editing your question to take into account your latest findings, and ask how to track your player's position with an animated object. \$\endgroup\$
    – DMGregory
    Commented Jul 9, 2020 at 17:25

1 Answer 1

1
\$\begingroup\$

I had a very similar question, it is not quite the same scenario, however I believe it could be applied in the same way. Basically, you should create a new var and set it to the player's position, then create a function that takes into the position. Like this.. 1st) If you dont already have one(Add an animation node) if you do then in your ready function I would connect your animation node

$AnimationPlayer.add_animation(#animationname, #variable)

2nd) Inside your use_item function: Create a variable that is set to your players position, then run a created function using the directions below 3rd) Create a function that takes in that new variable, runs through the position logic and then play the animation(if you need help with this ill post another comment) 4) Implement this function where you need it in your code Hope this helps!

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .