Sign in to follow this  
Bukinnear

Replaced function not receiving arguments

Recommended Posts

Bukinnear    1

Hi, hoping someone can lend me a hand with this. 

TL;DR: I replaced a function, and the arguments passed into it are nil. Why?

Context

I am trying to edit the standard evergreens prefab so that trees drop more logs. Problem is that all of the functions in evergreens.lua are local functions, and the variable I need to change is the builds variable (which contains all of the potential loot pools for trees when they transition between growth states).

So I abandoned the idea of editing any of that (if there is a way to edit local variables, please let me know), and instead decided to try intercept the lootpool change under inst.components.lootdropper (to be clear, I am still editing the evergreens.lua prefab).

The Problem

When I try to extend/replace inst.components.lootdropper.SetLoot, the arguments passed in to the new function are nil.

Is this a LUA scoping issue that I am not understanding? I remember seeing a post from one of the big posters (rezecib?) about how to override functions, but cannot for the life of me find it again.

Code

local function SetLootPool(inst)

	-- only run on the server
	if not GLOBAL.TheWorld.ismastersim then
		return
	end
	
	-- make sure we have the right component
	if (inst.components.lootdropper == nil) then
		return
	end
  
	-- Remember the old function
	OldSetLoot = inst.components.lootdropper.SetLoot
	
	-- Replace the function
	inst.components.lootdropper.SetLoot = function(loots)
		
		print(OldSetLoot)
		print(inst.components.lootdropper.SetLoot)
		print(loots[1])
    		-- This outputs:
    		-- [00:01:20]: function: 0B2D2310	
    		-- [00:01:20]: function: 05E98F20	
    		-- [00:01:20]: nil	
		
		-- Call the old function now. Obviously, this sets the loot pool to nil
		OldSetLoot(loots)
	end			
end

-- Just testing on the "normal" evergreens for the time being. 
-- I spawn one in, and use Wicker's book to trigger the above
AddPrefabPostInit("evergreen_normal", SetLootPool)

 

Share this post


Link to post
Share on other sites
Ultroman    750

Because SetLoot is defined as such:

function LootDropper:SetLoot(loots)

When they're defined this way, with a colon, it's shorthand for

function SetLoot(self, loots)

When extending such functions like you did with a . instead of , just add self as an "extra" first parameter.

  • Thanks 1

Share this post


Link to post
Share on other sites
CarlZalph    5266

Secondly, ensure your local scoping.

OldSetLoot = inst.components.lootdropper.SetLoot

Should have 'local ' in front of it so you're not using the last known tree's function on the server.

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites
rawii22    29

I think the forum you're talking about could be this one; scroll down to Tables as objects (and how to modify their functions nicely) section. He has a portion about function replacement.

 

  • Thanks 1

Share this post


Link to post
Share on other sites
Bukinnear    1

@Ultroman Thank you so much!

I thought the colon was only for calling the function, I didn't realise I could define it with the same. I didn't look carefully at the definition, and on top of that, It was one of your older posts that I was basing my work off of here. You explain that in the very first code snippet >.<

@CarlZalph Ah, yes, I did have trouble with that, and corrected that previously. This was just left over from an experiment to see if I printed them, whether it would show a different value, or whether it would be the same. They were different values, but judging by your comment, so I assume it was printing the reference variable, not the value.

That being said, I just tested it and it seems to work fine, so ¯\_(ツ)_/¯

A side note, I have been relying heavily on both of you from posts dating back quite a ways, so it's an honour to have both of you answer so quickly <3 Thank you both for your assistance!

@rawii22 I think I saw something different, but I have been heavily relying on this guide as well. Thanks!

  • Like 1

Share this post


Link to post
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
Sign in to follow this