Two object with the same script attached interact unexpectedly

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By sombresonge
:warning: Old Version Published before Godot 3 was released.

Hello,

I am trying to learn this engine by doing a simple 2D plateformer. Everything is fine except when I am trying to create ennemies enities.

I created a scene wich cointains the nodes needed for an ennemies:

  • KinematikBody2D
    –Sprite
    – CollisionShape
    – AnimationPlayer

Then I created a script to code the behavior of this entity.

Next I instanciated this scene in the main scene and everything is working fine.

The problem occur when I instanciated a second ennemy from the same scene but at a different position…
When I am interacting with one of them the other is affected too.
It’s looking like they use the same variable of the script.

Here is the script for thoses entities

enum direction { LEFT, RIGHT }

const MAX_H_SPEED = 50

var velocity = Vector2(0, 0)
var oldVelocity = Vector2(0, 0)
var originPos = Vector2(0,0)

const maxPositionOffest = 100

signal collisionDetected()

func _ready():
	var Animation = get_node("AnimationPlayer")
	Animation.play("Walking")
	originPos = get_pos()
	direction = RIGHT
	set_process(true)

func _process(delta):
	velocity = oldVelocity
	var currentPos = get_pos()
	
	if (currentPos.x < originPos.x - maxPositionOffest ):
		direction = RIGHT
		get_node("Sprite").set_flip_h(false)
		velocity = Vector2(0,0)
	elif (currentPos.x > originPos.x + maxPositionOffest ):
		direction = LEFT
		get_node("Sprite").set_flip_h(true)
		velocity = Vector2(0,0)
		
	if(direction == RIGHT):
		velocity = Vector2( MAX_H_SPEED * delta, 0)
	else:
		velocity = Vector2( -MAX_H_SPEED * delta, 0)
	
	move(velocity)
	oldVelocity = velocity
	
	if(is_colliding()) :
		emit_signal("collisionDetected")

When I am interacting with one of them the other is affected too.
It’s looking like they use the same variable of the script.

What do you mean? Are you talking about collisionDetected signal, or what?

Kamil Lewan | 2017-07-04 20:06

Can you show the code where you create instances of your enemy scene?

timoschwarzer | 2017-07-04 20:13

you need to show the “interaction” too, what is connected to the “collisionDetected” signal (it does not seems to specify who emit it or some other data).

eons | 2017-07-04 22:29

I didn’t create the instance of the second ennemy by code, I just add the node to the main scene.

By interaction I mean when the player character collide with the left dude for example It will push it: normal since the 2 of them inherits for a Kinematicbody.
But he will also push the right dude wich is far away from him!

You can see the bug here:
https://youtu.be/ZW2kFAILr2c
From 0 to 16 sec eveything is working"as expected", from 16sec to en the end I reproduced the unexpected behavior

Please find my project on this git repositoy if you want to look into it:
GitHub - Sombresonge/Simple2DPlateformer

sombresonge | 2017-07-05 23:27