Jump to content

Recommended Posts

23 minutes ago, Vindorable said:

I'm creating a weapon that changes damage based on player's sanity.

Problem is it doesn't load correctly, but I'm able to enter the game. Shortly after, I will get disconnected. Not sure why this is happening.

This is my code http://pastebin.com/HusThaTG

Well, you might want to check if there's inventoryitem owner in UpdateDamage.

Since you're running it on load, you can't be 100% sure if there is an owner already.

22 minutes ago, PanAzej said:

Well, you might want to check if there's inventoryitem owner in UpdateDamage.

Since you're running it on load, you can't be 100% sure if there is an owner already.

Oh! The reason it isn't loading properly is because the weapon that was on the ground doesn't have an owner. Thus, I can go inside the game but get disconnected shortly after.

 

Is there an example or somewhere I can look into and read up on the inventory owner functions you mentioned? 

local function UpdateDamage(inst)
	local owner = inst.components.inventoryitem.owner
	if owner and owner.components.sanity then
		local basedamage   = 40

		local sanitydamage = 0
		local sanity_percent = owner.components.sanity:GetPercent()
		if sanity_percent > 0.8 and sanity_percent <= 1.0 then sanitydamage = 0  end
		if sanity_percent > 0.6 and sanity_percent <= 0.8 then sanitydamage = 5  end
		if sanity_percent > 0.4 and sanity_percent <= 0.6 then sanitydamage = 10 end
		if sanity_percent > 0.2 and sanity_percent <= 0.4 then sanitydamage = 20 end
		if sanity_percent > 0.0 and sanity_percent <= 0.2 then sanitydamage = 40 end

		local newdamage    = basedamage + sanitydamage
		inst.components.weapon:SetDamage(newdamage)
	end
end

local function OnLoad(inst, data)
	UpdateDamage(inst)
end

local function onequip(inst, owner)
	UpdateDamage(inst)
	owner.AnimState:OverrideSymbol("swap_object", "swap_nightmareblade", "swap_nightmareblade")
	owner.AnimState:Show("ARM_carry")
	owner.AnimState:Hide("ARM_normal")
end

local function onunequip(inst, owner)
	UpdateDamage(inst)
	owner.AnimState:Hide("ARM_carry")
	owner.AnimState:Show("ARM_normal")
end

 

@DarkXero, thank you! Now, I can enter the game without being disconnected shortly after.

 

But, on game load, when I enter the game, the weapon's damage is not updated based on the character's sanity. It's damage is it's base damage.

 

I seem to know the cause. Am I right to say that when the game loads, the process is as follow: 1) Loads base game (here Sanity is set to 1) > 2) Loads mods (since, Sanity was 1, sanity-boost damage is 0) > Loads game save file (here is where the actual Sanity is set). Therefore, nothing can be done unless I do a OnSave function on the character's sanity value when I exit the game, correct?

Edited by Vindorable
36 minutes ago, Vindorable said:

1) Loads base game (here Sanity is set to 1) > 2) Loads mods (since, Sanity was 1, sanity-boost damage is 0) > Loads game save file (here is where the actual Sanity is set).

Mods are loaded before the game loads the save.

Else, your mod weapon would fail to spawn.

 

1 hour ago, Vindorable said:

But, on game load, when I enter the game, the weapon's damage is not updated based on the character's sanity. It's damage is it's base damage.

What is happening is this:

Game loads player inventory
- weapon is loaded in the OnLoad of inventory
-- owner is nil
-- sanity is nil
- player equips weapon in the OnLoad of inventory
-- has owner
-- sanity percent is 1
Game loads sanity
- changes percent from 1 to 0.5 or other

So you want this:

local function OnLoad(inst, data)
	inst:DoTaskInTime(0, UpdateDamage)
end

To delay the loading to the next frame, where sanity is already loaded.

 

Or use something like:

local function UpdateDamage(owner, data)
	local inst = data and data.weapon
	if inst and owner.components.sanity then
		local basedamage   = 40

		local sanitydamage = 0
		local sanity_percent = owner.components.sanity:GetPercent()
		if sanity_percent > 0.8 and sanity_percent <= 1.0 then sanitydamage = 0  end
		if sanity_percent > 0.6 and sanity_percent <= 0.8 then sanitydamage = 5  end
		if sanity_percent > 0.4 and sanity_percent <= 0.6 then sanitydamage = 10 end
		if sanity_percent > 0.2 and sanity_percent <= 0.4 then sanitydamage = 20 end
		if sanity_percent > 0.0 and sanity_percent <= 0.2 then sanitydamage = 40 end

		local newdamage    = basedamage + sanitydamage
		inst.components.weapon:SetDamage(newdamage)
	end
end

local function onequip(inst, owner)
	owner.AnimState:OverrideSymbol("swap_object", "swap_spear", "swap_spear")
	owner.AnimState:Show("ARM_carry")
	owner.AnimState:Hide("ARM_normal")
	inst:ListenForEvent("onattackother", UpdateDamage, owner)
end

local function onunequip(inst, owner)
	owner.AnimState:Hide("ARM_carry")
	owner.AnimState:Show("ARM_normal")
	inst:RemoveEventCallback("onattackother", UpdateDamage, owner)
end
                                                        

so that the damage updates right before an attack. No need to load nothing.

(Instead of using OnAttack, that runs after the damage was dealt.)

The onattackother event can be found in the DoAttack function of the combat component.

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