Jump to content

Help With modding rabbit trap plz


Recommended Posts

Hi anyone.

I need some info on how trap works. Since the auto-retrap mod doenst work when caves are activated I decided to build one of my own.

I started with the mod I already had and tried to edit it. While I'm not a certified programmer, I know how to; I already have 2-3 mod

Every way I look at it it brings me back to the Trap:Harvest function of the trap component

but even with an empty function it still pickup the ******* trap lol

It look easy to fix but there is something I dont get.

I understand it might be a client vs host issue

Why this has no effect whatsoever?

local require = GLOBAL.require
local Trap = require "components/trap"

AddComponentPostInit("trap", 
    function(self)
        local oldfn = self.havest
        self.harvest = function () end
        function Trap:Havest(doer)
            if self.issprung then
                --Cache these because trap may become invalid in callbacks
                local pos = self.inst:GetPosition()
                local timeintrap = self.inst.components.timer ~= nil and self.inst.components.timer:GetTimeElapsed("foodspoil") or 0
                
                self.inst:PushEvent("harvesttrap") ---> harvest animation+trap reset
                if self.onharvest ~= nil then        
                    self.onharvest(self.inst)        ---> trap.onhavested only use a finiteuse
                end
                
                --WARNING: May have become invalid now!  --> well...
                
                --- loot part---- 
                
                local inventory = doer ~= nil and doer.components.inventory or nil
                if self.lootprefabs ~= nil then
                    for i, v in ipairs(self.lootprefabs) do
                        local loot = SpawnPrefab(v)
                        if loot ~= nil then
                            if inventory ~= nil then
                                inventory:GiveItem(loot, nil, pos)
                            else
                                loot.Transform:SetPosition(pos:Get())
                            end
                            if loot.components.perishable ~= nil then
                                loot.components.perishable:LongUpdate(timeintrap)
                            end
                        end
                    end
                end
                
                ---Modded part---- 
                
                --if self.inst:IsValid() then
                    --self:Reset()
      --->       self:Set()
                    --if inventory ~= nil and
                    --   self.inst.components.finiteuses ~= nil and
                    --   self.inst.components.finiteuses:GetUses() > 0 then
                    --   inventory:GiveItem(self.inst, nil, pos)                    ----> this give the trap back in inventory if I understand right
                    --end
                --end
            end            
        end        
end) 

I also tried this

