Odd behaviour with tooltips for MenuButton items

Godot Version

4.2.2

Question

Trying to give items in a MenuButton tooltips but am seeing unpredictable behaviour.
This function is the only one setting anything in the MenuButton:

func setup(data: Dictionary, tooltips: Dictionary = {}) -> void:
    var id := 0
    for dict_name: StringName in data:
        get_popup().add_item(dict_name, id)
        if tooltips.has(dict_name):
            var index = get_popup().get_item_index(id)
            get_popup().set_item_tooltip(index, tooltips[dict_name])
            print(get_popup().get_item_text(index) + " = " + get_popup().get_item_tooltip(index))
        id += 1

It leads to correct output that looks like this (full output is 91 lines):
HOI = Hospitality
INTS = Intelligense
JJP = JJ Palms
LMB = Lambda Logistics
LASR = Lux Optics
MCM = Magnum Component Manufacturing
MASS = Mass Dynamics
MNSF = Mc’n’Sayn Fur’ture

However, the behaviour in game is unpredictable. Tooltips change randomly and arbitrarily so much that while writing this post I got confused because I’d look at a button’s tooltip, decide to bring it up as an example, then tab back in again and its tooltip had changed.

Generally, towards the bottom of the list (91 items) it gets more and more unpredictable. In the last few 30ish items usually there’s just no tooltip. The first few usually have the correct one.

I tried calling it that function’s printing bits during runtime and the printed list is still correct, even after I check the tooltips are fucked up.
(wrong tooltips)



(no tooltip)
image-1

Hi, create a resource for this.

extends Resource
class_name TileCardDefinition

var card_name : String = "UNDEFINED"
var description : String = "[font_size=10]effect[/font_size]
[i][font_size=8]flavour text testing if this is bottom[/font_size][/i]"

then do

for dict_name : TileCardDefinition in data:
	get_popup().add_item(dict_name.card_name, id)
	get_popup().set_item_tooltip(get_popup().get_item_index(id), dict_name.description)
	id += 1

4.2 doesn’t have static typed dictionaries so you will just have to be careful.

probably has to do with using a dictionary. it would be a good idea to use an Array instead and see if it gets fixed.
you can use a custom sorting algorithm to sort by name of each resource.

The issue seems to be unchanged by swapping to an array of resources. I’m not sure why static typing would matter in this instance anyway? I don’t have any items of the wrong type in any dictionary.

It’s an engine bug. I’ve opened an issue here PopupMenu shows wrong item tooltip when scrolled · Issue #104262 · godotengine/godot · GitHub

I’ll look at it tomorrow if it hasn’t been fixed yet.
If this is indeed a bug, OP will have to wait for another release and upgrade the project to 4.4-ish.

But maybe understanding why this happens will reveal some alternate solution.