Jump to content

Recommended Posts

I'm completely new to lua and still struggling to get my mind around the basics; as the title suggests, I'm trying to make a character that gets a damage bonus when using certain existing weapons (farming implements), but I haven't been able to find any existing files that give me an indication of how to do this. Anyone able to help?

Hm, nobody can give me a hand with this?

Well, assuming you know where to put it (hint: your character's constructor):

inst:DoPeriodicTask(0, function(inst)    local weapon = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HAND)    if weapon:HasTag("mod_multipliedweapon") then        local boost = (whatever multiplier you want)        local newdamage = weapon.components.weapon.damage * boost        if weapon.components.weapon then            weapon.components.weapon:SetDamage(newdamage)        end    endend)

You'll then want to give each weapon the "mod_multpliedweapon" tag through your modmain.

local function addmultipliedweapontag(inst)    inst:AddTag("mod_multipliedweapon")end

For example, to add it to the nightsword:

AddPrefabPostInit("nightsword", addmultipliedweapontag)
Edited by debugman18

Firstly, thanks a lot for the help, I appreciate it!

 

I'm getting a crash: "attempt to index local 'weapon' (a nil value)". I've fiddled around with it a bit without luck, I'm not sure why it's failing to set the value correctly. Any ideas?

Firstly, thanks a lot for the help, I appreciate it!

 

I'm getting a crash: "attempt to index local 'weapon' (a nil value)". I've fiddled around with it a bit without luck, I'm not sure why it's failing to set the value correctly. Any ideas?

Yes, it's an issue in the code snippet I gave you.

 

Change

if weapon:HasTag("mod_multipliedweapon") then

to

if weapon and weapon:HasTag("mod_multipliedweapon") then

If it tells you that EQUIPSLOTS is nil, then change EQUIPSLOTS.HAND to _G.EQUIPSLOTS.HAND.

Edited by debugman18

Hm, okay. That fixed the error, but the damage bonus doesn't seem to be applying itself. 

 

I've got this in my modmain.lua:

-- add multiplier for farming implementsfunction addmultipliedweapontag(inst)    inst:AddTag("mod_multipliedweapon")endAddPrefabPostInit("shovel", addmultipliedweapontag)AddPrefabPostInit("pitchfork", addmultipliedweapontag)

And this in my character prefab, under the local fn = function(inst):

 

-- boost set high for testing purposesinst:DoPeriodicTask(0, function(inst)   local weapon = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HAND)   if weapon and weapon:HasTag("mod_multipliedweapon") then       local boost = 100       local newdamage = weapon.components.weapon.damage * boost       if weapon.components.weapon then           weapon.components.weapon:SetDamage(newdamage)       end   endend)

Am I missing anything obvious?

Hm, okay. That fixed the error, but the damage bonus doesn't seem to be applying itself. 

 

I've got this in my modmain.lua:

-- add multiplier for farming implementsfunction addmultipliedweapontag(inst)    inst:AddTag("mod_multipliedweapon")endAddPrefabPostInit("shovel", addmultipliedweapontag)AddPrefabPostInit("pitchfork", addmultipliedweapontag)

And this in my character prefab, under the local fn = function(inst):

 

-- boost set high for testing purposesinst:DoPeriodicTask(0, function(inst)   local weapon = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HAND)   if weapon and weapon:HasTag("mod_multipliedweapon") then       local boost = 100       local newdamage = weapon.components.weapon.damage * boost       if weapon.components.weapon then           weapon.components.weapon:SetDamage(newdamage)       end   endend)

Am I missing anything obvious?

Hmm, what shows up in your log? It might help to add print(newdamage) after declaring newdamage, and to add print(weapon.prefab) in the if block. That way we can see what's being passed.

 

Edit: Oh, I feel silly. It should be EQUIPSLOTS.HANDS. Plural. :p

Edited by debugman18

Excellent! Works like a charm now. Much more elegant a bit of code than I figured I'd have to use, as well. Thanks a lot. :-)

 

Edit: Oops, wait, there are a couple of problems. It's calling the function over and over again whilst the weapon is equipped and increasing the damage each time. I modified it a little to make it call only once, but then encountered another problem - the damage doesn't change back to normal when the weapon's unequipped, so unequipping and reequipping it repeatedly causes repeated damage increases. Still, I have a much better idea of how things work now than before.

 

Further edit: Got it working with a bit of tweaking. Probably fairly clumsy in how I did it, but it was a good learning experience. Thanks again, wouldn't have had a clue where to start without the help :)

Edited by halberdsturgeon

you could put something like this in the modmain. Much smaller and works just fine:

AddPrefabPostInit("spear", function(inst)if GetPlayer().prefab == "Your character's name" then     inst.components.weapon:SetDamage(How much damage you want it to do)endend)

Hm... when I put that in the modmain, I get an error: "function arguments expected near '='".

 

Edit: Wait, no, my bad, I must've been entering it wrong. It is giving me a different error, though - something about GetPlayer() being a nil value?

 

Edit edit: Nevermind, I'm a fool, it works perfectly well. Must have been because I was testing it on Wilson's prefab.

Edited by halberdsturgeon

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