Jump to content

Recommended Posts

I am trying to make my first mod. I want to add woby to all characters. 

I copied some code about spawnwoby in walter.lua into modmain.lua. But when I choose the character (Wilson) and enter the game, the error is displayed then: 

attempt to call method 'LinkToPlayer' (a nil value)
  in (upvalue) SpawnWoby (Lua)
    in (field) fn (Lua)  ...

 

modmain.lua

----------------------------------------------------------------------------

require "class"
require "util"
local _G = GLOBAL
local TECH = _G.TECH
local SpawnPrefab = _G.SpawnPrefab

local function SpawnWoby(inst)
    local player_check_distance = 40
    local attempts = 0

    local max_attempts = 30
    local x, y, z = inst.Transform:GetWorldPosition()

    local woby = SpawnPrefab(TUNING.WALTER_STARTING_WOBY)
    inst.woby = woby
    woby:LinkToPlayer(inst)  ------------------ in (upvalue) SpawnWoby (Lua)---------attempt to call method 'LinkToPlayer' (a nil value)
    inst:ListenForEvent("onremove", inst._woby_onremove, woby)

    while true do
        local offset = FindWalkableOffset(inst:GetPosition(), math.random() * PI, player_check_distance + 1, 10)

        if offset then
            local spawn_x = x + offset.x
            local spawn_z = z + offset.z

            if attempts >= max_attempts then
                woby.Transform:SetPosition(spawn_x, y, spawn_z)
                break
            elseif not IsAnyPlayerInRange(spawn_x, 0, spawn_z, player_check_distance) then
                woby.Transform:SetPosition(spawn_x, y, spawn_z)
                break
            else
                attempts = attempts + 1
            end
        elseif attempts >= max_attempts then
            woby.Transform:SetPosition(x, y, z)
            break
        else
            attempts = attempts + 1
        end
    end

    return woby
end

local function OnWobyTransformed(inst, woby)
    if inst.woby ~= nil then
        inst:RemoveEventCallback("onremove", inst._woby_onremove, inst.woby)
    end
    inst.woby = woby
    inst:ListenForEvent("onremove", inst._woby_onremove, woby)
end

local function OnWobyRemoved(inst)
    inst.woby = nil
    inst._replacewobytask = inst:DoTaskInTime(1, function(i) i._replacewobytask = nil if i.woby == nil then SpawnWoby(i) end end)
end

local function OnRemoveEntity(inst)
    -- hack to remove pets when spawned due to session state reconstruction for autosave snapshots
    if inst.woby ~= nil and inst.woby.spawntime == GetTime() then
        inst:RemoveEventCallback("onremove", inst._woby_onremove, inst.woby)
        inst.woby:Remove()
    end

    if inst._story_proxy ~= nil and inst._story_proxy:IsValid() then
        inst._story_proxy:Remove()
    end
end

local function OnDespawn(inst)
    if inst.woby ~= nil then
        inst.woby:OnPlayerLinkDespawn()
    end
end

local function OnSave(inst, data)
    data.woby = inst.woby ~= nil and inst.woby:GetSaveRecord() or nil
end

local function OnLoad(inst, data)
    if data ~= nil and data.woby ~= nil then
        inst._woby_spawntask:Cancel()
        inst._woby_spawntask = nil

        local woby = SpawnSaveRecord(data.woby)
        inst.woby = woby
        if woby ~= nil then
            if inst.migrationpets ~= nil then
                table.insert(inst.migrationpets, woby)
            end
            woby:LinkToPlayer(inst)

            woby.AnimState:SetMultColour(0,0,0,1)
            woby.components.colourtweener:StartTween({1,1,1,1}, 19*FRAMES)
            local fx = SpawnPrefab(woby.spawnfx)
            fx.entity:SetParent(woby.entity)

            inst:ListenForEvent("onremove", inst._woby_onremove, woby)
        end
    end
end

 

AddPlayerPostInit(function(inst)
    inst._woby_spawntask = inst:DoTaskInTime(2, function(i) i._woby_spawntask = nil SpawnWoby(i) end)
    inst._woby_onremove = function(woby) OnWobyRemoved(inst) end

    inst.OnWobyTransformed = OnWobyTransformed

    inst.OnSave = OnSave
    inst.OnLoad = OnLoad
    inst.OnDespawn = OnDespawn
    inst:ListenForEvent("onremove", OnRemoveEntity)
end)

----------------------------------------------------------------------------

 

What do i do to fix it, i really like to give everyone a woby LOL


 

 

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