MisterInSayne Posted September 28, 2015 Share Posted September 28, 2015 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! Hope it helps! <3 Link to comment Share on other sites More sharing options...
RoboMat Posted September 28, 2015 Share Posted September 28, 2015 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/ MisterInSayne 1 Link to comment Share on other sites More sharing options...
MisterInSayne Posted September 28, 2015 Author Share Posted September 28, 2015 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! Link to comment Share on other sites More sharing options...
RoboMat Posted September 28, 2015 Share Posted September 28, 2015 You gotta start somewhere - If you look at my tutorial you can see that back in the day I didn't know about those little tweaks either MisterInSayne 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now