Jump to content

Need help with script - Add item on ground when shooting?


Rausheim

Recommended Posts

 

This is from an outdated mod of mine. Not sure if it still works, but it drops an _item_ on the floor at the player's current position.

    player:getCurrentSquare():AddWorldInventoryItem(item, 0.0, 0.0, 0.0);

 

That should still work as far as I'm aware, RoboMat.  All you'd need to do then, Rausheim, is tie it to an OnWeaponSwing event and designate the conditional statement(s) and you're golden!

Link to comment
Share on other sites

Deadend: That's the plan, then reload em (that part I've finished and it's as close to realism as it can, including textures). I only need to figure out this script, but I can't find any documentation for scripting this game.

Link to comment
Share on other sites

Figured it out. Now I can add empty brass to the inventory each time I use a weapon, but now comes the tricky part, how to only do something when a specific gun is used. This is the script atm

ReloadingMod = {}function addEmpty9mmBrass()local player = getSpecificPlayer(0);local whatgun = player:getPrimaryHandItem();if whatgun == Pistol thenplayer:getInventory():AddItem("reloading.emptybrass9mm");endendEvents.OnWeaponSwing.Add(addEmpty9mmBrass);

This is the only thing that keeps me from releasing the reloading mod.

Link to comment
Share on other sites

