Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate category enums from category heirarchy #169

Merged
merged 3 commits into from
Feb 28, 2024

Conversation

imothee
Copy link
Contributor

@imothee imothee commented Feb 9, 2024

Description

This attempts to create a PandoraCategories file that contains all the enum heirarchy written out.

Addressed issues

Attempts to close #63

Change

Outputs a pandora/categories.gd containing

# Do not modify! Auto-generated file.
class_name PandoraCategories


enum Tools {LOGGING = 30, MINING = 31, FISHING = 32, HARVESTING = 33, SKINNING = 34}
enum Wield {TOOLS = 5, WEAPONS = 87}
enum Armor {FEET = 23, LEGS = 24, BELT = 25, TORSO = 26, HANDS = 27, HEAD = 28, BEHIND = 29}
enum Equipment {WIELD = 35, ARMOR = 50, ACCESSORIES = 51}
enum Items {EQUIPMENT = 4, CONSUMABLE = 37, RESOURCE = 38, LITERARY = 39, QUESTS = 40, MISC = 41}
enum Enchantments {ARMOR_ENCHANTMENTS = 58, WEAPON_ENCHANTMENT = 63}
enum Characters {NPCS = 89}
enum Loot {TREES = 133}
enum Trees {TREE_A = 166, TREE_C = 170, TREE_B = 171}
enum Resources {TREES = 136}
enum GameConfig {BALANCE = 183}
enum RootCategories {ITEMS = 3, RARITY = 7, ENCHANTMENTS = 48, SKILLS = 66, ANIMATIONS = 70, CHARACTERS = 71, BODY_TYPES = 72, LOOT = 73, HAIR_TYPES = 120, RESOURCES = 135, RESOURCE_STATES = 142, GAMECONFIG = 177, EQUIPMENT_SLOTS = 193}

@imothee
Copy link
Contributor Author

imothee commented Feb 10, 2024

Discussion point: What's interesting is the way I got around this without generating the enums was to create a new category for each equipment slot (as an example) and set it as the resource default on the parent category.

Ie. EquipmentSlots: Hands
Armor/Hands -> Default EquipmentSlot to EquipmentSlots/Hands

That way it was a property lookup rather than caring what the category was. I'm not sure which is better, the enums use less disk space since you're not having to serialize a reference on every single item. My guess is that a true category enum system is more flexible and easier to maintain since the inheritance is defined by the category structure and not a bunch of hard coded fields.

That said, one of the ways the embedded reference was nicer was you could merge multiple categories more easily, right now the way we generate enums I don't have every child of Equipment in a single enum (I wonder if I should add that) or have a boolean for "only direct children" or "all children"

ie. Items.Equipment.Armor and Items.Equipment.Wield.Tools doesn't share a single type but my equipment system has slots you can equip items in for both sets and ideally the interface would be equip_item(item: Item, Slot: PandoraCategories.Equipment)

@imothee
Copy link
Contributor Author

imothee commented Feb 10, 2024

I added in a recursive bool to the code that produces something like

# Do not modify! Auto-generated file.
class_name PandoraCategories


enum Tools {LOGGING = 30, MINING = 31, FISHING = 32, HARVESTING = 33, SKINNING = 34}
enum Wield {TOOLS = 5, LOGGING = 30, MINING = 31, FISHING = 32, HARVESTING = 33, SKINNING = 34, WEAPONS = 87}
enum Armor {FEET = 23, LEGS = 24, BELT = 25, TORSO = 26, HANDS = 27, HEAD = 28, BEHIND = 29}
enum Equipment {WIELD = 35, TOOLS = 5, LOGGING = 30, MINING = 31, FISHING = 32, HARVESTING = 33, SKINNING = 34, WEAPONS = 87, ARMOR = 50, FEET = 23, LEGS = 24, BELT = 25, TORSO = 26, HANDS = 27, HEAD = 28, BEHIND = 29, ACCESSORIES = 51}
enum Items {EQUIPMENT = 4, WIELD = 35, TOOLS = 5, LOGGING = 30, MINING = 31, FISHING = 32, HARVESTING = 33, SKINNING = 34, WEAPONS = 87, ARMOR = 50, FEET = 23, LEGS = 24, BELT = 25, TORSO = 26, HANDS = 27, HEAD = 28, BEHIND = 29, ACCESSORIES = 51, CONSUMABLE = 37, RESOURCE = 38, LITERARY = 39, QUESTS = 40, MISC = 41}
enum Enchantments {ARMOR_ENCHANTMENTS = 58, WEAPON_ENCHANTMENT = 63}
enum Characters {NPCS = 89}
enum Loot {TREES = 133}
enum Trees {TREE_A = 166, TREE_C = 170, TREE_B = 171}
enum Resources {TREES = 136, TREE_A = 166, TREE_C = 170, TREE_B = 171}
enum GameConfig {BALANCE = 183}
enum RootCategories {ITEMS = 3, EQUIPMENT = 4, WIELD = 35, TOOLS = 5, LOGGING = 30, MINING = 31, FISHING = 32, HARVESTING = 33, SKINNING = 34, WEAPONS = 87, ARMOR = 50, FEET = 23, LEGS = 24, BELT = 25, TORSO = 26, HANDS = 27, HEAD = 28, BEHIND = 29, ACCESSORIES = 51, CONSUMABLE = 37, RESOURCE = 38, LITERARY = 39, QUESTS = 40, MISC = 41, RARITY = 7, ENCHANTMENTS = 48, ARMOR_ENCHANTMENTS = 58, WEAPON_ENCHANTMENT = 63, SKILLS = 66, ANIMATIONS = 70, CHARACTERS = 71, NPCS = 89, BODY_TYPES = 72, LOOT = 73, TREES = 136, HAIR_TYPES = 120, RESOURCES = 135, TREE_A = 166, TREE_C = 170, TREE_B = 171, RESOURCE_STATES = 142, GAMECONFIG = 177, BALANCE = 183}

