Jump to content

Recommended Posts

Hello, I have a problem if someone can help me with that'd reallly be great :D!

So, my character has a pet entity which can be spawned with petleash, the entity's prefab's called "tieravil" but I have no idea how to call upon it, like how would I call upon this pet in modmain.lua or mycharacterprefab.lua (outside of its spawning function of course)?

Here's the code for when its spawned.

modmain.lua

Spoiler

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

mycharacterprefab.lua

Spoiler

local function DoSpawnTieravil(tieravil, inst)
  tieravil.owner = inst
  
  if tieravil.prefab == "tieravil" then
  tieravil.sg:GoToState("hit")
  
  if not (inst.components.health:IsDead() or inst:HasTag("playerghost")) and not inst.components.rider:IsRiding() then
  inst.sg:GoToState("hit")
  end
  
  end 
end

local function OnSpawnTieravil(inst, tieravil)
  tieravil:DoTaskInTime(0, DoSpawnTieravil, inst)
end

if inst.components.petleash ~= nil then
   inst:AddComponent("petleash")
   inst.components.petleash:SetMaxPets(1)
end

inst.components.petleash:SetOnSpawnFn(OnSpawnTieravil)

 

Sorry, if the answer's in plain sight or something, I'm kinda dumb... Also, thanks for reading my problem, have a great day/night :)!

Edited by SuperDavid

I don't have time to test right now but judging from the petleash code it looks like accessing pets array in the petleash component is what you want.

Spoiler

local PetLeash = Class(function(self, inst)
    self.inst = inst

    self.petprefab = nil
    self.pets = {}
    self.maxpets = 1
    self.numpets = 0

    self.onspawnfn = nil
    self.ondespawnfn = nil

    self._onremovepet = function(pet)
        if self.pets[pet] ~= nil then
            self.pets[pet] = nil
            self.numpets = self.numpets - 1
        end
    end
end)

self.pets = {} stores a list of the pets you have

You just need to call upon it so maybe try this I took from waxwell.lua from his ondeath function but modified


local pet = nil
for k, v in pairs(inst.components.petleash:GetPets()) do
    if v.prefab == "tieravil" then
        -- do something
        -- e.g. you could make a variable to store the pets path for other functions
        pet = v
    end
end

 

 

Edited by IronHunter
mostly grammar
Spoiler
8 hours ago, SuperDavid said:

Thank you IronHunter your code worked perfectly :D!!

If I can just ask what did you mean by this? Sorry, I'm a noob xD..

 

 

Variables are data fields such as local pet = nil.

They can store all sorts of data such as numbers, strings, or paths to functions or prefabs in this case.

The point of this is to store the info that you are planning to use in the future, so that it can be accessed by other functions.

 

6 hours ago, IronHunter said:

Variables are data fields such as local pet = nil.

They can store all sorts of data such as numbers, strings, or paths to functions or prefabs in this case.

The point of this is to store the info that you are planning to use in the future, so that it can be accessed by other functions.

 

So, would it be possible to save this-

16 hours ago, IronHunter said:

local pet = nil
for k, v in pairs(inst.components.petleash:GetPets()) do
    if v.prefab == "tieravil" then
        -- do something
        -- e.g. you could make a variable to store the pets path for other functions
        pet = v
    end
end

 

-as a variable then I don't have to copy paste it 100 places xD? Sorry, if that's not what variables do, & thanks for your help IronHunter :D!!!

What I meant was store the path for your pet as a variable that way you don't have to constantly call the function. It just makes your code cleaner and more optimal.

See how I made that local variable called pet, it is storing the path to your pet "tieravil". I am saying you could instead of making a local variable there save it somewhere in the file so that other functions can access it without having to run this search again.

E.g. wx78.lua has a variable called inst.level, this stores his level and is accessed by many different functions in his code. If you make it local only the function that you defined the variable can use it.

I hope this clears it up

So, if I get it right.. if I got rid of "local pet = nil"

local pet = nil
	for k, v in pairs(act.target.components.petleash:GetPets()) do
		if v.prefab == "tieravil" then
			v.components.inventory:DropEverything(true)
			v.SoundEmitter:PlaySound("dontstarve/common/dropGeneric")
		pet = v
	end
end

in this code & replaced it with "pet = nil" then I wouldn't have to put it  whenever I use this code somewhere, right?

Sorry, for being such a noob & thanks for the information :D!!

 

It is more like, if you make a variable like inst.pet = nil in your master_postinit, you can then assign it for your own use.

Spoiler

for k, v in pairs(act.target.components.petleash:GetPets()) do
    if v.prefab == "tieravil" then
        v.components.inventory:DropEverything(true)
        v.SoundEmitter:PlaySound("dontstarve/common/dropGeneric")
        inst.pet = v
    end
end

You call this once when you spawn your pet so you don't have to keep calling this chunk of code, its a efficiency thing

Its just an idea, so you don't have to constantly run a "for loop" to find your pet so you could interact with it.

As well as a way to tidy up your document besides efficiency, depending on how frequently you are using this code.

Wow, it works now all thanks to you IronHunter, thank you this is SOo much more efficient :D!!

If I can just ask some stuff to make sure I got it all correct.. So, as long as I trigger this code one time when my "tieravil" spawns into the world & I have "inst.pet = nil" in my master_postinit

Spoiler

for k, v in pairs(inst.components.petleash:GetPets()) do
	if v.prefab == "tieravil" then
		inst.pet = v
	end
end

then I can call upon my "tieravil" by doing e.g. " inst.pet.components.talker:Say("Hello!") ", yes?

 

One more thing when I use "inst,pet" does it only call upon "tieravil" prefabs or does it call upon all pets like Varlings, Kittycoon, Glommling, ect.? And if it only calls upon tieravil prefabs then can I rename "inst.pet" into "inst.tieravil" or would that cause problems?

 

Thanks again so much man, this really will help me :D!!!

 

Edited by SuperDavid

You can rename inst.pet to whatever you want it is assigned the path of your "tieravil" prefab.

It is just a name for the variable

Yes you should be able to do inst.tieravil.components etc

There is no reason for it to call other pets as you are assigning it once to the path of the tieravil prefab when you run the loop after your pet spawns. Probably want to set it to nil if your pet tieravil dies.

Edited by IronHunter

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