0 votes

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?

in Engine by (14 points)

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.

1 Answer

0 votes

Create a scene tree like this:


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.

by (854 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.