0
\$\begingroup\$

I'm just starting out in Godot 4. I followed this tutorial on how to create my first game, and I'd like to add some new features to make it more interesting, and start coding some stuff of my own.

I'd like to create different colour mobs, and have the player's score increase with the number of mobs of the correct colour they collect (the correct colour being defined as the player launches the game). I've recoloured the animated sprites from the tutorial, now I'd need to create a scene for each colour of mob (I'll probably be adding some more stuff depending on the mob's colour later, so I prefer having them distinct rather than choosing a sprite randomly).

To do this, should I extend the current mob scene? Is that something that's possible?

Or should I copy + paste the existing mob scene to have the same starter functions and properties for each one, and then customise the individual scenes after?

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

I would just programmatically change the color at random when they are spawned by assigning a random sprite. You can then, when they are "collected" either test to see what sprite they are using, or set a variable on spawn that contains their color. No need to get fancy.

If you wanted to create an instance of each color you could. But unless they have different stats, there's no real need to do that. If you did that, you'd just randomly select from a list of the scenes you've created. Put this in your main.gd:

@export var mobs: Array[RigidBody2D]

Drop all the possible enemy types on them. Then change the first line in func _on_mob_timer_timeout():

# Create a new instance of the Mob scene.
var mob = mobs.pick_random().instantiate()

You'll need to know who you captured, but that's it. Just do something like

match mob.get_name():
  "blue":
    #give 1 point
  "red":
    #give 2 points
  _: #any other type/default option
    #die
\$\endgroup\$
2
  • \$\begingroup\$ That's certainly one possible approach. How do you link the colour tag to the colour of the actual sprite though? \$\endgroup\$
    – Whitehot
    Commented Jul 24 at 11:09
  • \$\begingroup\$ Just create a scene for each type and name the root node "blue", "red", "green", etc. \$\endgroup\$ Commented Jul 24 at 21:55
1
\$\begingroup\$

Short Answer: Use instancing

Long Answer: In Godot 4 you can use instancing method.

Docs For Instance: https://docs.godotengine.org/en/stable/tutorials/scripting/nodes_and_scene_instances.html

YouTube Tutorial For Instance: https://youtu.be/Qs8oSGmhx-U?si=1mPSshbLqOCjoQup

and then code so that everytime it spawns a different mob scene or it uses a random value everytime a new mob is spawned. This can be a string, color or anything. You can also add a score system using the Label Node and make it increment the score value everytime it spawns a new mob.

Docs For Scoreboard: https://docs.godotengine.org/en/stable/getting_started/first_3d_game/08.score_and_replay.html

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .