How would I make a random spawning system

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

Hello, this is quite a long one but I am pretty stuck.

I am making a tank game. The point of the game is to destroy waves of AI tanks, which will all have random tiers of “modules”. Each tank has 5 modules (belt, armor, barrel, shell, engine) and each module has tiers, for example a better tier engine will make it go faster.

The player will spawn, and it will be just like every other tank. Your tank will start out with only the first tier of every module, and all other tanks will also have only the first tier. The moment you buy a better tier module (for example better armor), all new enemy tanks will have a chance to spawn with that better armor as well.

The problem is, how do I do this? Currently, I have a scene for every single tier of every module, and each one has a script with a variable (for example armor 3 has the variable health = 300).
(each tier is the same thing but with a different sprite and variable)

I was thinking that you can simply have a tank scene with the scripts for AI and health and everything, but it will randomly choose a module scene to load. The problem is, I feel like the whole thing is super messy. If I load a Armor4 module, then to access the health amount I can’t just read the variable from $, I would have to add a 4 to it because of the name of the scene ($ Firstly, I don’t know how to add that 4 there, secondly, doing this each time seems really messy, lastly, it would work terribly with the player, a tank that changes these tiers because you buy them.

This is my attempt at making the enemy tank scene load a random tier. So in the first node of the scene which controls the entire game I specified the current tier of the Armor module, and each time a tank scene gets spawned, I want it to randomly choose from 1 to the current best tier. Firstly, it doesn’t work, but more importantly, the entire thing just seems… messy. Like I’m doing it the worst way imaginable.

I know it all is badly explained but if you understand my concept, please share with me some ideas of how would you go about making this. If you have any questions, ask me. I’m pretty new to Godot and not a good coder (I’m guessing it’s pretty obvious), and I’m really baffled because what I thought was a simple idea to code turned into a nightmare.

:bust_in_silhouette: Reply From: Inces

Base idea of naming scenes in one convention with different numbered affixes is good and frequently used, as this allows to dynamically create nodepaths from strings and calculated integers. You took nice advantage of that. However if these scenes differ only in value of variable, than there exist more ellegant sollutions :). For example one class/inherited scene for all modules sharing the same variables, calculating values depending on level using static function, or - if values are not easily calculated - using external data chart, like dictionary inside Resource.

I understand that with your current code structure, your worst problem is, that You have to instance entire scene just to reach the data about change of health. This should be inversed, data should be in highest scope and act as a blueprint for instancing a scene. I advise to create a dictionary in Your top node/Autoload somewhat like this :
var moduledata = {"Armor" : {1:0,2:100,3:300,4:1000},"Attack":{1: and so on
There are also possibilities to encrypt it in Excel table chart and make Godot use it, but it is a bit of a hassle

And when You would instance a Tank in code, You could preload one naked base tank scene or You could create everything from a class script, like, and use this dictionary to set variables of created tank, like : += $Topnode.moduledata["Armor"][rng.randi_range(1,maxtier)]
Tank.attack += and so on