Jump to content

Recommended Posts

Posted (edited)

Hello,
 

I want to create an inventory system.

 

mp.events.addCommand("inventory", (player) => {
    gm.mysql.handle.query('SELECT * FROM items', [], function (error, results, fields) {
		for(let i = 0; i < results.length; i++) {
		if(results[i].owner == player.name)
		{
		player.call("Inv", [player,results[i].name);
		player.call("Inventory", [player, "test"]);
		}
	}
	});
	
	
	
});

This is the Command to call the Inventory.


And this the Inventory:


 

mp.events.add({
	"Inv": (player,name) => {

		inventory.AddItem(Inv = new UIMenuItem(""+name,""));

			
	
	
	}
});

 

Every time I go to the menu, the same menu items are created again.

Have anyone a solution?

 

Thanks.

 

Greetings Boomray


 
Edited by Boomray
Posted

Well you should think in general about what you want to do here. Why do you fetch all results in Database ans WHILE fetching you check if the item is for the player? Also do you think its a good idea to always make a direct mysql interaction everytime somebody opens inventory? Why not caching in a player variable? There is to less code to help you out but i guess you always overwrite the inventory . You have to set a variable on client side above your function and let the function fill it. But as i said i wouldn't do it this way

 

Posted

Ok thanks.

I have found this, and will try to use it:

 
My Server-Side Code: 

mp.events.addCommand("inventory", (player) => {
    const inventory = player.getInventory();

    player.outputChatBox("Your inventory:");
    inventory.forEach((item, index) => {
		player.call("Inv", [player,invAPI.getItemName(item.key)]);
		player.call("Inventory", [player, "test"]);
    });
});


and the Client-Side Code:

 

mp.events.add({
	"Inv": (player,name) => {

		inventory.AddItem(inventor = new UIMenuItem(""+name,""));

			
	
	
	}
});

mp.events.add({
	"Inventory": (player) => {
		if (playerMenu.Visible |
			inventory.Visible) {
			pMenus.forEach(function(element, index, array){element.Close()});
		} else {
			
			inventory.Open();
			mp.gui.chat.show(false);
			mp.gui.cursor.visible = false;
		}
	}
});
What I have to do so that the "AddItems" do not always recreate?

Greetings Boomray

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...