The other option is to have two enums, one for the top level category and one like category_nested or category_all so you can reference either the single depth or multi-depth enum. Thoughts?

@imothee
Copy link
Contributor Author

imothee commented Feb 10, 2024

Oh one other thing I noticed, since enums write out as ints the category lookup needs a cast from the value to string so there's likely something to be considered for consistency sake on how we want to manage that.

@imothee
Copy link
Contributor Author

imothee commented Feb 22, 2024

Any thoughts on the direction you'd want to take this? Can convert into a discussion instead of using this PR

@bitbrain
Copy link
Owner

@imothee sorry for the late reply - I have been rather busy this week. I will check out this PR and will provide my feedback after.

@bitbrain bitbrain added the 🔼 enhancement New feature or request label Feb 23, 2024
Copy link
Owner

@bitbrain bitbrain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally a good approach. A few things need improvement:

  1. entities/categories may have different id types (number or string), depending on the ID generator. Hence, we cannot rely on enums but we probably have to generate constants/classes. For example, something like this:
# Do not modify! Auto-generated file.
class_name PandoraCategories


class Items:
   const SWORDS = "2"
   const SHIELDS = "3"

class Swords:
   const GREAT_SWORDS = "4"
   const SMALL_SWORDS = "5"

# root categories can be on the top level as const
const ITEMS = "1"
  1. Secondly, we may want to update the docs, too describing the new behaviour https://github.com/bitbrain/pandora/blob/godot-4.x/docs/mastery/id-constants.md

@imothee
Copy link
Contributor Author

imothee commented Feb 26, 2024

Updated generator. I had to append Categories to the classes because they usually conflicted with the generated entity file names. Naming conflicts is going to be tough regardless, I tried to handle non-unique category names.

Pandora will also generate a PandoraCategories file that contains the Category IDs of all categories and subcategories in your tree.
It handles non-unique categories by prepending the parents name to the category class generated.

It will generate a file called res://pandora/categories.gs that looks a bit like this:

# Do not modify! Auto-generated file.
class_name PandoraCategories


const ITEMS = "3"
const RARITY = "7"
const ENCHANTMENTS = "48"
const SKILLS = "66"
const ANIMATIONS = "70"
const CHARACTERS = "71"
const BODY_TYPES = "72"
const LOOT = "73"
const HAIR_TYPES = "120"
const RESOURCES = "135"
const RESOURCE_STATES = "142"
const GAMECONFIG = "177"


class ItemsCategories:
	const EQUIPMENT = "4"
	const CONSUMABLE = "37"
	const RESOURCE = "38"
	const LITERARY = "39"
	const QUESTS = "40"
	const MISC = "41"


class EquipmentCategories:
	const WIELD = "35"
	const ARMOR = "50"
	const ACCESSORIES = "51"

This allows you to do something like this:

if entity.is_category(ItemsCategories.EQUIPMENT):
   # entity is under the Equipment category

Copy link
Owner

@bitbrain bitbrain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing work.

@bitbrain bitbrain merged commit 874442f into bitbrain:godot-4.x Feb 28, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔼 enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generate category ids
2 participants