Jump to content
The Indie Stone Forums
Rausheim

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

Recommended Posts

Hey, could anyone help me with this type of script.

 

If shooting with a pistol, add a certain item on the ground where the player are or inside his inventory, each time the gun is fired.

Share this post


Link to post
Share on other sites

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);

Share this post


Link to post
Share on other sites

 

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!

Share this post


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

Share this post


Link to post
Share on other sites

Remember to prefix the item name with the module name. For example planks would be "Base.Plank" for item name. :)

 

Not sure if 100% needed but it is clearer.

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

Ah yeah but you can still melee with a gun and push zombies back, also triggering OnWeaponSwing if I am not mistaken.

You're right -- with build 28, that's a distinct possibility. :D

Share this post


Link to post
Share on other sites

Any way to check if a gun is loaded or not? I can't make heads or tails cos alot of the code in the files (I think are related to guns) are repeated several times in all files.

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

Awesome, just frickin' awesome. It's also pretty straight forward, since this should not be difficult to add in future versions of PZ if they do something to that script.

 

Thanks alot and the mod is out.

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

×
×
  • Create New...