+2 votes

I have items in my game and right now to define all the stats for them I use a dictionary. As there is more data to store the longer the dictionary gets and thats only for one item. Making a class called item would make everything a LOT easier but my items vary. Some are tools that do damage and have their own special stats while others may be blocks and have to store tilemap data. How can I do this? Or are classes not a thing in gdscript? I come from a python background.

in Engine by (432 points)

1 Answer

0 votes

In a fact everything in Godot is a Class, every script you make is a Class, so this is actually an easy one, and has multiple solutions you can use.

Either make a reference class called item (unattached script), and make every other item class (script) extend that class. Or make a multipurpose class called item that has extend variables of various stats, and change those variables via editor for each item node you create.

Additionally you can set an item_type in the reference/description of your items, and when using an item from active slot, decide what to do for each item type e.g.:

if active_item.item_type == "potion":
  player.health += 5
elif active_item.item_type == "block":
  var tile_position = world_to_map(get_global_mouse_position())
  tilemap.set_cell(tile_position.x, tile_position.y, active_item.tile)
by (128 points)
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 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 webmaster@godotengine.org with your username.