Jump to content

[SOLVED!!!] How can I save a entity then load it into a cave when its leader goes inside the cave...?


Recommended Posts

Hello, I really need help...

I'm trying to get followers follow the leader into caves but for some reason no matter what I try to do I can't get it.... And I feel i'm so close but i'm an idiot I just can't do it, it seems :(... If someone can help me that would be awesome :D!

Edited by SuperDavid
Link to comment
Share on other sites

On 10/3/2016 at 0:18 AM, Serpens said:

 

See the mod "personal chester", this chester can follow inside cave:

 

Thank you very much :D!

Unfortunately, I just went through the code of that mod & it seems that code is only useful for entities that use "eyebone" to follow their leader. My entity doesn't follow a "eyebone" so the code wouldn't do anything & I wouldn't know how to edit it to make it work without "eyebone", thanks so much for helping though :D!!!!

Edited by SuperDavid
Link to comment
Share on other sites

Use "leader" and/or "follower" components. I guess at least one of these components should exist.

Also use TheWorld:HasTag("cave") to check is it caves world if you want your code only in caves.

Edited by Maris
Link to comment
Share on other sites

47 minutes ago, Maris said:

Use "leader" and/or "follower" components

My entity already uses the follower component & my character has the leader component when the entity's following him around.

 

Anyways, I searched & I found some mod called "Weeseeks" that also has followers that don't use "eyebone" & they also go inside caves! The only problem is that I can't seem to change the code to work for my character & his entity..

Here's the code if someone could help me that would be great :D!

local function OnLoad_follower(inst, data)
    inst.puppets = {}
    if data and data.puppets then
        for k, v in pairs(data.puppets) do
            local puppet = SpawnSaveRecord(v)
            if puppet then
                inst.puppets[puppet] = puppet
                puppet.owner = inst
                puppet.sg:GoToState("spawn")
            end
        end
    end
end

local function OnSave_follower(inst, data)
    if inst.puppets then
        data.puppets = {}
        for k, v in pairs(inst.puppets) do
            if isValid(v) then
                data.puppets[#data.puppets and #data.puppets + 1 or 1] = v:GetSaveRecord()
            end
        end
    end
end

local master_postinit = function(inst)
inst.OnSave = OnSave_follower
inst.OnLoad = OnLoad_follower
end

My entity's is not a puppet? So, the code currently does nothing for me? But If someone could help me make this code that if a entity "tieravil" has a leader & the leader goes inside the cave then the code activates & spawns the "tieravil" inside a cave :)?

Link to comment
Share on other sites

5 hours ago, Maris said:

Use "leader" and/or "follower" components. I guess at least one of these components should exist.

Also use TheWorld:HasTag("cave") to check is it caves world if you want your code only in caves.

superdavids question is misleading ;)

I think he already has an entity that is able to follow the leader.
But if you take a look at the games chester: If you travel down to caves, you will drop the eyebone at the overworld automatically, so chester "won't follow you in caves". That is the question he is asking.

Link to comment
Share on other sites

Well I just tried something like and it didn't work.. I have no idea what I have to do anymore :wilson_cry:...

local function OnLoad_Tieravil(inst, data)
local tieravil = inst.prefab == "tieravil" and inst.components.follower and inst.components.follower.leader
    inst.tieravil = {}
    if data and data.tieravil then
        for k, v in pairs(data.tieravil) do
            local tieravil = SpawnSaveRecord(v)
            if tieravil then
                inst.tieravil[tieravil] = tieravil
                tieravil.owner = inst
            end
        end
    end
end

local function OnSave_Tieravil(inst, data)
local tieravil = inst.prefab == "tieravil" and inst.components.follower and inst.components.follower.leader
    if inst.tieravil then
        data.tieravil = {}
        for k, v in pairs(inst.tieravil) do
            if isValid(v) then
                data.tieravil = v:GetSaveRecord()
            end
        end
    end
end

local master_postinit = function(inst)
inst.OnLoad_Tieravil = OnSave_Tieravil
inst.OnLoad_Tieravil = OnLoad_Tieravil
end

 

Link to comment
Share on other sites

inst.puppets is a table that stores all follower entities so you could replace it with inst.components.leader.followers

local function OnLoad_follower(inst, data)
    if data and data.tieravils then
        for k, v in pairs(data.tieravils) do
            local tieravil = SpawnSaveRecord(v)
            if tieravil then
                inst.components.leader:AddFollower(tieravil)
                tieravil.owner = inst
                tieravil.sg:GoToState("idle")
            end
        end
    end
end

