Jump to content

Question about ReloadUtil


ORMtnMan

Recommended Posts

Hey all,

 

more questions from the ORMtnMan.

 

Does the ReloadUtil point only to a specific file? If so where? There is an ISReloadUtil which has the functions I see being called.

 

I tried putting a function in my ORGMReloadUtil, however when it is called via ReloadUtil it throws a nil...

 

I tried replacing ReloadUtil with ORGMReloadUtil and I get a slew of lua exceptions from all the lua scripts involved so I don't think that is the way to go...

 

Anyway,

 

Here is the block that is throwing the original nil value

 

The isUnloadable function is under my ORGMReloadUtil

 

I will keep fiddling with it, but if someone figures it out before I do...

 

Edit:

 

I changed my code a bit and my console tells me this:

xhVEX1G.png

When I follow it through it seems to be the playerObj is where the current problem lies.

 

Here is the block of code as it is now, the bolded bit is where it jumps out of this particular block and eventually runs into an error

 

ORGMInventoryUnloadMenu.createMenu = function(player, context, items)

    local isUnloadable = false;
    
    local playerObj = getSpecificPlayer(player)
    
    for i,v in ipairs(items) do
        local testItem = v;
        if not instanceof(v, "InventoryItem") then
            testItem = v.items[1];
        end
        if(ORGMReloadUtil:isUnloadable(testItem, player)) then
            isUnloadable = true;
        end
    end
    
    if isUnloadable then
        local item = items[1];
        if not instanceof(items[1], "InventoryItem") then
            item = items[1].items[1];
        end
        context:addOption("Unload", items, ORGMInventoryUnloadMenu.OnUnload, playerObj);
    end
end

 

ORGMInventoryUnloadMenu.OnUnload = function(items, player)
    local weapon = items[1];
    if not instanceof(items[1], "InventoryItem") then
        weapon = items[1].items[1];
    end
    ORGMUnloadManager:startUnloadFromUi(weapon);
end

Events.OnFillInventoryObjectContextMenu.Add(ORGMInventoryUnloadMenu.createMenu);

 

EDIT EDIT:.

 

Okay, I found a workaround but found another issue. I think I am on the right track though, so ignore this question now...

Link to comment
Share on other sites

  • 3 weeks later...

Hey ORGM, I am having the same problem

 

Can you inform me on who to fix this error? Its really pissing me off, because I'm trying to create weapons and it ends up crashing my game. 

 

 

Thanks, 

 

Billwa. 

Link to comment
Share on other sites

Hey ORGM, I am having the same problem

 

Can you inform me on who to fix this error? Its really pissing me off, because I'm trying to create weapons and it ends up crashing my game. 

 

 

Thanks, 

 

Billwa. 

 

I'm not near my ccomputer I did my coding on, but I'll review what I did when I get home and get back to you.

 

Also, you could message me with what you are trying to accomplish and I'll try and help you.

Link to comment
Share on other sites

Hey ORGM, I am having the same problem

 

Can you inform me on who to fix this error? Its really pissing me off, because I'm trying to create weapons and it ends up crashing my game. 

 

 

Thanks, 

 

Billwa. 

 

Okay, so the workaround I did for this was to do a ammo check to see if it was unloadable instead of trying to use the ReloadUtil.

 

Now my hypothesis on why my original code is as follows: (though as Herman Cain said "I don't have facts to back this up")

 

The IsUnloadable script I was trying to reference is a copy of the isReloadable script from the vanilla game.

The original Reload scripts reference ReloadUtil instead of a specific script, this (I think) is because it is a java class and it allows for the referencing of weapons added to the reload list, that were not vanilla weapons. If I referende ISReloadUtil I only get base game guns.

So, the Java ReloadUtil does not have a function of isUnloadable. So I tried to reference my own script, however, it was having issues bringing in the individual reload Type luas, so it would throw a nil

 

Again, this is a hypothesis from a person who has no formal training in coding and has a tenuous understanding about how the PZ code works.

 

Here is what my workaround looked like, the bolded bit is the workaround itself.

ORGMInventoryUnloadMenu.createMenu = function(player, context, items)

    local isUnloadable = false;

    

    local playerObj = getSpecificPlayer(player)

    

    for i,v in ipairs(items) do

        local testItem = v;

        if not instanceof(v, "InventoryItem") then

            testItem = v.items[1];

        end

        if testItem:getModData().currentCapacity ~= nil then

            capacity = testItem:getModData().currentCapacity

        else

            capacity = 0

        end

        if testItem:getModData().roundChambered ~= nil then

            chambered = testItem:getModData().roundChambered

        else

            chambered = 0

        end

            unloadtester = capacity + chambered

        if(unloadtester > 0) then

            isUnloadable = true;

        end

    end

    

    if isUnloadable then

        local item = items[1];

        if not instanceof(items[1], "InventoryItem") then

            item = items[1].items[1];

        end

        context:addOption("Unload", items, ORGMInventoryUnloadMenu.OnUnload, playerObj);

    end

