Jump to content

Preventing a context menu from instantly executing?


ethanwdp

Recommended Posts

I've been working on making a time warp in my cheat mod. Problem is, I want to give the user the ability to set it to any hour he wants, not just Morning and Noon and Night/etc, so I decided to test out passing arguments to a function so that I can neatly handle this in a single function. Problem is, there seems to be an issue where it executes even if I just right click to open a context menu once. So I right click, it instantly executes (undesirably), and clicking the menu normally doesn't do anything. After Googling, it seems that putting the brackets will make Lua instantly call it, which is a problem because I need those brackets to pass an argument.

 

ISUICheatMenu.createMenuEntries = function(_player, _context, _worldObjects)

    local context = _context;
    local worldobjects = _worldObjects;
    ISUICheatMenu.player = getSpecificPlayer(_player);
    
    for i,object in ipairs(worldobjects) do -- saves clicked square pos, basically
        if object:getSquare() then
                ISUICheatMenu.square = object:getSquare();
        end
    end
    
    -------------------------
    --Making the Cheat Menu--
    -------------------------
    
    local CheatOption = context:addOption("Cheat Menu", worldobjects, nil); -- Makes the cheat menu.
    local subMenu = ISContextMenu:getNew(context); -- As it is a submenu, I create a new submenu.
    context:addSubMenu(CheatOption, subMenu); -- I add the menu to the submenu, otherwise it won't appear.
    local GodOption = subMenu:addOption("God", worldobjects, ISUICheatMenu.GodToggle); --I add a new option to subMenu and set it to trigger the GodToggle function
    local CreativeOption= subMenu:addOption("Creative", worldobjects, ISUICheatMenu.CreativeToggle);
    local AmmoOption = subMenu:addOption("Inf. Ammo", worldobjects, ISUICheatMenu.AmmoToggle);
    local NoReloadOption = subMenu:addOption("No delay between shots", worldobjects, ISUICheatMenu.NoReloadToggle);
    local SkillOption = subMenu:addOption("Max All Skills", worldobjects, ISUICheatMenu.MaxStats);
    local CarryWeightOption = subMenu:addOption("Infinite carryweight", worldobjects, ISUICheatMenu.CarryWeightMax);
    local SpeedOption = subMenu:addOption("Run fast", worldobjects, ISUICheatMenu.GottaGoFast);
    local HealOption = subMenu:addOption("Heal yourself", worldobjects, ISUICheatMenu.DoHeal);
    local PreventDeathOption = subMenu:addOption("Prevent death", worldobjects, ISUICheatMenu.PreventDeath);
    local MeleeInstaKill = subMenu:addOption("Insta-kill melee", worldobjects, ISUICheatMenu.MeleeInstaToggle);
    local RepairHandItem = subMenu:addOption("Repair equipped item", worldobjects, ISUICheatMenu.DoRepair);
   
    -----------------------------
    --Making the time warp menu--
    -----------------------------
    
    local TimeOption = subMenu:addOption("Set time to...", worldobjects, nil);
    local subMenuTime = subMenu:getNew(subMenu);
    context:addSubMenu(TimeOption, subMenuTime);
    local MorningOption = subMenuTime:addOption("Morning/9 A.M", worldobjects, ISUICheatMenu.SetTime(9))
    local NoonOption = subMenuTime:addOption("Noon/12 P.M", worldobjects, ISUICheatMenu.SetTime(12))
end

ISUICheatMenu.SetTime = function(TimeToSet)
    local TimeToSet = TimeToSet
    local time = getGameTime()
    time:setTimeOfDay( TimeToSet )
end

 

(some of those don't call anything, mainly because this is a clipped version of the 500 line monstrosity I'm trying to minimize :P)

 

So when clicking MorningOption, it passes ISUICheatMenu.SetTime with the time I want to set, and SetTime saves the argument into a variable and sets the time to it. But again, it instantly executes.

How do I prevent this?

Link to comment
Share on other sites

Of course it will be instantly executed. Using the brackets aka (9) calls the function. You could try using an anonymous function as a wrapper:

 

local MorningOption = subMenuTime:addOption("Morning/9 A.M", worldobjects, function() ISUICheatMenu.SetTime(9) end)

 

If that doesn't work you'll have to use the ugly solution of creating a function for each variable (9, 12, etc.).

Link to comment
Share on other sites

Of course it will be instantly executed. Using the brackets aka (9) calls the function. You could try using an anonymous function as a wrapper:

local MorningOption = subMenuTime:addOption("Morning/9 A.M", worldobjects, function() ISUICheatMenu.SetTime(9) end)

If that doesn't work you'll have to use the ugly solution of creating a function for each variable (9, 12, etc.).

Yeah, I guess I learned that brackets calls it the hard way lol.

I eventually figured out that I can still pass a function.

 

local MorningOption = subMenuTime:addOption("Morning/9 A.M", worldobjects, ISUICheatMenu.SetTime, 9)

Turns out that there was another argument I could pass. *facedesk*

 

I'm hoping that the Project Zomboid modding resource gets updated soon, so I have an idea of what it is I'm calling. I still have no idea what worldobjects is doing (mindmod did it, so I assumed it was important) or if there are more arguments I can call.

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