How to create independent instances

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Cherep

Hi,

I’m not sure if thats the correct way to name it, but I have the following problem:
I’m instancing multiple KinematicBody2D’s in a Scene (RigidBody won’t do it for me I suppose). They run through the level by following the script.
However, all instances basically behave as if they were one object, as they all seem to access the same script for obvious reasons.
For example: If one of those KBody2D’s runs against a wall (where the Script would tell them to flip and run in the other direction), all the other instances do the same despite not having run into a wall themselves.

I want every instance that I create to run on its own script, basically, and not care about what any other instance does. Not sure if I’m missing something obvious, but any help would be appreciated.

The code for instancing so far is as follows:

var ix = Item.instance()
cont.add_child(ix)
ix.set_position(spawn_pos)
ix.connect("score", self, "_on_score")
ix.connect("hurt", self, "_on_hurt")

where Item is an onready var (preloaded Scene) and cont = the container node

Item is your KinematicBody2D? Have you tried with Item.new() instead of Item.instance()? However, i think you should show the script that moves the KinematicBody2Ds. Perhaps they are all connected to the same extern signal, and that causes that when one triggers the signal, al functions are executed? How do you detect that a wall was hit and how do you then tell the bodies to flip?

p7f | 2018-12-05 11:36

The signals themselves don’t have anything to do with the flipping. Score/hurt are disconnected from the movement of the KinematicBody, they just check if the player has connected with a collision area.
I think I tried item.new() but I guess it didn’t work, I can test again later though.

What I’m exactly trying to do here is let them fall down a level and whenever they fall a distance greater than 15 pixels, they flip. For this, I’m storing 2 positions: the last position and the current position, in variables, and if new_position.y - old_position.y > 15, they run in the opposite direction.

Just in case, here’s the code for the movement (might not be optimal, but whatever):

func save_positions():
if is_on_floor():
	ground = true
else:
	ground = false
	air = true

if ground == true && air == true:
	air = false
	old_pos = new_pos
	new_pos = get_position()
	if old_pos != Vector2(0,0):
		if new_pos.y - old_pos.y > 15:
			move_spd = -move_spd
		else:
			pass

(I made the bool variables just so that they don’t flip if they only fall down 2 pixels or whatever just because they hit the ground. That alone doesn’t seem to be the problem why they’re synching though, its possibly bc the position variables seem to be stored in only that one script and when it’s true for the first item, they all flip)

Cherep | 2018-12-06 15:42

Okay, just tried it out - new() doesn’t seem to be working on a Packed Scene, so instance is the only way to create them I suppose?

Cherep | 2018-12-06 15:45

you are right, it does not work on packed scene… my bad, i missread. I don’t think it’s a problem of being only one script, because every instance of an object should hold it’s own variables although using same code, thats the point of OOP. Can you share the tree structure of the scene where all instance are being created? Or may be share the whole project so we can see in a deeper context? It’s very strange that every instance behave the same.

Just to be sure if i understand correctly. You made a scene with root node KinematicBody2D and added the movement script to it. Then you have another scene which is the level or something, and instance your previous scene there as packed scene. Am i right?

p7f | 2018-12-06 16:07

Yes, you are correct. My item is a KB2D, it has a general script for the movement, it’s being instanced in my level later, but in that level, every item flips if even only one of them fulfills the conditions to flip.

Uploading the project is a bit difficult for me, but I can try as best as I can to tell you how my level works (and btw, thanks that you’re trying to help!):

I have a main Game scene/level, root node is a normal Node2D, with a script attached to it (where the items are being instanced in by code, the stuff you can read in the first post). The instancing itself does work, the instances just behave all the same.
The structure is… a bit messy, I will try to clear that up later, but for now:
RootNode2D (Game)
as a direct child:
ItemContainer (simple Node2D, this one has no children, no script, nothing - this is where my items are instanced in when they’re created)

then, as other children of my root node 2D, a whole lotta other Nodes2D that hold stuff like my Sprites, my platform-collisions, my player etc.

I get the ItemCon, the spawn etc by creating an onready var on the main script and the get_node() function.
onready var Item = preload(“res://Scenes/Item.tscn”)
onready var cont = get_node(“ItemCon”)

(here as a picture, pls dont judge: https://i.gyazo.com/2f9b1361c9918a38038d7e86d0eba5e0.png
I can add some more stuff if needed later, but that’s… all about it, actually. All the code that concerns the item is here + the structure now. Everything else is fine so far, but the items behave like clones)

Cherep | 2018-12-06 16:34

Hi, i think the concept is fine. I created a small project myself and my instances do not behave like clones. I don’t know if i can be any help without seeing whole project :frowning: Perhaps someone with more experience can.

p7f | 2018-12-06 17:46

If the code itself doesn’t seem to be the problem, I’ll maybe create the scene again later and code that stuff again, possibly its just a bug or it didn’t connect right.
I’ll test around in a bit, if nothing helps I’ll either try to solve it in a different way or maybe find a possibility to upload the whole thing.
In any case, thank you for trying!

Cherep | 2018-12-06 18:28

Okay, once again - I think I can (and will need to) find a way around this, but still, I at least wanted to upload a test version of it if you still wanna take a look at it.
It’s not the project itself, but a test with a KinematicBody with the exact same script as in my real project and the basic graphics/collision stuff. Maybe you can work with that? Just run the test.tscn or whatever, it even behaves like that if I dont instance it from code, I put 2 instances in through the editor and it still copies the other body.

Dropbox - test.pck - Simplify your life

pck

Dropbox - test.zip - Simplify your life

zip

Cherep | 2018-12-07 09:13

nevermind I’m stupid and I dont know how to edit that, these should be the project files:

Dropbox - test.zip - Simplify your life

Cherep | 2018-12-07 11:15

You probably used some sort of signals and groups, which probably caused an error

shield | 2018-12-08 19:36