Manage deifferent collision shapes at run time?

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

I want to implement “sliding” in my 2d game. So I wanna change collisions during sliding.
Now I simply create bunch of CollisionPolygon2D nodes and enable them manually in state machine. But this maks player node messy and unmanageable( Cuz all CollisionPolygon2D nodes have to be direct child of that KinematicsBody2D node).
Is there a more elegent way to manange collisions?

:bust_in_silhouette: Reply From: bloodsign

if you want to clean up your Player’s node tree, you can make each CollisionPolygon2D an instance/scene, organize them in a specific folder and even add a script to attach individual properties to it.

Then you load and unload them dynamically in your player scene.

In my case, I created several different CollisionShapes for my vehicle, each attached with their own script that contains different property values.


body00.tscn w/ script:

extends CollisionShape2D
var speed = 100

body01.tscn w/ script:

extends CollisionShape2D
var speed = 150

Then on my players scene, in it’s script, I dynamically load which vehicle body I need, and load their individual properties (such as speed), since they all use the same movement code.

player.tscn w/ script:

extends KinematicBody2D
array = ["res://body00.tscn", "res://body01.tscn", etc...]

func load_body(id)
 var pck = load(array[id])
 var body = pck.instance()
 speed = body.speed

Something like that.

That’s some how inspiring, many thanks!

Smile_Vault13 | 2021-02-14 23:57

glad to help, I honestly just stopped by here to take a break. Building the UI is such a chore, I got burned out a little bit. And yeah, I tend to digress when talking so I’ll leave it at that. Don’t hesitate to ask

bloodsign | 2021-02-15 04:59