This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+1 vote

I have a few items that I would like to be able to drop into the world with different properties but they're all items that the player can pick up.

  1. Apple
  2. Orange
  3. Ore
  4. Log

They have some properties e.g. consumable.

I have a json for all my objects

{ 'Apple': { 'consumable': true, 'asset_path': 'res://assets/items/apple.png' }, 'Log': { 'consumable': false, 'asset_path': 'res://assets/items/log.png' } }

What's the best way to structure this?

  1. A scene for each item specifically?
  2. One scene and instantiate the values programmatically?

If it's option 2, how do I go about it?

```
var item = Item.instance()
item.set_type('Log')

parent.add_child(item)
```

How about more complex scenes with timers and triggers? How do you make it more generic and "inheritable"? Thanks!

Godot version 3.3
in Engine by (13 points)

2 Answers

0 votes

If your pickups have many properties in common its best you create a base script then extend the other scripts from it.

by (2,018 points)

Thank you so much. I added the same question below btw.

Would I still create a scene for each one of my items?

Say I have a Timer and a Sprite and Collision box. Do I have to create the same for each item? I think that's the part I'm confused about.

0 votes

_magicalogic_ had got the right idea on how to set up the scene. Create a base class, Item, and extend it with the other items:

# Base class
class_name Item
var consumable: bool = false
var asset_path: string = ""

# Apple item
extends Item
func _ready():
     consumable = true
     asset_path = "res://assets/items/apple.png"
by (3,164 points)

Thank you so much. Would I still create a scene for each one of my items?

Say I have a Timer and a Sprite and Collision box. Do I have to create the same for each item? I think that's the part I'm confused about.

Only if you wanted to include them among the other items. Have a scene that's bare bones (e.g. only one Timer, one Sprite, or one collision node), and then instance them as necessary.

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.