Jump to content

Recommended Posts

I'm trying to modify the local function setflowertype inside flower.lua to be able to choose which type will be planted

I want to do all the stuff in modmain.lua but I can't find where this function gets attached (idk if it's really supposed to) so I'd be able to do something like:

local old_setflowertype = inst.somewhere.something.setflowertype

What I wish I could do:

Quote

 


local function MyStuff(inst)
	local name = someName
	local names = {"f1","f2","f3","f4","f5","f6","f7","f8","f9","f10"}
	local ROSE_CHANCE = someNumber
	local old_setflowertype = inst.somewhere.something.setflowertype
	inst.somewhere.something.setflowertype = function(inst, etc)
  		if name == "random" then
    			inst.animname = math.random() < ROSE_CHANCE and "rose" or names[math.random(#names)]
  		else
    			inst.animname = name
    		end
  	end
end

AddPrefabPostInit("flower", MyStuff)
	 

 

Is it even possible?

I also don't understand what animname is. 

I've spent so many days trying to figure out how to do this with no luck. Any help appreciated, thanks!

 

Thanks for your reply, Aquaterion!

I see, well after reading @rezecib's post I thought it would be possible somehow though

It seems there's no way to change rose chance without modding that function, but would it be possible to do what I want by using inst.animname only?

 

Edited by Threnke

You could just copy the setflowertype function with some alterations and make it rerun in the AddPrefabPostInit on the flower. That should work just fine other than having a bit of redundancy.

 

AddPrefabPostInit("flower", function(inst)
	inst:DoTaskInTime(0 function()--delay a bit to make sure it runs AFTER the original function
		setflowertype(inst, inst.animname)
	end)
end)

 

It worked thanks!

With a few changes I got it working the way I wanted.

No delay needed, tested it. I think it's not necessary since the postinit function execute after the prefab loads, correct me if I'm wrong.

I had to change the prefab name from "flower" to "planted_flower" because it was turning all the flowers in the world into the one I chose.

Here's how it looks like:

AddPrefabPostInit("planted_flower", function(inst)
	local name = "rose"
	local function setflowertype(inst, name)
        	inst.animname = name
        	inst.AnimState:PlayAnimation(inst.animname)
	end
	if name ~= "random" then
		setflowertype(inst, name)
	end
end)

Now I'll see if I can make the buttons work... Thanks a lot :D

Edit: If there's something to be concerned about this code, please let me know

Edited by Threnke

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