Jump to content

Recommended Posts

I am adding this AddPrefabPostinit to my modmain

AddPrefabPostInit("diviningrod", function(inst)if GLOBAL.GetPlayer().prefab == "trent" then 	inst:AddTag("sharp")	inst:AddComponent("weapon")    inst.components.weapon:SetDamage(55)endend)

and it gives me this error

[00:01:20]: error calling PrefabPostInit: diviningrod in mod Trent - DST (Trent Character): [string "../mods/Trent - DST/modmain.lua"]:67: attempt to index a nil value

What am I doing wrong?

Edited by ELEMENTALCRAFTER009
Link to comment
Share on other sites

@ELEMENTALCRAFTER009,

 

(a) GetPlayer shouldn't be used in DST

(b) the player objects will not exist at the time the postinit is run if there is a divining rod in the world

 

If (a) and (b) were solved, then this would make the divining rod do 55 damage for everyone in the game if (and only if) the host was trent.

 

So instead, you should probably have this modification occurring in an onequip (and restoring in an onunequip). That way you can make sure that when trent has it, it does damage, but otherwise doesn't.

Edited by rezecib
Link to comment
Share on other sites

@ELEMENTALCRAFTER009,

(a) GetPlayer shouldn't be used in DST

(b) the player objects will not exist at the time the postinit is run if there is a divining rod in the world

If (a) and (b) were solved, then this would make the divining rod do 55 damage for everyone in the game if (and only if) the host was trent.

So instead, you should probably have this modification occurring in an onequip (and restoring in an onunequip). That way you can make sure that when trent has it, it does damage, but otherwise doesn't.

local function onequip(inst, owner) 	if ThePlayer.prefab == "trent" then	inst:AddTag("sharp")	inst:AddComponent("weapon")    inst.components.weapon:SetDamage(55)end	endlocal function onunequip(inst, owner) 	inst:RemoveTag("sharp")	inst:RemoveComponent("weapon")end
Do I have the right idea here? Edited by ELEMENTALCRAFTER009
Link to comment
Share on other sites

@ELEMENTALCRAFTER009, If you useThePlayer, it will only care about who the host character is, like I said before. Instead, you can attach functions like that to your character specifically by listening for the equip and unequip events.

local function onequip(inst, data)    local item = data.itemif item and item.prefab == "diviningrod" then    item:AddTag("sharp")    item:AddTag("sharp")    item:AddComponent("weapon")    item.components.weapon:SetDamage(55)end    endlocal function onunequip(inst, data)    local item = data.itemif item and item.prefab == "diviningrod" then    inst:RemoveTag("sharp")    inst:RemoveComponent("weapon")endendinst:ListenForEvent("equip", onequip)inst:ListenForEvent("unequip", onunequip)
Edited by rezecib
Link to comment
Share on other sites

Alright, now after rezecib got me on the right track, I ran into some confusion again with a more modified version of the code.

local function onequip(inst, data)    local item = data.item    local chest = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.BODY)    if item and item.prefab == "spear" and chest == nil  then	inst.components.talker:Say("I think I could modify this.")    inst.AnimState:SetMultColour(2, 1, 1, 1)	elseif item and item.prefab == "spear" and chest.name == "Life Giving Amulet"  then	inst.components.talker:Say("I feel somewhat more focused.")    inst:RemoveComponent("finiteuses")    inst.AnimState:SetMultColour(2, 1, 1, 1)	elseif item and item.prefab == "spear" and chest.name == "Chilled Amulet" then	inst.components.talker:Say("I feel somewhat more quick.")    inst:RemoveComponent("finiteuses")    inst.AnimState:SetMultColour(1, 1, 2, 1)	elseif item and item.prefab == "spear" and chest.name == "Nightmare Amulet" then	inst.components.talker:Say("I feel somewhat more insecure.")    inst:RemoveComponent("finiteuses")    inst.AnimState:SetMultColour(2, 1, 2, 1)	elseif item and item.prefab == "spear" and chest.name == "Magiluminescence" then	inst.components.talker:Say("I feel somewhat more vibrant.")    inst:RemoveComponent("finiteuses")    inst.AnimState:SetMultColour(2, 2, 1, 1)	elseif item and item.prefab == "spear" and chest.name == "The Lazy Forager" then	inst.components.talker:Say("I feel somewhat more efficient.")    inst:RemoveComponent("finiteuses")    inst.AnimState:SetMultColour(2, 1.5, 1, 1)	elseif item and item.prefab == "spear" and chest.name == "Construction Amulet" then	inst.components.talker:Say("I feel somewhat more intelligent.")    inst:RemoveComponent("finiteuses")    inst.AnimState:SetMultColour(1, 2, 1, 1)end    end 

