+1 vote

I have an ItemList node for which I've allowed rmb selection, but when I use the item rmb selected signal I can seem to get the correct index. Here is the function I can't seem to get to work.

func _on_MemberList_item_rmb_selected( index, atpos ):
    get_node("RelPopup").clear()
    get_node("RelPopup").add_item("Add Player as friend",0)
    get_node("RelPopup").add_item("Add Player as lover",1)
    get_node("RelPopup").add_item("Add Player as rival",2)
    for z in peoplescript.get_outpost():
        listindex = listindex + 1 # listindex has been defined as the correct value
        print(z["id"]) #gives me what I want, the id value I've given to the npc
        print(str(index)) #always returns 0
        if (z["id"] == str(index)):
            continue #this should skip the itteration, but doesn't because index is always 0
        get_node("RelPopup").add_item("Add " + z["firstname"] + " " + z["lastname"] + " as a friend" + "index = " + str(listindex),listindex)
        get_node("RelPopup").add_item("Add " + z["firstname"] + " " + z["lastname"] + " as a lover",listindex)
    get_node("RelPopup").set_hidden(false)

I don't know what I'm doing wrong and I can't find any usefull documentation that is helpfull to me right now.

in Engine by (13 points)
edited by

1 Answer

+1 vote
  1. Did you used set_allow_rmb_select(bool) before?
  2. What peoplescript.get_outpost() returns? (type)
by (186 points)
edited by

1) Yes I used set_allow_rmb_select(true)in the _ready function
2) peoplescript is a variable in which I stored get_node("/root/People"), which is another script. In this script I have variable called outpost, which is an array with dictionaries in it. Every dictionary in outpost holds information about an npc (such as their first name, last name and an id used to track the npcs)

Don't know yet, why that's not working, but do you have multi-selection activated?
Is there any add_item in _ready or somewhere before that signal?

No, I do not have multi-selection activated.

It's one column, or multi column? I'm looking into Godot source code, and everything seems to be ok... Here's our index checking and line 445 looks interesting...

It's all in a single column and to be honest I wouldn't even know how to make it multi column if I wanted to. To be sure I didn't accidentally add another column I printed the result of get_max_columns, which returned 1.

func _ready():

   set_allow_rmb_select(true)
   add_item("a")
   add_item("b")
   add_item("c")
   connect("item_rmb_selected",self,"rmb")

func rmb(index,pos):
   print(index)

That works for me, so signal code work's fine...
Are items visable, eg. have size > 0? Node isn't hidden?
Are Items selectable?

I've gone and retyped that entire code, in the hope of fixing whatever mistake I made and now it works as intended. Must have been a typo somewhere that I didn't find. Thanks for your help anyway.

Curiosity, could you send new code for us? It may be useful for someone else :)

Here's the entire code I've got now and which works as intended. Still don't know exactly what the mistake was as I've looked it over again and it looks exactly the same as far as I can see.

var amount_members = 0
var selected_index
var listindex = 2
onready var peoplescript = get_node("/root/People")

var player_relation_pos = ["friend","lover","rival"]

func _ready():
    get_node("MemberContainer/MemberList").set_allow_rmb_select(true)
    get_node("OutpostNameLabel").set_text(peoplescript.get_outpost_name())
    for x in peoplescript.get_outpost():
        get_node("MemberContainer/MemberList").add_item(peoplescript.short_description(amount_members),null,true)
        get_node("MemberContainer/MemberList").set_item_tooltip(amount_members,peoplescript.simple_description(amount_members))
        get_node("MemberContainer/MemberList").set_item_icon(amount_members,load(str("res://Fonts/",peoplescript.get_gender(amount_members),".png")))
        amount_members += 1

func _on_AddButton_pressed():
    peoplescript.add_member(peoplescript.new_person())
    get_node("MemberContainer/MemberList").add_item(peoplescript.short_description(amount_members),null,true)
    get_node("MemberContainer/MemberList").set_item_tooltip(amount_members,peoplescript.simple_description(amount_members))
    get_node("MemberContainer/MemberList").set_item_icon(amount_members,load(str("res://Fonts/",peoplescript.get_gender(amount_members),".png")))
amount_members += 1



func _on_ReturnButton_pressed():
    get_node("/root/Scenechanger").setScene("res://Scenes/TitleScreen.tscn")


func _on_MemberList_item_selected( index ):
    peoplescript.set_relationship(index, (player_relation_pos[randi() % player_relation_pos.size()]), "player")
    get_node("prellabel").clear()
    get_node("prellabel").add_text(str(peoplescript.get_all_relationships(index)))
    get_node("RelPopup").set_hidden(true)
    selected_index = index


func _on_MemberList_item_rmb_selected( index, atpos ):
    get_node("RelPopup").clear()
    get_node("RelPopup").add_item("Add Player as friend",0)
    get_node("RelPopup").add_item("Add Player as lover",1)
    get_node("RelPopup").add_item("Add Player as rival",2)
    for z in peoplescript.get_outpost():
        if (z["id"] == str(index+1)):
            continue
        get_node("RelPopup").add_item("Add " + z["firstname"] + " " + z["lastname"] + " as a friend")
        get_node("RelPopup").add_item("Add " + z["firstname"] + " " + z["lastname"] + " as a lover")
    get_node("RelPopup").set_hidden(false)


func update_relationships(index):
    get_node("prellabel").clear()
    get_node("prellabel").add_text(str(peoplescript.get_all_relationships(index)))

func _on_RelPopup_item_pressed( ID ):
    if (get_node("RelPopup").get_item_text(ID) == "Add Player as friend"):
        peoplescript.set_relationship(selected_index, "friend", "player")
        update_relationships(selected_index)
        return
    elif (get_node("RelPopup").get_item_text(ID) == "Add Player as lover"):
        peoplescript.set_relationship(selected_index, "lover", "player")
        update_relationships(selected_index)
        return
    elif(get_node("RelPopup").get_item_text(ID) == "Add Player as rival"):
        peoplescript.set_relationship(selected_index, "rival", "player")
        update_relationships(selected_index)
        return
    else:
        for y in peoplescript.get_outpost():
            if(get_node("RelPopup").get_item_text(ID) == ("Add " + y["firstname"] + " " + y["lastname"] + " as a friend")):
                peoplescript.set_relationship(selected_index, "friend", y["id"])
                update_relationships(selected_index)
                return
            elif(get_node("RelPopup").get_item_text(ID) == ("Add " + y["firstname"] + " " + y["lastname"] + " as a lover")):
                peoplescript.set_relationship(selected_index, "lover", y["id"])
                update_relationships(selected_index)
                return
            elif(get_node("RelPopup").get_item_text(ID) == ("Add " + y["firstname"] + " " + y["lastname"] + " as a rival")):
                peoplescript.set_relationship(selected_index, "rival", y["id"])
                update_relationships(selected_index)
                return

Hmm. I don't know too :p Good for you that it now works. CU, good luck in coddin'.

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.