–1 vote

Hello,

I am currently trying to setup a monster mechanic similar to Pokémon with different monsters that have different stats and attacks.

The problem is I can't figure out what is the best way to set this up.

I basically need a main definition for a monster from which I can extend different monsters. I tried doing it with Resource files, but I can't create instances with separate values and save and load them via JSON, right? I can only create resources that share the defined stats like level, health, attack values it seems. That is problematic if my monster is the same type as an other encountered monster but differs in the level and other stats.

How would I create such a system that is easy to extend and load /save?

Godot version 3.5
in Engine by (11 points)

1 Answer

+1 vote

Arrays are your friend, as are dictionaries. After you learn how to put all that data you need into these structures, it's just how you program afterwards.

Godot Docs - Array

So it could be something like this:

var _FirebirdDictionary = {
        "Max health" : 100
        "Max skill points" : 20
        "Element" : "Fire"
        "Flying" : true
        "Health Level Up" : (randi()10)+3
        "Skill Level Up" : (randi()3)+2)
        "Level" : 1

So that holds your data. Then you tuck that into a dictionary:

var _Bestiary = [_FirebirdDictionary, _WaterTurtleDictionary....]

And pull that for fights. If it were me, hold the base stats and then have stats for how much each stat increases per level. So compare the level data with the dictionary, and after that it's a bunch of math. But hopefully that is good source material?

by (534 points)

Thanks for your answer, SnapCracklins!
I tried your approach before, but found a caveat that didn't really look solvable with arrays or dictionaries for me.

So I opted for creating classes for a base monster, that gets initialized with base values (in the _init() method from tres reference files). The reason also was that I want to define functions for attacking, getting damage, levelling up etc, right inside one specific monster instance and having the ability to change that aswell.
That did not seem possible with just base values like in dictionaries (or is there a way to save instances of custom classes in dictionaries)?

e.g. I currently save my monster party of 6 monsters in an array of Monster classes and initialize them based on the inst2dict() values in my savegame when loading the game.
That way I have differing values from the base class and can override stuff inside there.

Thats seems to be a plausible and extendable way for me, but let's see when I see a downside of this approach as well :)

Yeah i have seen the resource method in a lot of tutorials. For the record, you can save objects and data structures in dictionaries. But glad you got it worked out. That’s the great thing about programming - there’s always more than one way to build something.

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 Frequently asked questions and 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 [email protected] with your username.