Rausheim Posted August 11, 2014 Share Posted August 11, 2014 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. Link to comment Share on other sites More sharing options...
RoboMat Posted August 11, 2014 Share Posted August 11, 2014 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); Link to comment Share on other sites More sharing options...
Pravus Posted August 11, 2014 Share Posted August 11, 2014 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 More sharing options...
Deadend Posted August 11, 2014 Share Posted August 11, 2014 now let me see those bullet shells drop! Link to comment Share on other sites More sharing options...
Rausheim Posted August 11, 2014 Author Share Posted August 11, 2014 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 More sharing options...
Viceroy Posted August 11, 2014 Share Posted August 11, 2014 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. Link to comment Share on other sites More sharing options...
Rausheim Posted August 11, 2014 Author Share Posted August 11, 2014 For some reason I can't get even the tutorial scripts to work. I've renamed the txt file to lua and everything. Link to comment Share on other sites More sharing options...
Rausheim Posted August 12, 2014 Author Share Posted August 12, 2014 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 atmReloadingMod = {}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 More sharing options...
RoboMat Posted August 12, 2014 Share Posted August 12, 2014 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 More sharing options...
Viceroy Posted August 12, 2014 Share Posted August 12, 2014 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 More sharing options...
Rausheim Posted August 12, 2014 Author Share Posted August 12, 2014 Thank you guys so much! Now only to figure out how to only give brass when actually shooting. This is frickin' awesome. Link to comment Share on other sites More sharing options...
Rausheim Posted August 13, 2014 Author Share Posted August 13, 2014 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 More sharing options...
Viceroy Posted August 13, 2014 Share Posted August 13, 2014 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 More sharing options...
Rausheim Posted August 13, 2014 Author Share Posted August 13, 2014 I've been looking inside the Reloading folder and there are a lot of files, including "what to do" when firing a weapon. Might be something there. Link to comment Share on other sites More sharing options...
EnigmaGrey Posted August 13, 2014 Share Posted August 13, 2014 If OnWeaponSwing isn't called when a weapon is out of ammo, all you'd have to check is whether a gun is equipped. Link to comment Share on other sites More sharing options...
Viceroy Posted August 13, 2014 Share Posted August 13, 2014 Ah yeah but you can still melee with a gun and push zombies back, also triggering OnWeaponSwing if I am not mistaken. Link to comment Share on other sites More sharing options...
EnigmaGrey Posted August 13, 2014 Share Posted August 13, 2014 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. Link to comment Share on other sites More sharing options...
Rausheim Posted August 13, 2014 Author Share Posted August 13, 2014 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. Link to comment Share on other sites More sharing options...
Viceroy Posted August 14, 2014 Share Posted August 14, 2014 (edited) 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 RegardsVic Edited August 14, 2014 by Viceroy Link to comment Share on other sites More sharing options...
Viceroy Posted August 14, 2014 Share Posted August 14, 2014 (edited) Oh and, bump. Think that solves it. 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 August 14, 2014 by Viceroy Link to comment Share on other sites More sharing options...
Rausheim Posted August 14, 2014 Author Share Posted August 14, 2014 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. Viceroy 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now