AddComponentPostInit("trap", function(self)    
    oldfn = self.inst.components.trap.harvest    
    self.inst.components.trap.harvest = function(doer)
    if self.issprung then... ect

 

and also tried AddClassPostConstruct and GlobalClassPostconstruct but theres always a code error

and also tried 100's permutations of theses codes, it is as if the componentpostinit doesnt apply at all

Can anyone guide me on the right track? plz?

 

Link to comment
Share on other sites

local G = GLOBAL
AddComponentPostInit("trap",function(cmp,prefab)
	if not G.TheWorld.ismastersim then return end
	cmp.Harvest=function(self, doer)
		if self.issprung then
			--Cache these because trap may become invalid in callbacks
			local pos = self.inst:GetPosition()
			local timeintrap = self.inst.components.timer ~= nil and self.inst.components.timer:GetTimeElapsed("foodspoil") or 0

			self.inst:PushEvent("harvesttrap")
			if self.onharvest ~= nil then
				self.onharvest(self.inst)
			end
			--WARNING: May have become invalid now!

			local inventory = doer ~= nil and doer.components.inventory or nil
			if self.lootprefabs ~= nil then
				for i, v in ipairs(self.lootprefabs) do
					local loot = G.SpawnPrefab(v)
					if loot ~= nil then
						if inventory ~= nil then
							inventory:GiveItem(loot, nil, pos)
						else
							loot.Transform:SetPosition(pos:Get())
						end
						if loot.components.perishable ~= nil then
							loot.components.perishable:LongUpdate(timeintrap)
						end
					end
				end
			end

			if self.inst:IsValid() then
           -- modification here
				if self.inst.components.finiteuses ~= nil and
					self.inst.components.finiteuses:GetUses() > 0 then
					self.inst:PushEvent("ondropped")
				end
			end
		end
	end
end)

 

Link to comment
Share on other sites

hey thanks for the reply,

looks like I was close to the solution

but I have some question plz

function(cmp,prefab)

why self didnt do it?

where did you get cmp from? is it just a random name you put?

 

if not G.TheWorld.ismastersim then return end---> means is server mod then right?

 

Edited by Noldaz
Link to comment
Share on other sites

Where did you put the code? It is server only, and has been tested.

3 hours ago, Noldaz said:

function(cmp,prefab)

The first parameter is the component, the second parameter is the prefab.

Link to comment
Share on other sites

Quote

 

name = "test"
description = "test"
author = "ptr"
version = "0.1.0"
forumthread = ""

api_version = 6
api_version_dst = 10
dst_compatible = true

server_only_mod = true
all_clients_require_mod = false
client_only_mod = false

configuration_options = 
{}

 

the code is modmain, and this is modinfo.

Link to comment
Share on other sites

ok, but ive read all function in trap components and nowhere there is a "cmp"

unless you mean i could have written this instead:

function(trap,prefab)

or

function(peanutbutter,prefab)

or is that cmp = components

as if inst.component.trap equals inst.cmp.trap

coz in fact its the part I need to understand to finish the mod

Edited by Noldaz
Link to comment
Share on other sites

I added an else in case the finiteuse component isnt present

if self.inst:IsValid() then
                    --self:Reset()
                    if self.inst.components.finiteuses ~= nil and
                       self.inst.components.finiteuses:GetUses() > 0 then
                       --inventory:GiveItem(self.inst, nil, pos)
                       self.inst:PushEvent("ondropped")
                    else
                        self.inst:PushEvent("ondropped")
                    end

end

Link to comment
Share on other sites

3 minutes ago, Noldaz said:

oups forgot to quote

 

7 minutes ago, ptr said:

It is just a formal parameter, you can name it whatever you like.

Please quote next time, so I can get the forum notification.

 

Link to comment
Share on other sites

16 minutes ago, ptr said:

Please quote next time, so I can get the forum notification.

I have the feeling it has to do with inventory and replica inventory with the loot, maybe I'm wrong

I'll try somethings

Link to comment
Share on other sites

19 minutes ago, Noldaz said:

ok now I got disconnected with the birdtrap, hummm, I guess its using the same trap component

I have just tested the birdtrap, it worked good with my code. I suggest you to check the server_log for errors

Link to comment
Share on other sites

7 minutes ago, Serpens said:

btw there are already working trap reset mods. I just tested it yesterday if it works with caves enabled and it worked:
http://steamcommunity.com/sharedfiles/filedetails/?id=679636739

yes I know but the coding seem more simple and elegant when changing the component than changing every prefab

for my disconnect, I have to add something like this:

local SpawnPrefab = GLOBAL.SpawnPrefab

Link to comment
Share on other sites

4 minutes ago, Noldaz said:

yes I know but the coding seem more simple and elegant when changing the component than changing every prefab

for my disconnect, I have to add something like this:

local SpawnPrefab = GLOBAL.SpawnPrefab

ok adding the spawnprefab global did the trick yay :)

Thank you alot sir ptr, I'll credit you in my mod, in which I'll integrate this

Link to comment
Share on other sites

I did not try it, but you have this:
http://steamcommunity.com/sharedfiles/filedetails/?id=831794009

and this:
http://steamcommunity.com/sharedfiles/filedetails/?id=825009701

client_only is more complicated and as far as I know clients do not have access to most components (did not check any files).
I'm just saying that in this special case, there are mods there, that do what you ask for, so there is no need to reinvent the wheal :)

Link to comment
Share on other sites

5 minutes ago, Serpens said:

I did not try it, but you have this:
http://steamcommunity.com/sharedfiles/filedetails/?id=831794009

and this:
http://steamcommunity.com/sharedfiles/filedetails/?id=825009701

client_only is more complicated and as far as I know clients do not have access to most components (did not check any files).
I'm just saying that in this special case, there are mods there, that do what you ask for, so there is no need to reinvent the wheal :)

yeah I already have thoses but if im not mistaken they use prefabpostinits instead of componentpostinits

so the way to do it client side is more automatisation of the process of putting down a trap vs altering the trap

Link to comment
Share on other sites

4 minutes ago, Noldaz said:

yeah I already have thoses but if im not mistaken they use prefabpostinits instead of componentpostinits

so the way to do it client side is more automatisation of the process of putting down a trap vs altering the trap

yes, but they do it this way, cause there is no other way if you want "client_only". You do not have access to the trap component as client.

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