How would you set which item it's applying the effects to?

Edited by ELEMENTALCRAFTER009
Link to comment
Share on other sites

@ELEMENTALCRAFTER009, What exactly are you trying to do? 

 

Also, you should use the prefab names for the amulets instead of the display names, since those could change (e.g. if someone has a language pack installed).

I'm trying to modify the spear with certain traits (such as higher damage, a colored tint, and/or faster movement speed) when it's equipped with a specific amulet.

Link to comment
Share on other sites

@ELEMENTALCRAFTER009, I don't know if there's a way to set the mult color of a specific symbol (the equipped spear). As for how to organize the code generally, this is how I would do it, since you need it to trigger not when the spear is equipped, but when any equipment changes, if there is a spear equipped and an amulet equipped.
 

inst.spearstate = nillocal spearfns = {	amulet =  --using the prefab names as indexes in this table, amulet = Life-Giving Amulet	{		equip = function(player, amulet, spear)			-- stuff to add this amulet's effect, for example:			player.components.talker:Say("I feel somewhat more focused.")			amulet:RemoveComponent("finiteuses")			spear.components.weapon:SetDamage(55)		end,		unequip = function(player, amulet, spear)			-- stuff to remove this amulet's effect, for example:						-- copied from amulet.lua for the red amulet			amulet:AddComponent("finiteuses")			amulet.components.finiteuses:SetOnFinished(amulet.Remove)			amulet.components.finiteuses:SetMaxUses(TUNING.REDAMULET_USES)			amulet.components.finiteuses:SetUses(TUNING.REDAMULET_USES)						spear.components.weapon:SetDamage(TUNING.SPEAR_DAMAGE)		end	},	blueamulet = -- Chilled Amulet	{		equip = function(player, amulet, spear)			-- stuff to add this amulet's effect		end,		unequip = function(player, amulet, spear)			-- stuff to remove this amulet's effect		end	},	purpleamulet = 	{		equip = function(player, amulet, spear)			-- stuff to add this amulet's effect		end,		unequip = function(player, amulet, spear)			-- stuff to remove this amulet's effect		end	},	yellowamulet = -- Magiluminescence	{		equip = function(player, amulet, spear)			-- stuff to add this amulet's effect		end,		unequip = function(player, amulet, spear)			-- stuff to remove this amulet's effect		end	},	orangeamulet = -- The Lazy Forager	{		equip = function(player, amulet, spear)			-- stuff to add this amulet's effect		end,		unequip = function(player, amulet, spear)			-- stuff to remove this amulet's effect		end	},	greenamulet = -- Construction Amulet	{		equip = function(player, amulet, spear)			-- stuff to add this amulet's effect		end,		unequip = function(player, amulet, spear)			-- stuff to remove this amulet's effect		end	},}local function onequip(inst, data)    local hands = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)    local chest = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.BODY)	if hands.prefab == "spear" and spearfns[chest.prefab] and inst.spearstate ~= chest.prefab then		spearfns[chest.prefab].equip(inst, chest, hands)		inst.spearstate = chest.prefab	endendlocal function onunequip(inst, data)	local item = data.item	-- this construct makes sure it gets the item that was unequipped as the spear/amulet    local hands = data.item.equipslot == EQUIPSLOTS.HANDS 					and item					or inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)    local chest = data.item.equipslot == EQUIPSLOTS.BODY					and item					or inst.components.inventory:GetEquippedItem(EQUIPSLOTS.BODY)	if hands.prefab == "spear" and spearfns[chest.prefab] then		spearfns[chest.prefab].unequip(inst, chest, hands)		inst.spearstate = nil	endend

Edit: Added inst.spearstate to make sure the transformation isn't applied twice, if, say you equip a helmet after having equipped the spear and amulet

Edited by rezecib
Link to comment
Share on other sites