end

ORGMInventoryUnloadMenu.OnUnload = function(items, player)

    local weapon = items[1];

    if not instanceof(items[1], "InventoryItem") then

        weapon = items[1].items[1];

    end

    ORGMUnloadManager:startUnloadFromUi(weapon);

end

Events.OnFillInventoryObjectContextMenu.Add(ORGMInventoryUnloadMenu.createMenu);

Link to comment
Share on other sites

I'm currently working on a complex crossbow crafting  mod to be released soon and this is what you need to do

 

You create some .lua file in your mod's media/lua/shared directory that builds a weapon object that corresponds to one of your items (type/module must match). Then you call RealoadUtil:addWeaponType(object) to register it. This will set up your weapon in the system.

 

Example

local Xbow = { type = 'WoodenCrossbow',	       moduleName = 'ZXB',	       reloadClass = 'ZXBCrossbowWeapon',	       ammoType = 'WoodenBolt',		       shootSound = 'crossbow',	       rackSound = 'bulletOutVarmint',               clickSound = 'stormyShotgunClick',               insertSound = 'bulletInRifle',		       rackTime = 0};	ReloadUtil:addWeaponType(Xbow);

Note the reloadClass property. This defines the class that is called when you press 'R' to reload or when you fire a shot. What I did was to copy the existing ISShotgunWeapon.lua, renamed it and modified the methods the way I needed them to work (my crossbows support different kinds of ammunition and i disabled racking and such). This is how you can adjust anything that is related to reloading/ racking and firing. 

 

 

Edit:

 

To elaborate on ORMtnMans last code example. (Btw @ORMtnMan I looked at your mod when I first got started: Excellent work!! )

 

The more clean way to do the unload would be to create an 'unLoad' method in the class you referenced with the reloadClass property above.

 

This is because this class already contains all information about the state of your gun such as roundChambered, currentCapacity, maxCapacity, ammoType and such.

 

To call this method from a UI hook you'd use ReloadUtil:getReloadableWeapon(weapon, character); to get the instance of your reloadClass and then call unload on it:

 

 

So here is ORMtnMan's example from above abit cleaner:

 

YourUI.lua

ORGMInventoryUnloadMenu.OnUnload = function(items, player)    local weapon = items[1];    if not instanceof(items[1], "InventoryItem") then        weapon = items[1].items[1];    end    local playerObj = getSpecificPlayer(player);    local reloadClass = ReloadUtil:getReloadableWeapon(weapon, playerObj);    reloadClass:unLoad(playerObj);endEvents.OnFillInventoryObjectContextMenu.Add(ORGMInventoryUnloadMenu.createMenu);

Your reloadClass (ISShotGunWeapon.lua copy) (This was written @work so it is not 100% accurate but you can look at the existing reloadPerfom method and to exactly the reverse for unload)

function YourReloadClass:unLoad(player)   local totalAmmoInGun = self.currenCapacity + self.roundChambered   if totalAmmoInGun > 0 then      -- Add ammo to players inventory      for i=0, i < totalAmmoInGun do         player:getInventory():AddItem(self.ammoType);      end      -- Set variables to 0 to 'empty' the gun      self.currentCapacity = 0;      self.roundChambered = 0;   endend

Note: If you want the unload to take some time (green progress bar above characters head) you will have to create a ISTimeBasedAction and do the reloadClass = ReloadUtil:getReloadableWeapon(weapon, playerObj); and reloadClass:unLoad(playerObj); from there. There are plenty of example for TimeBasedActions in the project zomboid code

Link to comment
Share on other sites

I'm currently working on a complex crossbow crafting  mod to be released soon and this is what you need to do

 

You create some .lua file in your mod's media/lua/shared directory that builds a weapon object that corresponds to one of your items (type/module must match). Then you call RealoadUtil:addWeaponType(object) to register it. This will set up your weapon in the system.

 

Example

local Xbow = { type = 'WoodenCrossbow',	       moduleName = 'ZXB',	       reloadClass = 'ZXBCrossbowWeapon',	       ammoType = 'WoodenBolt',		       shootSound = 'crossbow',	       rackSound = 'bulletOutVarmint',               clickSound = 'stormyShotgunClick',               insertSound = 'bulletInRifle',		       rackTime = 0};	ReloadUtil:addWeaponType(Xbow);

Note the reloadClass property. This defines the class that is called when you press 'R' to reload or when you fire a shot. What I did was to copy the existing ISShotgunWeapon.lua, renamed it and modified the methods the way I needed them to work (my crossbows support different kinds of ammunition and i disabled racking and such). This is how you can adjust anything that is related to reloading/ racking and firing. 

 

Erm...  I understand that... but the ReloadUtil wouldn't pickup Unloading... at least not that I could get...

 

Anyway, I have my code set up for all of the above you mentioned. The issue was the Ui button I was trying to make. It is fixed now anyway.

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