Idea is, there are 3 scenes - player, a coin, and a main scene. At the ready() of the main scene (just a sprite with a camera) I create one instance of a player scene and one instance of a coin scene at random locations. Player emit "hit" signal when it touches the coin, which is connected to the main scene with a collable "jump", that should move the coin to a random location. An it works, but only once. Coin jumps, but touching it again does nothing. Only touching the spot where it WAS at the start. It looks like the contact area just stays where the coin spawned. Coin has no script, it is just a sprite with a rigid body and an area, and player has an "enter rigid body" signal to react to it. What am I doing wrong?`
extends Sprite2D
var xpos = 0
var ypos = 0
var coin
var rnd = RandomNumberGenerator.new()
var player
var scene

Called when the node enters the scene tree for the first time.

func _ready():
rnd.randomize()
xpos = rnd.randf()170
ypos = rnd.randf()
110
scene = load("res://player.tscn")
player = scene.instantiate()
player.position = Vector2(xpos-85,ypos-55)
add_child(player)
xpos = rnd.randf()170
ypos = rnd.randf()
110
scene = load("res://coin.tscn")
coin = scene.instantiate()
coin.position = Vector2(xpos-85,ypos-55)
add_child(coin)
player.connect("hit", _jump)

func _jump():
xpos = rnd.randf()170
ypos = rnd.randf()
110
coin.position = Vector2(xpos-85,ypos-55)

Called every frame. 'delta' is the elapsed time since the previous frame.

func _process(delta):
pass`

  • xyz replied to this.

    Shrimp Format your code properly using``` tags. Post the scene structure as well.

      xyz I did fomat it, no idea why it didn't show up.

      • xyz replied to this.

        xyz
        ...
        func _on_body_entered(body):
        fill += 10
        pbar.set_value(fill)
        emit_signal("hit")

        ...
        Apart from the controls, nothing spatial it just emits the signal.

        I've been told it's because the coin is a rigid body, and I can't move it other then with an impulse or a force. And I can apply impulse for it to move, not teleport though, so it's seems to be the case. Do I have to change it for something else and the signal for the player? Or can I move the 2DCollisionShape of the coin instead, make it coordinates the same as the sprites's each frame? It is technically not the rigid body, so it can be changed, right?

        • xyz replied to this.

          xyz not really, no, I just didn't know it was a thing what so ever, and now all the signal are tied to it, so if I change it I will have to change all the scripts with collisions... not a big deal, yes, but still, plus, I don't know if other types have their own "fun" conditions like hat.

          • xyz replied to this.

            Shrimp For collectables like coins an Area2D node is typically used.

              xyz it worked with "StaticBody2D" too. I guess the lesson here is "do not mess with physics if you do need any physics"... thanks.

                Shrimp StaticBody will block other physics objects (if the collision layer/mask is defined that way).
                For things that should only detect overlapping bodies and not block them Area2D is the way to go. Pickups like coins, health, areas that should trigger events or deal damage etc.