You'll have to do something like this (pseudocode more or less):

 

    ReloadingMod = {}            local function addEmpty9mmBrass()                local player = getSpecificPlayer(0);                                -- Get the name. (Not sure if that's the correct function)...                local name = player:getPrimaryHandItem():getName();                                -- TODO remove when you have found correct function                print(name); -- ... print name to the console so you can find out.                -- Compare the name (string) to the weapon strings                if name == 'pistol' then                    player:getInventory():AddItem("reloading.emptybrass9mm");                elseif name == 'shotgun' then                    foo();                end            end            Events.OnWeaponSwing.Add(addEmpty9mmBrass);

 

Not sure what the correct function is for getting the item name. I'm sure you can find it here:

 

http://theindiestone.com/zomboidjavadocs/zombie/inventory/InventoryItem.html

 

The print() call will give you the correct names of your weapons, which you then fill into the strings of the if statement afterwards.

Link to comment
Share on other sites

Remember that getSpecificPlayer(0) only gets the player of that index in MP, if you want to get the player that you are, and if you are executing the code as client (Which should be the case here) you should use getPlayer() instead to get the dude you are playing as.

 

Otherwise you would get a problem where if I (player 2 (index 1)) shoots, the brass would spawn at the position of player 1 (index 0).

 

Hope that clears it up. Here is a script, based on what Robo wrote.

ReloadingMod = {} -- This is a meta-table holding your functions.	local function addEmptyBrass()		local player = getPlayer();	-- gets the player you are playing as.                	-- Get the name. (Not sure if thats the correct function)...	local name = player:getPrimaryHandItem():getName();                	-- TODO remove when you have found correct function	print(name); -- ... print name to the console so you can find out.	-- Compare the name (string) to the weapon strings	if name == "Pistol" then		player:getCurrentSquare():AddWorldInventoryItem("reloading.emptybrass9mm", 0.0, 0.0, 0.0);	-- This is 9mm brass	elseif name == "Shotgun" then		player:getCurrentSquare():AddWorldInventoryItem("reloading.emptyshell12ga", 0.0, 0.0, 0.0);	-- This is shotgun shells, rename the item to what it is called.	elseif name == "VarmintRifle" then		player:getCurrentSquare():AddWorldInventoryItem("reloading.emptybrass223", 0.0, 0.0, 0.0);	-- .223, rename to what it is in your code.	elseif name == "HuntingRifle" then		player:getCurrentSquare():AddWorldInventoryItem("reloading.emptybrass308", 0.0, 0.0, 0.0);	-- .308, rename to what it is in your code.	endend		Events.OnWeaponSwing.Add(addEmptyBrass);
Link to comment
Share on other sites

I was thinking about to add a condition using to check if the sound played is a shooting sound (9mmshot.ogg) etc but is this possible?

 

Current script:

ReloadingMod = {} -- This is a meta-table holding your functions.	local function addEmptyBrass()		local player = getPlayer();	-- gets the player you are playing as.                	-- Get the name. (Not sure if thats the correct function)...	local name = player:getPrimaryHandItem():getName();                	-- TODO remove when you have found correct function	print(name); -- ... print name to the console so you can find out.	-- Compare the name (string) to the weapon strings	if name == "Pistol" then		player:getCurrentSquare():AddWorldInventoryItem("reloading.emptybrass9mm", 0.0, 0.0, 0.0);	-- This is 9mm brass	elseif name == "Shotgun" then		player:getCurrentSquare():AddWorldInventoryItem("reloading.emptyshell12ga", 0.0, 0.0, 0.0);	-- This is shotgun shells, rename the item to what it is called.	elseif name == "Varmint Rifle" then		player:getCurrentSquare():AddWorldInventoryItem("reloading.emptybrass223", 0.0, 0.0, 0.0);	-- .223, rename to what it is in your code.	elseif name == "Hunting Rifle" then		player:getCurrentSquare():AddWorldInventoryItem("reloading.emptybrass308", 0.0, 0.0, 0.0);	-- .308, rename to what it is in your code.	endend		Events.OnWeaponSwing.Add(addEmptyBrass);

What I'm thinking (if someone can help it would be great):

ReloadingMod = {} -- This is a meta-table holding your functions.	local function addEmptyBrass()	local player = getPlayer();	-- gets the player you are playing as.                	-- Get the name. (Not sure if thats the correct function)...	local name = player:getPrimaryHandItem():getName();        local namesound = WHATEVERGETCODEISUSED():getName();                	-- TODO remove when you have found correct function	print(name); -- ... print name to the console so you can find out.	-- Compare the name (string) to the weapon strings	if (name == "Pistol") and (namesound == "9mmshot") then		player:getCurrentSquare():AddWorldInventoryItem("reloading.emptybrass9mm", 0.0, 0.0, 0.0);	-- This is 9mm brass	elseif (name == "Shotgun") and (namesound == "shotgun")  then		player:getCurrentSquare():AddWorldInventoryItem("reloading.emptyshell12ga", 0.0, 0.0, 0.0);	-- This is shotgun shells, rename the item to what it is called.	elseif (name == "Varmint Rifle") and (namesound == "shotgun")   then		player:getCurrentSquare():AddWorldInventoryItem("reloading.emptybrass223", 0.0, 0.0, 0.0);	-- .223, rename to what it is in your code.	elseif (name == "Hunting Rifle") and (namesound == "shotgun")   then		player:getCurrentSquare():AddWorldInventoryItem("reloading.emptybrass308", 0.0, 0.0, 0.0);	-- .308, rename to what it is in your code.	endend		Events.OnWeaponSwing.Add(addEmptyBrass);
Link to comment
Share on other sites

Hmmm I am unsure if you are capable of detecting if a certain sound is played. I haven't come accross something like that so far. Another approach could possibly be monitoring the amount of ammo in the gun and seeing if it becomes lower than it was at the start of the tick perhaps. Which would be the case for a weapon firing but not pushing a zombie...

 

I am not 100% certain about how I'd go about doing that though. I'd have to look into it a bit.

Link to comment
Share on other sites

I'll look into it today, sorry I didn't get back to you yesterday. Had food poisoning (What fun!).

 

***

 

Okay so I looked into it, and the only proper way of doing this without a bunch of redundant code would be to hook into the method used by the ISReloadManager to initiate a shot being fired. I managed to code that in and it works, it drops 'brass' when you shoot but not when empty or when pushing with a gun equipped.

 

Here is the script: ISReloadManager.lua

 

Remember to place it in this position in your mod filestructure in order to make it overwrite the vanilla stuff.

 

mods\MOD_NAME\media\lua\shared\Reloading\HERE

 

And be weary of new versions of zomboid that might alter the reloading system. Because you will then just need to take the fresh script from the future update, and modify it in a similar fashion by putting the function in and the call in ISReloadManager:fireShot().

 

You will see I commented the points in the script itself. I hope this helps you and can't wait to see the mod in action. :)

 

-Kind Regards

Vic

Edited by Viceroy
Link to comment
Share on other sites

Oh and, bump. Think that solves it. :D

 

EDIT: Ah and remember to just use that script and then the item declarations for the brass you made. Prefixed in the 'reloading' module. But I am sure you got that part working.

Edited by Viceroy
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...