@ELEMENTALCRAFTER009, I don't know if there's a way to set the mult color of a specific symbol (the equipped spear). As for how to organize the code generally, this is how I would do it, since you need it to trigger not when the spear is equipped, but when any equipment changes, if there is a spear equipped and an amulet equipped.

 

inst.spearstate = nillocal spearfns = {	amulet =  --using the prefab names as indexes in this table, amulet = Life-Giving Amulet	{		equip = function(player, amulet, spear)			-- stuff to add this amulet's effect, for example:			player.components.talker:Say("I feel somewhat more focused.")			amulet:RemoveComponent("finiteuses")			spear.components.weapon:SetDamage(55)		end,		unequip = function(player, amulet, spear)			-- stuff to remove this amulet's effect, for example:						-- copied from amulet.lua for the red amulet			amulet:AddComponent("finiteuses")			amulet.components.finiteuses:SetOnFinished(amulet.Remove)			amulet.components.finiteuses:SetMaxUses(TUNING.REDAMULET_USES)			amulet.components.finiteuses:SetUses(TUNING.REDAMULET_USES)						spear.components.weapon:SetDamage(TUNING.SPEAR_DAMAGE)		end	},	blueamulet = -- Chilled Amulet	{		equip = function(player, amulet, spear)			-- stuff to add this amulet's effect		end,		unequip = function(player, amulet, spear)			-- stuff to remove this amulet's effect		end	},	purpleamulet = 	{		equip = function(player, amulet, spear)			-- stuff to add this amulet's effect		end,		unequip = function(player, amulet, spear)			-- stuff to remove this amulet's effect		end	},	yellowamulet = -- Magiluminescence	{		equip = function(player, amulet, spear)			-- stuff to add this amulet's effect		end,		unequip = function(player, amulet, spear)			-- stuff to remove this amulet's effect		end	},	orangeamulet = -- The Lazy Forager	{		equip = function(player, amulet, spear)			-- stuff to add this amulet's effect		end,		unequip = function(player, amulet, spear)			-- stuff to remove this amulet's effect		end	},	greenamulet = -- Construction Amulet	{		equip = function(player, amulet, spear)			-- stuff to add this amulet's effect		end,		unequip = function(player, amulet, spear)			-- stuff to remove this amulet's effect		end	},}local function onequip(inst, data)    local hands = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)    local chest = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.BODY)	if hands.prefab == "spear" and spearfns[chest.prefab] and inst.spearstate ~= chest.prefab then		spearfns[chest.prefab].equip(inst, chest, hands)		inst.spearstate = chest.prefab	endendlocal function onunequip(inst, data)	local item = data.item	-- this construct makes sure it gets the item that was unequipped as the spear/amulet    local hands = data.item.equipslot == EQUIPSLOTS.HANDS 					and item					or inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)    local chest = data.item.equipslot == EQUIPSLOTS.BODY					and item					or inst.components.inventory:GetEquippedItem(EQUIPSLOTS.BODY)	if hands.prefab == "spear" and spearfns[chest.prefab] then		spearfns[chest.prefab].unequip(inst, chest, hands)		inst.spearstate = nil	endend

Edit: Added inst.spearstate to make sure the transformation isn't applied twice, if, say you equip a helmet after having equipped the spear and amulet

Since I don't think you can change the tint of only the item, would it be possible to change the build of the player instead?

Link to comment
Share on other sites

 

@ELEMENTALCRAFTER009, You could make alternate swap_spear anim files and override the symbol for them just as it does on equipping:

player.AnimState:OverrideSymbol("swap_object", "swap_spear_red", "swap_spear_red")

[00:08:57]: [string "../mods/Trent - DST/scripts/prefabs/trent.l..."]:261: attempt to index local 'chest' (a nil value)

Also, is that inst.spearstate = nil supposed to be at the top all by itself?

 

Edited by ELEMENTALCRAFTER009
Link to comment
Share on other sites

@ELEMENTALCRAFTER009, This code should be in your master_postinit, in which inst should definitely be declared

The only problem left now is that it's not passing the onunequip at all.

 

Edit: Also, do you know if is possible to increase the amount of loot every monster drops by a certain amount? 

Edited by ELEMENTALCRAFTER009
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
 Share

×
  • Create New...