0 votes

Hello!

In my program there are two moving objects: player's bullets and the monsters.
This is the script that move the monsters:

extends KinematicBody2D

var team="enemies"

# Called when the node enters the scene tree for the first time.
func _ready():
    pass # Replace with function body.


func _process(delta):
    position.x+=4
    if position.x>(get_viewport_rect().size).x:
        queue_free()

and the script of the bullets:

extends KinematicBody2D

var itsspeed = 4
var team="bullets"

func _physics_process(delta):
    var collenemy=move_and_collide(Vector2(itsspeed,0).rotated(rotation))

    if position.x<1 or position.y<1 or position.x>(get_viewport_rect().size).x  or position.y>(get_viewport_rect().size).y:
        queue_free()
    elif (collenemy):
        if collenemy.collider.team=="enemies":
            collenemy.get_collider().queue_free()
            var pk=get_node("/root/Node2D/level1/pointlabel")
            pk.point+=1
            pk.text="Score: "+str(pk.point)
            queue_free()

The first problem when I coded this that when more than one monster are in the screen, the bullets shot down the first enemy but the others push away the bullets.
Besides I couldn't get the properly name of collider with this code:

if collenemy.collider.name=="monster":

Therefore I was forced to use an own variable ("team") to identity the collided objects. It works, but I don't know why push away the enemies the bullets and the bullets each other otherwise?
The second problem is that as if would exist an invisible instance of the enemy monsters on the screen, although I load it from another scene. But when I shoot, the bullets often destroy at the place where the monster exists, but in another scene.
And the Score indicates hit in this case.
Has anyone encountered a similar problem in own games and can help me?

Godot version 3.3 Stable Official Win64
in Engine by (47 points)

1 Answer

0 votes
if collenemy.collider.name=="monster":

Will only work for the first instance of the monster. Godot will name subsequent instances something crazy like @@monster2@@ open the remote debugger while your game is running to see exactly what they're named.

The best way to handle this is to let the engine handle the work for us. Add add_to_group("enemies") in the _ready() function of your enemies. Then your check becomes if collenemy in get_tree().get_nodes_in_group("enemies"):

Another way you could handle this is adding class_name Monster to the top of your enemy script then checking: if collenemy is Monster:. This is a tad more performant. I wanted to show you both ways because they both have their merits.

"The second problem is that as if would exist an invisible instance of the enemy monsters on the screen, although I load it from another scene. But when I shoot, the bullets often destroy at the place where the monster exists, but in another scene."

The only thing I can think is the background is being drawn on top of the enemies maybe? Set the z_index property of your enemies to like 10 and see if you can at least see these enemies at that point.

by (2,834 points)

Thank you, Timothy!
About the second question: I saw whether the background is being drawn on top of an enemy (I switched off its visibility), but there is no enemy node on that scene.
Is there a function, with which I can check the number of a node (the enemies in this case) possibly?

You can use the remote debugger while the game is running to check the states of the objects in the game:
https://docs.godotengine.org/en/stable/tutorials/debug/overview_of_debugging_tools.html#remote-in-scene-dock

And a very well written article about all of Godot's debugging features:
https://www.gdquest.com/tutorial/godot/gdscript/debugging/

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.