#How to properly use inherited gdscript classes in the same scene

1 messages · Page 1 of 1 (latest)

steep plaza
#

So I've got a scene with an attached script. I've made a subclass of that script with some unique behaviours. Now I would like to create an instance of that scene but with the subclassed script attached. At the moment I'm doing:

var itemTemp : Item = load("res://item.tscn").instantiate() itemTemp.set_script(load("res://item-Potion.gd")) itemTemp.initPotion() potion = itemTemp as ItemPotion

But I've got a bug and I feel like I'm probably not doing this step properly.

I want to use the exact same scene as item but item-Potion.gd inherits from item.gd and just adds some attributes and functions.

Thanks for any help

echo idol
#

Hey! I have two questions to help you get help faster:

  • What bug are you getting?
  • Can you format your code monospaced to make it easier to read?
steep plaza
#

Sorry I thought adding the spaces would make it monospaced, I'll edit it. The bug is longer to explain, it may or may not be related to how I'm instantiating this node. I'm mostly just asking if the way I'm instantiating this object makes sense? Or is there a much cleaner way to do the same thing?

glacial mirage
#

What is the class Tile (used for the type hint on the first line)? Is that related to the class ItemPotion? Do you have a class Item?

steep plaza
#

Tile is a bad name

#

Its the super class

#

it should be called Item right now

#

it was briefly Tile

#

fixed it

glacial mirage
#

In most OOP languages, it's not common (sometimes not even possible) to change the type of an object without just constructing an entirely new object. GDScript allows you to do this with the set_script() method, but i'm personally hesitant to use it given that it's not a common pattern or well-defined pattern. I personally would probably look into other ways of accomplishing the same thing, such as using composition (give Item an optional member which modifies the behavior or adds extra properties).

#

But that's just my two cents. Design questions are always at least somewhat a matter of opinion, and just because changing the type of an object isn't common, that doesn't necessarily mean it's bad