–1 vote

So I want one of my global scripts which is attached to an itemList node to run a function when I call it from another scipt. But one line in my function code wont work.

I call the function : Global.add_quest(1)

This is what the function consist of :

func addquest(questnum):
questnum = questnum

additem(Quest.questlist[quest_num].Name) # Line of code that works when I do it in the global script itself, but if I try to call it from another script this line wont work
print("addquest") # for debugging. this line works when I call the func from outside of script

func add_quest(quest_num):
    quest_num = quest_num


in Engine by (659 points)

1 Answer

+2 votes
Best answer

What do you mean by "won't work"? What do you expect to happen and what happens instead? Do you get any errors or warnings? How does Quest.quest_list look like? I tried to reproduce your issues, but couldn't. Here's what I did:

extends ItemList

var quest_list = ["Find the Bug?", "Squash It!"]

func add_quest(quest_num):
    print("adding quest #", quest_num, ": ", quest_list[quest_num])

func get_quests(id):
    print("total quests count: ", self.get_item_count())
    print("quest #", id, ": ", self.get_item_text(id))

I'm then calling this from another scene like this:


And it (correctly) prints the following:

adding quest #0: Find the Bug?
total quests count: 1
quest #0: Find the Bug?
adding quest #1: Squash It!
total quests count: 2
quest #1: Squash It!

Of course there will be issues, when you provide a quest_num bigger than the quest_list (for example because you're forgetting it's zero-indexed), but that should result in an error - not just simply "don't work".

by (10,469 points)
selected by

Sorry, forgot to expand on that. Whats not working is the add_item. I want the name of the quest from the array to show on the item list. But Its not adding to my item list when I try to call it from the global.

No idea then. As described above, that's precisely what I did - and it works perfectly fine for me. You probably should provide an example project if you want more help.

Alright, so when you call QuestUiP.add_quest(1), you're calling a function in the Singleton, i.e. a node independent from your current scene, accessible through the path /root/QuestUiP (check the remote view!). It is this specific instance where you quest will be added. However, as you only added a script (a file ending in ".gd") as a Singleton, Godot certainly knows it has to spawn an ItemList (as the script extends ItemList), but doesn't know how that ItemList should look. That's why you can't see it: it has a width of 0. If you switch to the remote view of the tree while running your game, you can edit that value and the list (containing the added quest) will become visible. Alternatively you can add a scene (a file ending in ".tscn") as the Singleton instead, so Godot knows how wide the ItemList should be from the get go.

In any case, the quest will be added to that instance, not the local instance in your player scene. For that you need to do $ItemList.add_quest(1) instead.

Thanks so much my problem is now solved!

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 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 webmaster@godotengine.org with your username.