I'm working on my first mod, involving my own custom shield, and I wanted to play around with the idea of granting a bonus of some kind for performing a perfect block. For example, I've been trying to see if I could make it so the shield grants a second or two of Rage, such that a counterattack with the weapon in the other hand deals more damage. However, I'm not sure how to properly set that up on the .activeitem file. Is there a way to have a status effect apply when the shield manages a perfect block?
Welcome to modding I'm guessing this could be done through a ".lua" file. If you look at the vanilla file "/items/active/shields/shield.lua", you can find the following if statement within the update function: Code: if status.resourcePositive("perfectBlock") then animator.setGlobalTag("directives", self.perfectBlockDirectives) else animator.setGlobalTag("directives", "") end This if statement seems to be checking for whether or not the player has performed a perfect block (and then tells the animator to do something). So--although I don't have the time to test it right now--I'm guessing you could make a new Lua file (say "perfect_rage.lua") with something similar to the following functions: Code: function init() -- We'll use this variable to make sure not to give the player the status effect multiple times per block. self.rageActive = false end function update(dt, fireMode, shiftHeld) -- First, check whether the player is performing a perfect block. if status.resourcePositive("perfectBlock") then -- Then, make sure we haven't all ready given him the rage effect. if not self.rageActive then status.addEphemeralEffect("rageEffectName", 1.5) self.rageActive = true end else -- If the player is not performing a perfect block, reset the "gate" variable. self.rageActive = false end end Then you would add this Lua file to the shield's ".activeitem" file, in the "scripts" array, like so: Code: "scripts" : ["/items/active/shields/shield.lua", "/items/active/shields/perfect_rage.lua"] Also, it might be helpful to have a look at this page to see some information on the different status controller functions ("status.resourcePositive", "status.addEphemeralEffect", etc.).
Gave it a try - basically copy/pasted that second block of code into its own .lua file. Unfortunately, when I add that third bit for the scripts, the game crashes the moment I pull out the shield. Where do I put that .lua file inside the mod folder? In with the images folder and .activeitem file, or in the same spot as the .metadata?
You can put the Lua file wherever you like (in your custom shield's folder is fine), you just have to put that Lua file's path into the "scripts" array in the active item file. I had given the example of "/items/active/shields/perfect_rage.lua", but perhaps your path is more like "/items/active/shields/new_shield/perfect_rage.lua", or maybe "/items/new_shield/perfect_rage.lua". Also, that "scripts" array should all ready exist in your active item file, in which case you would add the path to the existing "scripts" array (rather than making two of the same array). You probably did it this way, but I thought I'd mention it, just to be sure.
Okay, I think I got the script figured out, it didn't crash as I loaded it up (although there was a weird glitch where my character was holding the shield in a locked position). Unfortunately, it doesn't seem to be granting the Rage effect, even secretly. There's no flash of the rage status on the HUD, and some testing showed that my weapons were no more powerful at any period after the block. Any advice? EDIT: Keep getting that glitch when I set it all up right. Holds the shield in his hand like a harp, out in front.
Hm. I'm not sure. It may have something to do with there being two "init" functions now (one might be overriding the other). To confirm this--if you want to give it a try--you could merge the "shield.lua" and the "perfect_rage.lua" into one file (with only one "init" and "update" function), and add that new single file path as the content of the "scripts" array: Code: "scripts": ["perfect_rage_shield.lua"] But if not, I'll try to do some testing tomorrow, and see what I can find.
Did some quick testing - no dice. Not sure what's up here. I confess this is almost entirely out of my field off knowledge - I have no idea how to write an .lua file, let alone set this up. I'll keep poking around, see what I can do.
this is actually something I'm looking into myself heh, although instead of a buff, I'm just thinking of a perfect block dealing damage as if it were a "shield bash" so to speak I've got a friend who's an absolute lua wizard who'll be helping with me on that though, if you're unable to figure this out I'm sure they could whip up something for you based on what I'm trying to do
That would be fantastic! I'd love to hear from your friend if you guys manage to make it all work - honestly, I had long since given up on a proper shield-bash and settled for this instead.
So, it turns out that having scripts with the same functions, cause the earlier functions to be overwritten (or--perhaps--just stopped from being called). So only the "init" and "update" functions from the new Lua file were being used--hence the error with your character holding his shield funny. I've attached the mod I made to test the perfect block bonus, so you can have a look at that to see how to get things working. The main bit of code would be the Lua file "perfect_rage.lua" which contains a single function named "raiseShield". This function is the same as the vanilla one (in "shield.lua") except for the addition of the line Code: status.addEphemeralEffect("thorns", 5.0) Here I used the vanilla status effect "thorns" (applied for 5 seconds), but you can change that to whatever effect name you like A couple things I learned: The "rageActive" variable wasn't needed, as every time a perfect block is performed, the duration of the status effect is reset (I was afraid it would get added twice, or something). It turns out that the "perfectBlock" resource we were checking for is assigned a positive value regardless of whether or not the player's shield is struck. Therefore, I moved the effect application out of the "update" function, and into the damage listener (inside the "raiseShield" function), which checks to see if the player was attacked while in the perfect block window. I was hoping not to have to overwrite any of the original Lua, but I couldn't find a way to modify the damage listener without replacing the "raiseShield" function. Anyway, maybe you'll get that shield-bashing feature, and won't need any of this, but this should work if you want it. - Aside - For anyone interested, I also found (thanks to an older post) that if you have multiple Lua scripts applied to the same item/file, you can actually use the same function names, but you have to store them in variables and call them yourself, like so: Code: local oldInit = init function init() oldInit() -- New code. end Unfortunately this wasn't of much use in this shield scenario as--it seems--the damage listener can only be given assigned a single function. If the vanilla damage listener function had been defined globally (and given a name) rather than being anonymous, then I might have been able to make my own and call the original from within, but alas, it was not so.
Gave it a test-run - worked like a charm! I changed "thorns" to "rage" and adjusted stuff for the name, animation and sfx of the shield, but otherwise it worked perfectly! Thanks so much for the help - I'll be sure to credit you when I post my new shield publicly!
It's up! Thanks again for everything - you've been credited in the description for the huge help. https://steamcommunity.com/sharedfiles/filedetails/?id=2470140417&searchtext=Power+Shield
if you'd like, I could still share the code for the "shield bash" effect with you once my friend has gotten it ready
in theory it should work by making the perfect block effect apply damage to enemies, still trying to get it to work though
alright, sorry for taking so long, but I have the code here. apparently this also ended up involving statuseffects amusingly enough. This was made with the invaluable assistance of my friend silver sokolova.
Yeah, no problem, I tend to come and go here anyway. I'm guessing it's a typo in the "shieldbashdamage.statuseffect" that lists the script as "r-shieldbashdamage.lua" even though the Lua file is named "shieldbashdamage.lua": Code: { "name" : "r-shieldbashdamage", "effectConfig" : {}, "defaultDuration" : 0.1, "scripts" : ["r-shieldbashdamage.lua"] } That aside, it's pretty cool that it takes so little code. I'm not too familiar with sending messages to entities, but this will be good to keep in mind for later. Thanks for sharing (and to your friend).