"Fishy": Randomising enemy size & conditional logic

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

Hi guys! I am still new to Godot, and actually to programming in general. I’ve done several Godot tutorials and now I am trying to check on my progress by recreating the game “Fishy,” where your fish eats smaller fish to grow, and gets eaten by larger fish.

I’ve managed to create a player sprite that moves the way I want it to, and enemies that spawn from random spots at a time interval and get eaten by the player. The problem is, they are all the same size, and they are all eaten by the player.

I’m pretty stuck on this one part (arguably, the main part…) where the enemy’s fish size is randomized on spawn, and a smaller fish than the player is eaten, while a larger fish than the player eats. I’ve been trying different if, else statements, as well as playing with .scale, and everything I’ve tentatively messed around with so far has broken the game.

I am wondering if someone would let me know if a) it seems like I am coming at the problem from the wrong direction (should I have a different scene for several different types of fish instead of one Enemy and one EnemySpawner node?) or b) which terms I could search for to increase my understanding. If there’s a tutorial that handles anything similar to this, I’d be very interested!

Thanks so much, from a newbie.

:bust_in_silhouette: Reply From: jgodfrey

While maybe not the best solution, as a beginner-friendly approach, you could probably just:

  • Randomly set the scale value of the enemy fish when you spawn them. A value of < 1 will make it smaller and a value of > 1 will make it larger. Just randomly select a scale value between 0.5 and 1.5 (for example).
  • When your fish “eats” another fish, compare the scale values of your fish to that of the fish you’re eating. If its scale is larger than yours, you’ve been eaten. Otherwise, you’ve eaten the other fish.

Thanks so much for providing your answers, they’re invaluable. This is where I’ve been coming from, logic-wise. The less beginner-friendly way, would that be to have several enemy nodes for larger and smaller fish, or something else, out of curiosity?

_vindicator | 2023-01-11 18:48

It’s hard to say what a less-beginner friendly way might be. It just depends on what your requirements are. What I mentioned above is just the simplest thing that came to mind to handle the basic requirements of this kind of game. And, really, there’s nothing wrong with simple if it gets the job done.

For this game, it seems that the “eat or be eaten” logic is purely “size” based. In that case, the scale is a reasonable way to make that decision. However, that only works if you’re using the same “base” size for all of the fish. If you were to create different kinds of fish, you might make their sprites larger or smaller by default - outside of the scale value. In that case, two fish of differing “visual” sizes might both have a scale value of 1. So, in that case, scale comparison would no longer work for the main decision and you’d have to devise some other method.

That could be a simple as adding a property to the script attached to each fish that contains a value that somehow represents “size” without involving the actual scale property. In that case, the logic would be similar, but you’d just use a different value for comparison.

Regarding having several enemy nodes… Again, that depends on the expectations. Doing so would allow you to easily create fish that look different, and potentially even have differing behaviors based on their attached scripts.

Again, it’s all a matter of what you’re trying to achieve.

If you get stuck trying to work through the basics, feel free to post some relevant code for some additional input from the community.

jgodfrey | 2023-01-11 19:17