Jump to content

Is there a way to Upvalue Hack this local variable?


Recommended Posts

I want to change friendly fruit fly's SEE_DIST to extend its range in finding farm plants to tend. However, this SEE_DIST is a local variable in findfarmplant.lua, which is in scripts/behaviors. I've only dabbled with AddPrefabPostInit so far and I've no idea how to modify scripts in scripts/behaviours.

On the other hand, I've dabbled with Upvalue Hacker and I've tested and made a handful of modifications with it. But how can I reach SEE_DIST with it?

Here's its reference chain:

Prefabs.friendlyfruitfly.fn -> *"friendlybrain" -> "FriendlyFruitFlyBrain:OnStart" -> "FindFarmPlant" -> "SEE_DIST"

friendlybrain is however a "table" (it's a file called from require). I've tried skipping this to no avail.

If Upvalue is out of the question, is there another way? I know findfarmplant.lua is just a small script and there would probably be no harm in just loading my own, but I would love to know first how to do it the "clean" way, if there is.

Edit: I just realized I don't even know how to replace non-Prefab files via modmain.lua...

Edited by GrowthMindset
Just realized
Link to comment
Share on other sites

This should probably be as simple as:

local MY_SEE_DIST = 160 --this is the diameter of a player's render range, which is definitely excessive

AddClassPostConstruct("behaviours\findfarmplant", function(self)
    UpvalueHacker.SetUpvalue(self.PickTarget, MY_SEE_DIST, "SEE_DIST")
end)

You can definitely just replace the script, though. Use a custom TUNING value in the replacement script if you want to make it configurable.

Edited by Bumber64
Link to comment
Share on other sites

1 hour ago, Bumber64 said:

This should probably be as simple as:


local MY_SEE_DIST = 160 --this is the diameter of a player's render range, which is definitely excessive

AddClassPostConstruct("behaviours\findfarmplant", function(self)
    UpvalueHacker.SetUpvalue(self.PickTarget, MY_SEE_DIST, "SEE_DIST")
end)

You can definitely just replace the script, though. Use a custom TUNING value in the replacement script if you want to make it configurable.

Thanks but I figured it out. I remember trying AddClassPostConstruct and failing with the logs saying findfarmplant isn't a proper class.

But this worked:

AddBrainPostInit("friendlyfruitflybrain", function(brain)
	local SEE_DIST = 100
	local index = nil
	for i,v in ipairs(brain.bt.root.children) do
		if v.name == "FindFarmPlant" then
			index = i
			break
		end
	end
	UpvalueHacker.SetUpvalue(brain.bt.root.children[index].PickTarget, SEE_DIST, "SEE_DIST")
end)

The next problem was it also affected hostile fruitflies. So I thought of a better and more-future proof solution.

Edited by GrowthMindset
made whitespace consistent, although tab sucks in this forums.. hmm..
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...