Jump to content

[SOLVED] Bit of code for followers causes game to disconnect


Recommended Posts

This is a little hard to explain, but here goes. For my character mod, K_K, he has an ability where when he begins doing any kind of work, such as chopping, mining, etc. nearby recruitable mobs (namely pigs and merms (only when wearing clever disguise)) will begin following him because he plays music (actual music not yet implemented). In short, he is a walking, somewhat-infinite, but situational, One-Man Band.

The issue my friend and I are running into is that, a piece of code that's supposed to trigger a loss of sanity for K_K when one of his followers dies is causing the game to disconnect me from the server upon beginning to chop a tree (and I assume doing other forms of work as well, but haven't tested). The funny thing is that once I remove this code, the game runs fine. This is the piece of code in question:

    if inst.components.leader then --Make sure we are not messing with something that doesn't exist (For custom gamemodes and such)
        local oldAddFollower = inst.components.leader.AddFollower -- Store a backup of the AddFollower function for later use
        inst.components.leader.AddFollower = function(follower) -- Replace the AddFollower function with our own
            oldAddFollower(inst.components.leader, follower) -- Run the stored backup of the AddFollower function so we don't miss out on the original functionality of it
			follower:ListenForEvent("death", onFollowerDeath) --Run the "onFollowerDeath" function when the follower dies
			follower:ListenForEvent("stopfollowing", onFollowerLeave) --Run the "onFollowerLeave" function when the follower stops following us
        end
    end

And here is his prefab file:

kk.lua

And here's the server log after one of those forced disconnects:

server_log.txt

Can anyone identify the problem(s) and perhaps show me how to fix it? Thank you!

Edited by Garamonde
Link to comment
Share on other sites

 if inst.components.leader then --Make sure we are not messing with something that doesn't exist (For custom gamemodes and such)
        local oldAddFollower = inst.components.leader.AddFollower -- Store a backup of the AddFollower function for later use
        inst.components.leader.AddFollower = function(self,follower) -- Replace the AddFollower function with our own
            oldAddFollower(self, follower) -- Run the stored backup of the AddFollower function so we don't miss out on the original functionality of it
			follower:ListenForEvent("death", onFollowerDeath) --Run the "onFollowerDeath" function when the follower dies
			follower:ListenForEvent("stopfollowing", onFollowerLeave) --Run the "onFollowerLeave" function when the follower stops following us
        end
    end

This is how the code should work.

The problem was that you passed follower as the first argument, but for such functions self (the component itself) is always the first argument. Then you can also give the correct component to the old function :)

  • GL Happy 1
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...