Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KinematicCollision2D is overriden even though the variable isn't reassigned #36290

Closed
Tracked by #45334
KoBeWi opened this issue Feb 17, 2020 · 3 comments · Fixed by #52954
Closed
Tracked by #45334

KinematicCollision2D is overriden even though the variable isn't reassigned #36290

KoBeWi opened this issue Feb 17, 2020 · 3 comments · Fixed by #52954

Comments

@KoBeWi
Copy link
Member

KoBeWi commented Feb 17, 2020

Godot version:
3.2 and 7724b84

Issue description:
Look at this code:

extends KinematicBody2D

var global_col: KinematicCollision2D

var dir := Vector2.RIGHT * 2

func _physics_process(delta: float) -> void:
	var col := move_and_collide(dir)
	if col:
		dir.x = -dir.x
		if not global_col:
			print("------------------------")
			global_col = col
	
	if global_col:
		print(global_col.collider.name)

You can clearly see that global_col is assigned only once (------ indicates it) and from a local variable. However every time col gets a new value, global_col is modified too. AFAIK that's not how references work.

Minimal reproduction project:
TestProject.zip

EDIT:
Ok, for whatever reason KinematicBody2D reuses and returns the same collision every time. Any reason it's like this? It should at least return a duplicate.

@Xrayez
Copy link
Contributor

Xrayez commented Feb 17, 2020

You mean that whatever is returned by move_and_collide() is the same internal reference to KinematicCollision2D somewhere within KinematicBody2D? If that's the case perhaps that's due to performance reasons.

It would be nice to have KinematicCollision2D as a property then, or adding a method which can retrieve this reference outside of the move_and_collide() call, something akin to get_collision() similarly to get_slide_collision(), and perhaps is_colliding() as in a raycast.

@KoBeWi
Copy link
Member Author

KoBeWi commented Feb 17, 2020

You mean that whatever is returned by move_and_collide() is the same internal reference to KinematicCollision2D somewhere within KinematicBody2D?

Yes. At first I thought it's some bug related to references, but the returned collision is the same object actually.

If that's the case perhaps that's due to performance reasons.

The collision is a simple struct, so I doubt it's about performance. It might just be needed somewhere else, but still, the returned collision should be unique. So duplicating it on demand would be a good idea IMO.

@KoBeWi
Copy link
Member Author

KoBeWi commented Dec 21, 2020

Still valid in bccbd4b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants