Strategy pattern implementation

Godot Version

4.4.1

Question

I learnt about the strategy pattern from this Bitlytic video.
When I followed the exact setup in a test game, everything was working. But when I tried to use the concept for the card game I’m working on, I changed the way to get the upgrade from a “pickup” scene holding the resource to a component on the card itself that is called by a signal. When I try to append the strategy I get the error:
Attempted to push_back an object into a TypedArray, that does not inherit from ‘GDScript’.
If I change the the upgrades array to a non-typed one, just a regualr empty array, I instead get:
Invalid call. Nonexistent function ‘apply_upgrade’ in base ‘GDScript’.
Not matter how I try and hold the upgrade resource, I just run into the same issue.

Paste your script, it sounds like a very specific syntax problem. You are adding a GDScript type to an array, I would bet you are using a class_name’s type instead of the object

i.e.

class_name MyType

# other script
# valid array of type "MyType"
var my_array: Array[MyType] = []

# Trying to add the type itself to the array, results in your first error
my_array.push_back(MyType)

# creates a default MyType
my_array.push_back(MyType.new())

Based on the source code maybe you did this for an export.

# Wrong
# Assigned to BaseBulletStrategy, the type is GDScript
@export var bullet_strategy = BaseBulletStrategy

# Correct
# Not assigned any value, the type is explicitly BaseBulletStrategy
@export var bullet_strategy: BaseBulletStrategy
2 Likes

Thanks so much for reply. I’m 100% just calling the class name, yeah. I also had one more idea of how I could fix it, I’ll try that along with what you suggested. I’m going to bed, but I’ll post my code tomorrow if I can’t get it working.

1 Like

Thanks again. I got it working. The very specific syntax problem was a typo, lol. I’m a fool.