Jump to content

[SOLVED] [C#] How to fill NativeUI with a <List> of Informations?


Spliddorama

Recommended Posts

Hi community, I have a rather basic question about c# programming.
I work on the server and client side with c#.
So far I have already implemented some menus using NativeUI, which was never a problem as long as the amount of information was manageable.

Now I'm at a point where I need to pass an "unknown" set of records to a NativeUI menu.
In particular, it is about querying "keys" (for vehicles, houses, etc.).
All keys of the player should be displayed in the menu.
This can be one or hundreds. For the menu I only need the ID and the label of the data record.

I have now spent many hours creating a list via SQL query, which I now want to transfer to the NativeUI.

//datebase writes the list as follows
var keyList = new List<Tuple<int, string>>();
...
while (reader.Read())
{
  int id = reader.GetInt16("id");
  string label = reader.GetString("label");

  keyList.Add(new Tuple<int, string>(id, label));
}
...

//then send the list to the player/nativeUi
NAPI.ClientEvent.TriggerClientEvent(player, "TriggerNuiMenuKeyCopy", keyList);


With the client-side event, it's not a "list" that arrives, but an "object". I just don't know how to get a list there again.

Events.Add("TriggerNuiMenuKeyCopy", MenuKeyCopy);
public void MenuKeyCopy(object[] args){}


In the NativeUI menu, I wanted to create the UIMenuItems using a "foreach" loop. This, in turn, does not work either, since the items have to be named differently in each case.

Long story short: Can someone please tell me how you transfer a list of information to your NativeUI and process it there? (C#)

Edited by Spliddorama
Link to comment
Share on other sites

  • 2 weeks later...

I've now managed to get the individual records/entries into the menu:

List<UIMenuItem> MenuItems = new List<UIMenuItem>();
List<String> ButtonActions = new List<String>();

var json = Convert.ToString(args[0]);

var jArray = JsonConvert.DeserializeObject<JArray>(json);

var list = new List<Tuple<int, string>>();
var buttonActions = new List<String>();

foreach (var item in jArray)
{
    list.Add(new Tuple<int, string>(item.Value<int>("Item1"), item.Value<string>("Item2")));
}

foreach (var key in list)
{
    int id = Convert.ToInt16(key.Item1);
    string label = Convert.ToString(key.Item2);

    MenuItems.Add(new UIMenuItem($"{label}", $"Lasse eine Kopie des Schlüssels ~b~{label}~s~ anfertigen."));
    keycopyMenu.AddItem(MenuItems.Last());

    MenuItems.Last().Activated += (menu, item) =>
    {
        if (menu == keycopyMenu && item == MenuItems.Last())
        {
            RAGE.Ui.Cursor.Visible = false;
            Chat.Show(true);
            keycopyMenu.Visible = false;
            keycopyMenu.FreezeAllInput = false;

            Events.CallRemote("CopyKey_FromClient", id);
        }
    };
}

 

However, I have the problem that the MenuButtons/MenuItems do not call the "onclick" function because they only exist within the "foreach" loop.
Does anyone have any idea how I can provide the "for each individual element" (see below) event outside of the foreach loop?

MenuItems.Last().Activated += (menu, item) =>
{
    if (menu == keycopyMenu && item == MenuItems.Last())
    {
        RAGE.Ui.Cursor.Visible = false;
        Chat.Show(true);
        keycopyMenu.Visible = false;
        keycopyMenu.FreezeAllInput = false;

        Events.CallRemote("CopyKey_FromClient", id);
    }
};


 

Link to comment
Share on other sites

For those who are interested in the solution ...

i got it:

 

foreach (var key in list)
{
    int id = Convert.ToInt16(key.Item1);
    string label = Convert.ToString(key.Item2);

    MenuItems.Add(new UIMenuItem($"{label}", $"Lasse eine Kopie des Schlüssels ~b~{label}~s~ anfertigen."));
    keycopyMenu.AddItem(MenuItems.Last());

    TestFunction(MenuItems.Last(), label, id);
}


void TestFunction(UIMenuItem last, string label, int id)
{
    last.Activated += (menu, item) =>
    {
        if (menu == keycopyMenu && item == last)
        {
            RAGE.Ui.Cursor.Visible = false;
            Chat.Show(true);
            keycopyMenu.Visible = false;
            keycopyMenu.FreezeAllInput = false;
 
            Events.CallRemote("CopyKey_FromClient", id);
        }
    };
}

i had to write a function

Link to comment
Share on other sites

  • Spliddorama changed the title to [SOLVED] [C#] How to fill NativeUI with a <List> of Informations?

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...