Jump to content

Crash with projectiles


Recommended Posts

Hello again forums! I get this crash
 

[00:12:21]: [string "scripts/mainfunctions.lua"]:301: bad argument #1 to 'find' (string expected, got nil)
LUA ERROR stack traceback:
=[C]:-1 in (field) find (C) <-1--1>
scripts/mainfunctions.lua:301 in (field) SpawnPrefab (Lua) <300-308>
   name = nil
   skin = nil
   skin_id = nil
   creator = nil
../mods/Wheeler/modmain.lua:208 in (method) LaunchProjectile (Lua) <202-230>
   self =
      LaunchProjectile = function - ../mods/Wheeler/modmain.lua:202
      heightoffset = 2.5
      inst = 110911 - trusty_shooter(LIMBO) (valid:true)
      onprojectilelaunch = function - ../mods/Wheeler/scripts/prefabs/trusty_shooter.lua:178
      onattack = function - ../mods/Wheeler/scripts/prefabs/trusty_shooter.lua:131
      _ = table: 2F8429B0
      damage = 10
   attacker = 110111 - wheeler (valid:true)
   target = 112669 - hound (valid:true)
scripts/components/combat.lua:842 in (method) DoAttack (Lua) <805-886>

I've add Wheeler's Pew matic Horn and I crash whenever the last piece of ammunition is used.
I think the problem stems from this? I overwrote the LaunchProjectile Function
 

AddComponentPostInit("weapon", function(self)
    function self:LaunchProjectile(attacker, target)
        if self.projectile ~= nil then
            if self.onprojectilelaunch ~= nil then
                self.onprojectilelaunch(self.inst, attacker, target)
            end

            local proj = GLOBAL.SpawnPrefab(self.projectile)
            if proj ~= nil then
                    proj:AddTag("projectile")

                    proj:AddComponent("projectile")
                    proj.components.projectile:SetSpeed(35)
                    proj.components.projectile:SetOnHitFn(OnHit)

                    proj.persists = false
                
                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
end)

This of course breaks other projectiles which I'll fix but I wanna make my thing work first. After this though I tried putting this into my trusty_shooter prefab
 

local function OnProjectileLaunch(inst, attacker, target, proj)

    inst.SoundEmitter:PlaySound("dontstarve_DLC003/characters/wheeler/air_horn/shoot")
    
    local proj = SpawnPrefab(inst.components.container:GetItemInSlot(1).prefab)
    
    proj:AddTag("projectile")
    proj:AddComponent("projectile")
    
    proj.components.projectile:SetSpeed(35)
    proj.components.projectile:SetOnHitFn(OnHit)
    
    proj.components.inventoryitem.canbepickedup = false

    proj.persists = false
    
    local removed_item = inst.components.container:RemoveSingleItemBySlot(1)
    if removed_item then
        removed_item:Remove()
    end
    
end

It doesn't seem to work though. I don't crash but it doesn't launch anything either. I'd be grateful for any help, Thank you!

Link to comment
Share on other sites

This code is probably the culprit:

        if self.projectile ~= nil then
            if self.onprojectilelaunch ~= nil then
                self.onprojectilelaunch(self.inst, attacker, target)
            end

            local proj = GLOBAL.SpawnPrefab(self.projectile)

From what the error says, SpawnPrefab is being called with a 'nil' instead of a string (prefab name). Since you check for self.projectile being nil in the first line, we know there IS one, but when you get to SpawnPrefab then self.projectile is nil. So there is only one thing that can make self.projectile be nil: the onprojectilelaunch function.

Maybe you set self.projectile to nil in your onprojectilelaunch when you run out of ammo? An easy fix could be simply spawning the prefab before calling onprojectilelaunch().

By the way...do you know that what your code does, is change the LaunchProjectile function for all weapons in the entire game?

And it completely overwrites what any mods might have done to the LaunchProjectile function before your mod gets loaded.

It's rather annoying that LaunchProjectile doesn't return the new projectile when you call it. There's really no way around overwriting the function, or doing something elaborate, if you want to affect the resulting projectile.

Edited by Ultroman
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...