Sign in to follow this  
Corlesi

Problem with NativeUI

Recommended Posts

Hey there, I'm having a problem with this ui: The first time I open it - everything fine! https://prnt.sc/rb3fio

Problems arise when I open it for the second time and more - https://prnt.sc/rb3er5

 

As you can see, it looks like multiple menus stacking up. Scrolling speed also increases with each menu opened which makes it increasingly more difficult to interact with..

 

Any help appreciated  - Sorry for long script, but I really dont know where I went wrong with this :D So might aswell post it all

using System.Collections.Generic;
using RAGE;
using RAGE.NUI;
using Newtonsoft.Json.Linq;

namespace ClientSideGTA
{
	class Inventory : Events.Script
	{
		bool isMenuOpen = false;
		MenuPool mPool;
		UIMenu mainMenu;
		UIMenu inventoryMenu;
		UIMenu itemOptionsMenu;
		List<Item> itemList = null;
		Item currentItem = null;
		List<UIMenuItem> invItems;

		public Inventory()
		{
			Events.Add("MenuKeyPressed", OpenPlayerMenu);
			Events.Add("InventoryItems", GetInventoryItems);
		}

		//args[0] inventory list | args[1] currentweight | args[2] maxweight
		private void GetInventoryItems(object[] args)
		{
			inventoryMenu.Clear();
			itemList = null;
			invItems = null;
			JArray test = (JArray)args[0];
			itemList = test.ToObject<List<Item>>();
			invItems = new List<UIMenuItem>();

			foreach (var item in itemList)
			{
				UIMenuItem invItem = new UIMenuItem(item.Name, item.Description);
				invItem.SetRightLabel($"{item.Amount}x");
				invItems.Add(invItem);
			}

			UIMenuItem invWeightItem = new UIMenuItem("Gewicht:");
			invWeightItem.SetRightLabel($"{args[1]} / {args[2]}");
			inventoryMenu.AddItem(invWeightItem);

			foreach (var item in invItems)
			{
				inventoryMenu.AddItem(item);
				inventoryMenu.BindMenuToItem(itemOptionsMenu, item);
			}

			SetupItemMenu();
			
			inventoryMenu.RefreshIndex();

			foreach (var selection in invItems)
			{
				selection.Activated += (sender, item) =>
				{
					if (sender == inventoryMenu)
						if (item == selection)
						{
							foreach (var i in itemList)
							{
								if (i.Name == item.Text)
								{
									currentItem = i;
									CloseMenus();
								}
							}
						}
				};
			}
		}

		private void SetupCloseEvents()
		{
			inventoryMenu.OnMenuClose += (sender) =>
			{
				CloseMenus();
			};
			itemOptionsMenu.OnMenuClose += (sender) =>
			{
				CloseMenus();
			};
			mainMenu.OnMenuClose += (sender) =>
			{
				if (sender == mainMenu)
				{
					CloseMenus();
				}
			};
		}

		private void SetupItemMenu()
		{
			UIMenuItem useBtn = new UIMenuItem("Benutzen");
			UIMenuItem deleteBtn = new UIMenuItem("Wegwerfen");
			UIMenuItem giveBtn = new UIMenuItem("Weitergeben");

			itemOptionsMenu.AddItem(useBtn);
			itemOptionsMenu.AddItem(deleteBtn);
			itemOptionsMenu.AddItem(giveBtn);

			useBtn.Activated += (sender, item) =>
			{
				if (item == useBtn)
				{
					RAGE.Chat.Output("using current item: " + currentItem.Name);
					RAGE.Events.CallRemote("UseItem", currentItem.Name);
					CloseMenus();
				}
			};
		}
		

		private void OpenPlayerMenu(object[] args)
		{
			RAGE.Chat.Output("called1");
			if (isMenuOpen) return;
			RAGE.Chat.Output("called2");
			isMenuOpen = true;
			RAGE.Ui.Cursor.Visible = false;			
			
			mPool = new MenuPool();
			mainMenu = new UIMenu("Menu", "");
			
			mPool.Add(mainMenu);

			itemOptionsMenu = new UIMenu("Inventar123", "");
			mPool.Add(itemOptionsMenu);

			inventoryMenu = mPool.AddSubMenu(mainMenu, "Inventar");
				
			Chat.Show(false);

			mainMenu.OnItemSelect += (sender, item, index) =>
			{
				if (sender == mainMenu)
				{
					if (item.Text == "Inventar")
					{
						RAGE.Events.CallRemote("GetInventoryItems");

					}
				}
			};

			mPool.RefreshIndex();
			mainMenu.RefreshIndex();
			mainMenu.Visible = true;

			Events.Tick += (name) =>
			{
				mPool.ProcessMenus();
			};
			mPool.ResetCursorOnOpen = true;
			SetupCloseEvents();
		}

		private void CloseMenus()
		{
			inventoryMenu.FreezeAllInput = false;
			isMenuOpen = false;
			RAGE.Ui.Cursor.Visible = false;
			Chat.Show(true);
			inventoryMenu.Visible = false;
			itemOptionsMenu.Visible = false;
			mainMenu.Visible = false;
			mPool.CloseAllMenus();
		}
	}
}

 

Share this post


Link to post
Share on other sites

it looks like the menus

	mainMenu = new UIMenu("Menu", ""); mPool.Add(mainMenu);
	itemOptionsMenu = new UIMenu("Inventar123", ""); mPool.Add(itemOptionsMenu);
	...
	

 

are being generated several times. the CloseAllMenus() seems to close them, but not discard them which would justify re-creating them again.

i suggest you to create the menus once, in some other function, and avoid  the menu-contruction inside your loops.

(i had a similar problem a few weeks ago, try to /*comment*/ the menu-construction lines above to see if they are NOT being created. then you know that IF its NOT showing up, WHERE the code sits. if its occurs once only, the only suspect MUST be the bad boi ^^ i.e. you need to put it somewhere else outside the OpenMenu() function)

 

i'm sorry that i cant post/test any code yet, as iam just starting off, too...

  • Like 1

Share this post


Link to post
Share on other sites

Hey, thanks for answering! I rebuild the script a few times, being careful about creating the menupool.. Still same error. So after hours of headscratching I just went with a cef based menu https://streamable.com/mlikg -  

Thanks for trying tho :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.