local function OnSave_follower(inst, data)
    local tieravils = inst.components.leader.followers
    if tieravils then
        data.tieravils = {}
        for k, v in pairs(tieravils) do
            if isValid(v) then
                data.tieravils[#data.tieravils and #data.tieravils + 1 or 1] = v:GetSaveRecord()
            end
        end
    end
end

local master_postinit = function(inst)
inst.OnSave = OnSave_follower
inst.OnLoad = OnLoad_follower
end

i think this should work

Link to comment
Share on other sites

On 10/8/2016 at 11:18 AM, Aquaterion said:

i think this should work

Unfortunately, it didn't do anything :(. But thanks so much for tryinh to help Aquaterion :D!!!

Edited by SuperDavid
Link to comment
Share on other sites

My assumption would be because it doesn't look like you're actually declaring an argument for data.

My suggestion is to include an else clause for this function:

local function OnLoad_follower(inst, data)

printing what the values of the args currently are might help get you pointed in the right direction of how they are calculating.

EDIT: Plus you have not included this from the script you are mimicking.:

local master_postinit = function(inst)
inst.OnSave = OnSave_follower
inst.OnLoad = OnLoad_follower
end
Edited by Commanding
Included in post
Link to comment
Share on other sites

What I suggest is that you put this on your character's prefab file in the master_postinit function:

local function master_postinit(inst)
	inst:AddComponent("petleash")
	inst.components.petleash:SetMaxPets(20)
end

And then, whenever you want to spawn a follower for your character, do:

local x, y, z = inst.Transform:GetWorldPosition()
inst.components.petleash:SpawnPetAt(x, y, z, "tieravil")

Where inst is your character, and "tieravil" is the prefab of your character.

 

SpawnPetAt will handle the spawning and the linking of the new follower to your character.

The petleash component handles going up and down for you.

Simple.

Link to comment
Share on other sites

@DarkXero Wow, that's so amazing. I can't believe such a little amount of code did what I was trying to do for 1 whole week!

Words cannot express how happy I am right now, I can't thank you enough DarkXero.

If I can just ask a 3 things is that would it be possible to make the entity when it's spawning play a state? Before I had it like this

local tieravil = SpawnPrefab("tieravil")
if tieravil ~= nil then
tieravil.Transform:SetPosition(act.target.Transform:GetWorldPosition())
tieravil.owner = act.target
tieravil.sg:GoToState("summon")
act.target.sg:GoToState("summon")
end

The 2nd question is does "inst.components.petleash:SetMaxPets(20)" mean the max amount of followers the character can have between all entities or just "tieravil"? Because I want my character to only be able to have 1 "tieravil" at a time.

 

The 3rd question is does this link the player & "tieravil" share last forever? Like when you log out that "tieravil" will still be tamed?

 

And finally thank you so, so much DarkXero, you're truly amazing :wilson_smile:!!!

Link to comment
Share on other sites

12 hours ago, SuperDavid said:

If I can just ask a 3 things is that would it be possible to make the entity when it's spawning play a state? Before I had it like this

local tieravil = SpawnPrefab("tieravil")
if tieravil ~= nil then
tieravil.Transform:SetPosition(act.target.Transform:GetWorldPosition())
tieravil.owner = act.target
tieravil.sg:GoToState("summon")
act.target.sg:GoToState("summon")
end

becomes

local target = act.target

if target then
	local x, y, z = target.Transform:GetWorldPosition()
	if target.components.petleash then
		target.components.petleash:SpawnPetAt(x, y, z, "tieravil")
	end
end

 

12 hours ago, SuperDavid said:

The 2nd question is does "inst.components.petleash:SetMaxPets(20)" mean the max amount of followers the character can have between all entities or just "tieravil"? Because I want my character to only be able to have 1 "tieravil" at a time.

In addition to the first code, you also want:

local function DoSpawnStuff(tieravil, inst)
	tieravil.owner = inst
	tieravil.sg:GoToState("summon")

	if not (inst.components.health:IsDead() or inst:HasTag("playerghost")) and not inst.components.rider:IsRiding() then
		inst.sg:GoToState("summon")
	end
end

local function OnSpawnPet(inst, tieravil)
	tieravil:DoTaskInTime(0, DoSpawnStuff, inst)
end

local function master_postinit(inst)
	inst:AddComponent("petleash")
	inst.components.petleash:SetMaxPets(1)
	inst.components.petleash:SetOnSpawnFn(OnSpawnPet)
end

To have a max of 1 pet.

And also to handle going to the "summon" state.

 

12 hours ago, SuperDavid said:

The 3rd question is does this link the player & "tieravil" share last forever? Like when you log out that "tieravil" will still be tamed?

That's how it works for Maxwell.

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