This is going to be a long post, so I'll give you the TDLR upfront: Following a text adventure tutorial, confused on how to create and reference objects with unique properties.
Here's the tutorial's 'items' episode for reference:
I'm very new to GD Script so forgive my lack of understanding in advance. Came from Gamemaker Studio 2 so my concept of objects and how they communicate is completely different from the Godot community's.
Basically, I have an "Item" class (extending from a 'resource' node) I'm using to act as a core for all of the text adventure's objects. "Types.ItemTypes" just refers to a enum in another script.
extends Resource
class_name Item
export(String) var item_name := "Item Name"
export(Types.ItemTypes) var item_type := Types.ItemTypes.KEY;
func initialize(item_name: String, item_type: int):
self.item_name = item_name;
self.item_type = item_type;
#function I'm trying to get to work correctly
func speak(item: Item) -> String:
if item.item_name == "phone":
return "hahaha I'm speaking on the phone";
else:
return "error";
This item class is initialized into a resource in a "Room Manager" script. It takes the resource and puts it in a tutorial room.
extends Node
func _ready() -> void:
#create resources
var key = Item.new();
var phone = Item.new();
#essentially a constructor?
key.initialize("key", Types.ItemTypes.KEY);
phone.initialize("phone", Types.ItemTypes.DIALOGUE);
#connect exits and add items to rooms
$TutorialRoom.connect_exit("north", $FirstRoom);
$TutorialRoom.add_item(key);
$TutorialRoom.add_item(phone);
The major issue I'm struggling with is that I'm trying to call the 'speak' function in another script called "CommandProcessor" which takes in user input and returns a string. Everything else in the code is working for user input (recognizes commands, gives appropriate responses, etc) which is good. However, I'm having issues accessing the "phone" object in the Tutorial Room so it run the 'speak' function from the parent 'item' class.
func use(second_word: String) -> String:
#check if item is in room
if current_room.item_in_room(second_word):
match(second_word):
"phone":
#issue I'm running into
return phone.speak(current_room.phone);
"key":
return "You use the key.";
_:
return "error";
#check if item is in player inventory
elif player.item_in_inventory(second_word):
return "You pull out the " + second_word + " and use it.";
#not in room nor inventory
else:
return "That's not here.";
I've been combing Godot's documentation and can't seem to find a solution to this problem. Just wanted to know if there's a better way to do this? Currently, I don't think the tutorial's classification of items as resources is going to work for my game, as the objects in my game need to hold more than just data. But I want to solve one issue at a time.
I'm not asking for a complete solution or anything, but some direction or recommendation of external resources to look into would be astronomically helpful.
Will gladly post more code/context if needed.
Thanks.