Jump to content

Farm harvest x3 multiplier


Recommended Posts

modmain.lua:

local function CropPostInit(self)
	local _Harvest = self.Harvest
	self.Harvest = function(self, harvester)
		local pos = self.inst:GetPosition()
		local ret, product = _Harvest(self, harvester)
		if ret and product and harvester and harvester:HasTag("Crops3x") then
			local _prefab = product.prefab
			local _wetness = GLOBAL.TheWorld.state.wetness
			local _iswet = GLOBAL.TheWorld.state.iswet
			-- Giving function to schedule
			local _givefn = function()
				local extra = GLOBAL.SpawnPrefab(_prefab)
				if extra.components.inventoryitem then
					extra.components.inventoryitem:InheritMoisture(_wetness, _iswet)
					harvester.components.inventory:GiveItem(extra, nil, pos)
				else
					extra.Transform:SetPosition(pos.x, pos.y, pos.z)
				end
			end
			-- For some reason the game can display incorrect stack amounts when giving many items at once
			-- It is better to time them separately
			harvester:DoTaskInTime(0.33, _givefn)
			harvester:DoTaskInTime(0.66, _givefn)
		end
		return ret, product
	end
end
AddComponentPostInit("crop", CropPostInit)

This gives two extra products to the people who harvest a crop and have the tag "Crops3x".

So, after using this, you edit your character.

your_character_prefab.lua:

local function common_postinit(inst)
	inst:AddTag("Crops3x")
end

You add the tag to the common_postinit function of your character.

Link to comment
Share on other sites

interesting idea! :)

I will make a mod that gives a small chance to get 2 or 3 crops , of course with credits to you both. (don't know if I will publish it or only use for myself)
But will this be an all_client or a server_only mod?
And can I use things like GLOBAL.GetRandomMinMax(0,1) ? Or can this generate different results for every client and therefore unsyncs the game or simular ? (if this is a server only mod, please still answer the question about random for all_client mods)

Edited by Serpens
Link to comment
Share on other sites

18 hours ago, Serpens said:

But will this be an all_client or a server_only mod?

That code works at the server side. So the mod would be a server only mod.

18 hours ago, Serpens said:

And can I use things like GLOBAL.GetRandomMinMax(0,1) ? Or can this generate different results for every client and therefore unsyncs the game or simular ?

The random function of lua may generate different results on different simulations, so the server could get a 0.2 and the client could get a 0.5. It could unsync the game, but it doesn't matter. I don't even need a random function, I can just edit the files so the sanity/health/hunger points displayed are incorrect.

Because the server is constantly sending info to the clients, they will synchronize sooner or later. And if they don't sync, then it still doesn't matter, because the server cares about what is going on inside itself, and not about what is going on in clients.

Now the better question is, why would you run math.random on both server and client? If you run it on the server, you propagate the result to the client and that's it.

Link to comment
Share on other sites

49 minutes ago, DarkXero said:

Now the better question is, why would you run math.random on both server and client? If you run it on the server, you propagate the result to the client and that's it.

Thank you very much :)

that is the thing I was not aware of.  There are sooo many things I still don't know... and it seems the only way to learn all these things is making it wrong in the first place, or studying the whole game code =/
So for server_only I can use random without problem.
And for all_clients I should propagate the result to clients. How to do this? Or in which game files I can see examples of this?

Edited by Serpens
Link to comment
Share on other sites

56 minutes ago, Serpens said:

So for server_only I can use random without problem.
And for all_clients I should propagate the result to clients. How to do this? Or in which game files I can see examples of this?

local function OnDirtyEvent(inst)
	local val = inst.mynetvar:value()
	-- Use val and do client related stuff
end

local function RegisterListeners(inst)
	-- check that the entity is the playing player
	if inst.HUD ~= nil then
		inst:ListenForEvent("dirtyevent", OnDirtyEvent)
	end
end

AddPlayerPostInit(function(inst)
	-- defined in netvars.lua
	-- GUID of entity, unique name identifier (among entity netvars), dirty event name
	inst.mynetvar = GLOBAL.net_tinybyte(inst.GUID, "unique.name", "dirtyevent")

	-- set a default value
	inst.mynetvar:set(0)

	inst:DoTaskInTime(0, RegisterListeners)
end)

-- now when doing server stuff, use
-- inst.mynetvar:set(num), with inst being a player and num a number between 0 and 7
-- changes will propagate to clients

This is basically what happens.

Also, when you do "inst:AddTag("newtag")", the new tag will propagate and appear on the entities of clients automatically.

Link to comment
Share on other sites

@DarkXero Thank you very much :) I expected there would be a function that already exist in game files for doing this?! 

Are you also able to answer my question here?
http://forums.kleientertainment.com/topic/69205-possible-to-mod-entityscriptlua-function/?do=findComment&comment=798961
At least how to get the component.heater at client side is important.. without that I can't work further at my mod =/

 

 

Link to comment
Share on other sites

On 31.7.2016 at 11:17 PM, DarkXero said:

local function OnDirtyEvent(inst)
	local val = inst.mynetvar:value()
	-- Use val and do client related stuff
end

local function RegisterListeners(inst)
	-- check that the entity is the playing player
	if inst.HUD ~= nil then
		inst:ListenForEvent("dirtyevent", OnDirtyEvent)
	end
end

AddPlayerPostInit(function(inst)
	-- defined in netvars.lua
	-- GUID of entity, unique name identifier (among entity netvars), dirty event name
	inst.mynetvar = GLOBAL.net_tinybyte(inst.GUID, "unique.name", "dirtyevent")

	-- set a default value
	inst.mynetvar:set(0)

	inst:DoTaskInTime(0, RegisterListeners)
end)

-- now when doing server stuff, use
-- inst.mynetvar:set(num), with inst being a player and num a number between 0 and 7
-- changes will propagate to clients

This is basically what happens.

Also, when you do "inst:AddTag("newtag")", the new tag will propagate and appear on the entities of clients automatically.

@DarkXeroHow to propagate any float number to the client? At the moment I would like to show the inst.components.moisture:GetMoistureRate() in the clients HUD, but the client can't access moisture... I tried this mynetvar function, but this does not help with int numbers between 0 and 7 ...=(

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