Jump to content
Sign in to follow this  
ORMtnMan

Question about ReloadUtil

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

Share this post


Link to post
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. 

Share this post


Link to post
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.

Share this post


Link to post
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);

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Here's the problem guys,

 

I get a CTD to desktop when reloading some of my weapons in multiplayer. I will however try out your suggestions guys. 

Share this post


Link to post
Share on other sites

Oh well multiplayer is still an enigma to me, I was planning to look into it once my mod work in SP.

 

But if you post the stack trace of the error message I might be able to help.

Share this post


Link to post
Share on other sites

Hello ORM, i am Billwa's brother.

 

Would you like us to upload a picture of what error we are seeing so you can get a better visual?

 

Yep, that would definitely help!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...