How to call function from global script

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Asthmar

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 add_quest(quest_num):
quest_num = quest_num

add_item(Quest.quest_list[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


:bust_in_silhouette: Reply From: njamster

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”.

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.

Asthmar | 2020-03-03 15:50

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.

njamster | 2020-03-03 16:08

GitHub - Athsmar/globalexample ok here it is

Asthmar | 2020-03-03 18:06

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.

njamster | 2020-03-04 10:17

Thanks so much my problem is now solved!

Asthmar | 2020-03-06 00:16