What's the best way to have multiple sprites where everything is the same except for their hitboxes?

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

I’m currently following the “Your first game” tutorial, and the way they instruct it, they’d have you assign the same hitbox to every mob despite the fact that one in particular is wildly different.

So first I tried assigning unique collisionshape2d nodes to each animatedsprite node, but then of course I found out that collisionshape2d nodes can only be children of rigidbody2d or area2d nodes.

My second idea was to give each mob its own scene, and that worked fine. I currently have a scene for every mob where they all have their own collision and animations, and that works fine, but it brings a new issue where I can’t just choose a random animatedsprite in the one mob scene because, again, I split the mobs into individual scenes.

So my question is twofold. First, is there a better way to assign multiple sprites different hitboxes while keeping everything else the same?

Second, if I am going down the right path, how can I have Godot randomly pick between the three scenes I’ve created for my mobs?

I can’t answer your first question so I’ll address your second in a comment.
Godot has a built-in random number generator named (unsurprisingly) RandomNumberGenerator. All you need to do is create an instance of it with .new(), and then call .randomize() on the instance once. After that, you can call .randi_range(0, 3) to get a random number. Combine that with a match block to determine which scene is instanced depending on the generated number. You can read more about match here. It’s basically a more elegant, readable version of a long if/else block.

DDoop | 2020-07-19 19:44

:bust_in_silhouette: Reply From: JimArtificer

Create a scene tree like this:

  AnimatedSprite
  |-RigidBody2D
    |-CollisionShape2D

It sounds like you are asking if you can instance the first two nodes and then attach a collision shape to them. You can attach child nodes to instanced scenes.

To randomly pick between some options, use randi_range() and assign a number to each option.