How to remove menu item from OnsScreenMenu

Hi!

In this procedure a menu item is added but how can I remove it after selecting it?
(I want interactive menu items on run time that depend on choices made).
I have tried " OnScreenCrewmemberInfoMenu.RemoveControl(MenuItem);" but the
“Where can I find” menu Item is still there and every time I call this procedure a new “Where can I find” item is added again (so it becomes a list).
I also tried MenuItem.Destroy but it takes the menu item away before selecting.

class procedure TEventHandler.CrewmemberInformationClick(Sender: TObject);
var
I: Integer;
HS, HH: String;
MenuItem: TCastleOnScreenMenuItem;
begin
SearchString := (Sender as TCastleOnScreenMenuItem).Caption;

For I := 1 to NPCAmount do // check all NPC for their current location
begin
if NPCinfo[I].Personalia.FirstName = SearchString then
begin
Window.Controls.Remove(OnScreenCrewmemberInfoMenu);
Window.Controls.InsertFront(TalkTextLabel);
TalkTextLabel.Text.Clear;
if NPCinfo[I].Personalia.Gender = ‘m’ then HS := ‘He’;
if NPCinfo[I].Personalia.Gender = ‘m’ then HH := ‘him’;
if NPCinfo[I].Personalia.Gender = ‘f’ then HS := ‘She’;
if NPCinfo[I].Personalia.Gender = ‘f’ then HH := ‘her’;

  MenuItem := TCastleOnScreenMenuItem.Create(Application);
  MenuItem.Caption := 'Where can I find ' + HH + '?';
  MenuItem.OnClick := @TEventHandler(nil).CrewmemberLocationClick;
  OnScreenCrewmemberInfoMenu.Add(MenuItem);
  Window.Controls.Remove(OnScreenConversationMenu);
  Window.Controls.InsertFront(OnScreenCrewmemberInfoMenu);
  if NPCinfo[I].Location = 'Unknown' then LabelText := 'I really do not know.' else LabelText := HS + ' is at ' + NPCinfo[I].Location;
  OnScreenCrewmemberInfoMenu.RemoveControl(MenuItem);
end;

end;
Window.Controls.Remove(OnScreenCrewmemberMenu);
end;

how can I remove it after selecting it?

Try OnScreenCrewmemberInfoMenu.MenuItems.RemoveControl(MenuItem);. Note that it’s a simple TCastleVerticalGroup under the hood.

Also note that in the code above you Add(MenuItem); and then immediately RemoveControl(MenuItem); - i.e. it won’t ever appear on screen. I think the code after Add(MenuItem); was supposed to go inside CrewmemberLocationClick?

1 Like

Thanks!
You were right it did not appear so I just put OnScreenCrewmemberInfoMenu.MenuItems.RemoveControl(MenuItem) in the CrewmemberLocationClick procedure.
For that to work I had to redeclare MenuItem as a global variable.

Thanks for your help, it saved me many hours of trying things out.

1 Like