I am making a RPG-like game, and I want to have a lot of items, where each item does a different thing. I am wondering what is the best way to manage this situation.
The way I am currently implementing this is: in a script called items.gd I create a class for each item, and then I return them with an index, as:
if (index == item1.uid): return item1.new()
elif (index == item2.uid): return item2.new()
With this, I can get any item as:
item = get_item(0) #item is item1 object
This is ok, but when the number of items is big, the amount of code I to use for this approach grows a lot. I thought that each item could extend from a BaseItem class that has the general behaviour for functions and stores common variables (as the name or texture).
However, even doing this I still need to implement the
get_item function, returning by hand each one of the classes. The best that comes to my mind is to put all the classes in an array and something like:
var classes = [item1, item2]
for c in classes:
if (index == c.uid): return c.new()
But still, hard-coding the
classes array seems very brute-force to me. Is there any way to solve this problem in an elegant way using GDScript?