Jump to content

Recommended Posts

I'd like to write a mod where the player would spawn in a character (like a wilson) and can play alongside them while the character executes basic commands given in the chat (like, "chop wood" or "gather grass") and can keep himself alive by eating food in his inventory when he's hungry, etc.

Right now the problem I'm running into is that I would like the character to persist if I restart the server so that the stat levels, inventory, etc are the same, just like if an irl player were to log out and log in again.

I've figured out how to spawn a character with `Global.SpawnPrefab("wilson")`, however when I restart the server, the character is gone. Based on my googling, I need to implement an OnSave and OnLoad function, but I don't know where they are supposed to go or how to make sure they get called.

 

modmain.lua:

 

local _G = GLOBAL


local function SpawnCharacter(character_name, caller)
    -- Check if this is the server-side
    if not _G.TheWorld.ismastersim then
        return
    end


    -- Spawn the character at a specific position
      x,y,z = caller.Transform:GetWorldPosition()
      local character = _G.SpawnPrefab("wilson")
      if character then
          character.Transform:SetPosition(x, y, z)
      end
end


AddUserCommand("friend", {
    prettyname = "friend",
    desc = [[
Spawn Friendbot
optional paramter: character name
]],
    permission = _G.COMMAND_PERMISSION.USER,
    slash = true,
    usermenu = false,
    servermenu = false,
    params = {"character"},
    paramsoptional = {true},
    vote = false,
    localfn = function(params, caller)
        if caller == nil or caller.HUD == nil then
            return
        end
    SpawnCharacter("wilson",  caller)
    end,
})


 

Repro steps:

1. Start Server

2. type /friend into chat

3. a wilson appears

4. log out

5. log in

6. wilson is gone

desired behavior: wilson is still there

Edited by Koalaphant1

( I sadly can't help with it, I am just here to let you know:

That is such an adorable idea :wilson_dorky:

Kind of nostalgic memories to artifical Wilson but if I understood you right your bot is supposed to play /with/ you not run off only focused on themselves like artifical Wilson does? )

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