Hi! In my game, I have items called “Modules” that need to be randomly generated. These randomly generated module items each need to contain 1-3 stats, chosen from a list of 8 total stats, then be able to pass these stats onto the player.
The code I currently have for generating new modules, which for now just generates a module that grants 1-3 Damage, is the following, located within the script that handles the inventory system.
public ModuleCard cardPrefab;
public AudioSource audioSource;
public AudioClip moduleCreateSound;
[ContextMenu("Create Random Stat Module")]
public void CreateCommonStatModule()
{
ModuleCard newCard = Instantiate(cardPrefab, transform);
Module newModule = new Module()
{
damage = UnityEngine.Random.Range(0, 3) + 1
};
newCard.setStatDisplay(newModule);
// Play sound
audioSource.PlayOneShot(moduleCreateSound);
}
Using this as an example, I'm essentially trying to figure out a way to only add the Damage stat to the generated module if the function picked Damage as one of the stats from the list of 8 possible stats. Once I figure that out, it should be pretty easy to implement picking, generating and adding the other 7 stats.
The “Module” class that this uses simply contains the variables for the possible stats, as follows:
public class Module
{
[Header("Turret Stats")]
public float range;
public float fireDelay;
public int bulletCount;
public float spreadAngle;
[Header("Bullet Stats")]
public float shotSpeed;
public int damage;
public int pierce;
public float bulletLifetime;
// Special stats (Still passed to bullet)
public bool isExplosive;
public float explosionSize;
public bool isHoming;
public float homingStrength;
public bool givesBurn;
public float burnTime;
}
Additionally, I have a ModuleCard class that handles the displaying of these stats on a card.
public class ModuleCard : MonoBehaviour, IPointerClickHandler
{
//Use an array of TMP_Text later to organize the stat descriptions;
public TMP_Text upgradeText;
public Module module;
private GameObject Turret;
TurretScript script;
private void Start()
{
Turret = GameObject.FindWithTag("Turret");
script = Turret.GetComponent<TurretScript>();
}
public void setStatDisplay(Module _module)
{
module = _module;
// This needs to check the module created by the Conveyor Inventory for stats that it includes, and then only include those stats in its description
upgradeText.text = $"<sprite=0>+{module.damage}";
}
public void OnPointerClick(PointerEventData eventData)
{
script.AddModule(module);
Destroy(gameObject);
}
}
This will need to be able to receive information from the CreateCommonStatModule() function about which stats are included in the module, and change the upgradeText.Text to match the included stats accordingly.
I’ve been struggling with this for a few hours, and can’t seem to find anything online that generates stats for items in this way. Any help is greatly appreciated!
Generating stats is just calling the random number generator with specific inputs and then recording the result somewhere.
ScriptableObject usage in RPGs: (note: character stats are identical to any other weapon or item stat… it’s just a number…
If you have a system and it is misbehaving, that just means you wrote a bug… time to start debugging!
By debugging you can find out exactly what your program is doing so you can fix it.
Use the above techniques to get the information you need in order to reason about what the problem is.
You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.
Once you understand what the problem is, you may begin to reason about a solution to the problem.
I would recommend NOT combining this tightly with your inventory system until you have it full operational alone. Inventory systems are hairy enough without combining item generation systems!!!
These things (inventory, shop systems, character customization, dialog tree systems, crafting, ability unlock systems, tech trees, etc) are fairly tricky hairy beasts, definitely deep in advanced coding territory.
The following applies to ALL types of code listed above, but for simplicity I will call it “inventory.”
Inventory code never lives “all by itself.” All inventory code is EXTREMELY tightly bound to prefabs and/or assets used to display and present and control the inventory. Problems and solutions must consider both code and assets as well as scene / prefab setup and connectivity.
If you contemplate late-delivery of content (product expansion packs, DLC, etc.), all of that has to be folded into the data source architecture from the beginning.
Inventories / shop systems / character selectors all contain elements of:
a database of items that you may possibly possess / equip
a database of the items that you actually possess / equip currently
perhaps another database of your “storage” area at home base?
persistence of this information to storage between game runs
presentation of the inventory to the user (may have to scale and grow, overlay parts, clothing, etc)
interaction with items in the inventory or on the character or in the home base storage area
interaction with the world to get items in and out
dependence on asset definition (images, etc.) for presentation
→ what it looks like lying around in the world? In a chest? On a shelf?
→ what it looks like in the inventory window itself?
→ what it looks like when worn by the player? Does it affect vision (binoculars, etc.)
→ what it looks like when used, destroyed, consumed?
Just the design choices of such a system can have a lot of complicating confounding issues, such as:
can you have multiple items? Is there a limit?
if there is an item limit, what is it? Total count? Weight? Size? Something else?
are those items shown individually or do they stack?
are coins / gems stacked but other stuff isn’t stacked?
do items have detailed data shown (durability, rarity, damage, etc.)?
can users combine items to make new items? How? Limits? Results? Messages of success/failure?
can users substantially modify items with other things like spells, gems, sockets, etc.?
does a worn-out item (shovel) become something else (like a stick) when the item wears out fully?
etc.
Your best bet is probably to write down exactly what you want feature-wise. It may be useful to get very familiar with an existing game so you have an actual example of each feature in action.
Once you have decided a baseline design, fully work through two or three different inventory tutorials on Youtube, perhaps even for the game example you have chosen above.
Breaking down a large problem such as inventory:
If you want to see most of the steps involved, make a “micro inventory” in your game, something whereby the player can have (or not have) a single item, and display that item in the UI, and let the user select that item and do things with it (take, drop, use, wear, eat, sell, buy, etc.).
Everything you learn doing that “micro inventory” of one item will apply when you have any larger more complex inventory, and it will give you a feel for what you are dealing with.
Breaking down large problems in general:
The moment you put an inventory system into place is also a fantastic time to consider your data lifetime and persistence. Create a load/save game and put the inventory data store into that load/save data area and begin loading/saving the game state every time you run / stop the game. Doing this early in the development cycle will make things much easier later on.
Various possible inventory data structures in Unity3D: