Jump to content

Giving custom options to items when you right click them


MisterInSayne

Recommended Posts

So, here is the next code sniplet! We all know the menu that pops up when you right click an item. This is how you add options to it, for specific items!

yourContextMenuObjectName = {}; -- Change "yourContextMenuObjectName" to the name you want, it needs to be different in every file.yourContextMenuObjectName.doMenu = function(player, context, items)		for i,v in ipairs(items) do		local item = v;        if not instanceof(v, "InventoryItem") then            item = v.items[1];        end				if item:getType() then			-- this is where you put your code!			if item:getType() == "MyItemID" then -- Change MyItemsID to the ID you used in your items.txt file. (so for base.Axe this would be Axe)								context:addOption("Do the thing with the thing", item, yourContextMenuObjectName.onTheThingWithTheThing, player);							end			-- this is where you put your code!		end	endendyourContextMenuObjectName.onTheThingWithTheThing = function(item, player)	-- This is where the code goes for when someone clicks the option in the menu	local playerObj = getSpecificPlayer(player);		playerObj:Say("Doing the thing with " .. item:getName());	-- This is where the code goes for when someone clicks the option in the menuend-- The link to the right click menu evenEvents.OnFillInventoryObjectContextMenu.Add(yourContextMenuObjectName.doMenu);

This lua file goes into the media/lua/client/ folder of your mod.

 

If you have any questions, just ask! :D Hope it helps! <3

 

Link to comment
Share on other sites

Personally I prefer to make all functions local by default. So instead of GlobalTable.doMenu you can simply call doMenu. It's generally a good idea to avoid globals whenever you can. I think the require call has been fixed in one of the recent versions so you could in theory code completely without globals.

 

As an aside: Declaring functions using the syntactic sugar (e.g. local function foo() print('Hello World') end) has some benefits. For example if you want to call it recursively you won't have to pre-declare it.

 

Here is my old tutorial on the topic: http://pz-mods.net/guide/how-to-create-inventory-context-menus/

Link to comment
Share on other sites

Personally I prefer to make all functions local by default. So instead of GlobalTable.doMenu you can simply call doMenu. It's generally a good idea to avoid globals whenever you can. I think the require call has been fixed in one of the recent versions so you could in theory code completely without globals.

 

As an aside: Declaring functions using the syntactic sugar (e.g. local function foo() print('Hello World') end) has some benefits. For example if you want to call it recursively you won't have to pre-declare it.

 

Here is my old tutorial on the topic: http://pz-mods.net/guide/how-to-create-inventory-context-menus/

 

My hero! <3 I'm still learning myself too, only started with Lua 4-5 days ago, likewise with PZ modding, but figured I should share my findings, as everyone now uses recipe's for EVERYTHING. You're completely right, and I feel pretty silly now that I haven't been using it that way. Thank you, this helped me a lot! :D

Link to comment
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
×
×
  • Create New...