Jump to content

Problem with States


Recommended Posts

Hello forum! My weapon would do the regular swipe attack when attacking, so I added states but for some reason the weapon won't shoot projectiles anymore!
 

AddStategraphState("wilson",
	GLOBAL.State{
        name = "shoot",
        tags = {"attack", "notalking", "abouttoattack", "busy"},
        
        onenter = function(inst)
            local otherequipped = inst.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HANDS)
            if (otherequipped and otherequipped:HasTag("hand_gun")) then
                inst.AnimState:PlayAnimation("hand_shoot")
            end
		end,
        
        timeline=
        {
            GLOBAL.TimeEvent(17*GLOBAL.FRAMES, function(inst) 
                inst.components.combat:DoAttack(inst.sg.statemem.target) 
                inst.sg:RemoveStateTag("abouttoattack") 
            end),           	
            GLOBAL.TimeEvent(20*GLOBAL.FRAMES, function(inst)
                    inst.sg:RemoveStateTag("attack")
            end),           
            
        },
        
        events=
        {
            GLOBAL.EventHandler("animover", function(inst)
                inst.sg:GoToState("idle")
            end )
        }
    }
)

AddStategraphActionHandler("wilson",
	GLOBAL.ActionHandler(GLOBAL.ACTIONS.ATTACK,
		function(inst, action)
			inst.sg.mem.localchainattack = not action.forced or nil
			if not (inst.sg:HasStateTag("attack") and action.target == inst.sg.statemem.attacktarget or inst.components.health:IsDead()) then
				local weapon = inst.components.combat ~= nil and inst.components.combat:GetWeapon() or nil
				return (weapon == nil and "attack")
					or (weapon:HasTag("blowdart") and "blowdart")
					or (weapon:HasTag("thrown") and "throw")
					or (weapon:HasTag("propweapon") and "attack_prop_pre")
					or (weapon:HasTag("multithruster") and "multithrust_pre")
					or (weapon:HasTag("helmsplitter") and "helmsplitter_pre")  
					or (weapon:HasTag("hand_gun") and "shoot" )
					or "attack"
			end
		end
	)
)

This is my code in the modmain.lua


 

local function LoadWeapon(inst, item)

    if inst.ammo == 0 then
            -- inst.SoundEmitter:PlaySound("dontstarve_DLC003/characters/wheeler/air_horn/load_2")
		print("Checkpoint3")

        inst:AddTag("projectile")
        inst.components.weapon:SetProjectile(item.prefab)
        inst:AddTag("gun")

        SetAmmoDamageAndRange(inst, item)

        --If equipped, change current equip overrides
        if inst.components.equippable and inst.components.equippable:IsEquipped() then
            inst.components.inventoryitem.owner.AnimState:OverrideSymbol("swap_object", inst.override_bank, "swap_trusty_shooter")
        end

        inst.replica.inventoryitem:SetImage("trusty_shooter")
    end

    --[[if item.components.stackable then
        inst.ammo = inst.ammo + item.components.stackable.stacksize
    else
        inst.ammo = inst.ammo + 1
    end]]
end
local function OnProjectileLaunch(inst, attacker, target, proj)
	print("CheckPoint2")

    inst.SoundEmitter:PlaySound("dontstarve_DLC003/characters/wheeler/air_horn/shoot")
	
	inst:DoTaskInTime(0.1, function()
		for i = 1, inst.components.container:GetNumSlots() do
			local removed_item = inst.components.container:RemoveSingleItemBySlot(1)
			if removed_item then
				removed_item:Remove()
			end
		end
	end)
end
inst:AddComponent("weapon")               ----------This is in the fn
	
    function inst.components.weapon:LaunchProjectile(attacker, target)
		print("Checkpoint1")
	    if self.projectile ~= nil then
		    if self.onprojectilelaunch ~= nil then
			    self.onprojectilelaunch(self.inst, attacker, target)
		    end
			

		    local proj = SpawnPrefab(self.projectile)
		    if proj ~= nil then
				proj:AddTag("projectile")
				proj:AddComponent("projectile")
    
				proj.components.projectile:SetSpeed(35)
				proj.components.projectile:SetOnHitFn(OnHit)
    
				proj.components.inventoryitem.canbepickedup = false

				proj.persists = false
			-- Do something to the projectile here.
			    if proj.components.projectile ~= nil then
				    proj.Transform:SetPosition(attacker.Transform:GetWorldPosition())
				    proj.components.projectile:Throw(self.inst, target, attacker)
				    if self.inst.projectiledelay ~= nil then
					    proj.components.projectile:DelayVisibility(self.inst.projectiledelay)
				    end
			    elseif proj.components.complexprojectile ~= nil then
				    proj.Transform:SetPosition(attacker.Transform:GetWorldPosition())
				    proj.components.complexprojectile:Launch(target:GetPosition(), attacker, self.inst)
			    end
		    end
	    end
	end
	
    --inst.components.weapon:SetCanAttack(CanAttack)
    --inst.components.weapon:SetAttackCallback(OnAttack)
    inst.components.weapon:SetOnProjectileLaunch(OnProjectileLaunch)
    inst.components.weapon.heightoffset = 2.5

And these are SOME code from the prefab (It's what I think is most important but i'll share the prefab file), As you can see I did place print functions in some places but I didn't get anything in the client log or console, So something is preventing these from running for some reason? Thanks for any help

trusty_shooter.lua

Link to comment
Share on other sites

It seems like you were already doing what has to be done. Track down exactly where something goes haywire, just by printing state all the time and all over the place, until you see something that isn't quite right. Then you can slowly turn off the broad phase debugging, and start printing more variables to ensure they are what you think they should be. I can't look at that mountain of code and instantly tell you where the problem is. Only if I was lucky enough to stare right at it and have the right brainwave.

Link to comment
Share on other sites

16 hours ago, Ultroman said:

[snip snop]

I just wonder, why the functions where do I put the prints aren't running? They run just fine with a regular "attack" state, but once I apply my state it doesn't seem to run. Maybe something in the attack state can help me? I'll look there and come back if I can't find anything.

Link to comment
Share on other sites

Load up your state with some prints. Not sure if all the prints go to the same log. Remember to check all 2-3 logs.

Also, keep in mind that when you change a stategraph, you change it for every character using that stategraph, but not for any of the characters with their own stategraph. If this is character-specific, you should make your own stategraph file.

Link to comment
Share on other sites

17 minutes ago, Ultroman said:

Load up your state with some prints. Not sure if all the prints go to the same log. Remember to check all 2-3 logs.

Also, keep in mind that when you change a stategraph, you change it for every character using that stategraph, but not for any of the characters with their own stategraph. If this is character-specific, you should make your own stategraph file.

Alright i'll try that, And no the state isn't specific. It's for anyone using the weapon, Thank you.

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