Jump to content

New bees getting stuck after editing findflower behavior (solved)


Recommended Posts

I was trying to make a new type of bee, and I seem to have gotten it mostly working, except for some kind of error in their pollination. I wanted to make them require less flowers for pollination so I made them their own pollinator component, but this broke the game unless I also made a new brain and findflower behavior to go with it. I think the brain is working ok, as spawned bees will wander, properly aggro, return home at dusk, and seek flowers, but after they find a flower and rest on it, they become perpetually stuck flying above it and no longer move until dusk comes, at which point they return home. I figure since the other behaviors are working it must be the new findflower that's causing problems but I can't see where.

(Also if someone could help me figure out how to give the player a regular bee when these are caught instead of the new bee that would also be appreciated, but that's secondary)

findflowernew.lua

honeybeebrain.lua

betterpollinator.lua

Edited by Mayli-Song
Link to comment
Share on other sites

Bump, I still haven't been able to figure out what causes the bees to stop moving or doing anything after landing on a flower. All I did in any of these files was change some names, and I changed the amount of flowers needed to be considered full in the pollinator component. I still don't know what would have broken the bees

Link to comment
Share on other sites

Ehm...you don't need all that. You can just set the "collectcount" variable on all bees using AddPrefabPostinit.

AddPrefabPostinit("bee", function(inst)
	if inst.components.pollinator then
		-- Set number of flowers needed for full pollination. Default is 5
		inst.components.pollinator.collectcount = 3
	end
end)

That's just one of the bee-prefabs, but I think it's the only one you need.

Link to comment
Share on other sites

2 hours ago, Ultroman said:

Ehm...you don't need all that. You can just set the "collectcount" variable on all bees using AddPrefabPostinit.

So I don't actually want all the bees to be better pollinators I just want this one new type of bee to be, would it be the same idea?

Link to comment
Share on other sites

Because I still want regular bees to be regularly good at their job and otherwise be completely unchanged, but I'm working on a character who can make a better bee box that spawns new honeybees, which are more efficient bees that only spawn from this bee box. Ideally I'd also make the honeybees give regular bees when caught but I'm more worried about actually getting the bees working first.

Link to comment
Share on other sites

You can just spawn normal bees, and right after you've spawned them, you just set that value.

No need for a new prefab for that. Or new brains or stategraphs or components or anything.

Just like the pollinator component does for flowers:

local flower = SpawnPrefab(parentFlower.prefab)
flower.planted = true

you can do:

local bee = SpawnPrefab("bee")
bee.components.pollinator.collectcount = 3

 

Edited by Ultroman
  • Like 1
Link to comment
Share on other sites

That definitely does seem more straightforward than what I was doing. I had a different sprite, different stats and different drop ratios for the honeybees which I'd be a little bummed to lose, would those also be able to be added with that method? Also I'm pretty new at this so would the edits to the bees with that method be added to the beebox or the bees, or some other file? It's my understanding AddPrefabPostInit can only be used in modmain but it seems like maybe this is not using AddPrefabPostInit, and that was a seperate method to what you mentioned first?

Link to comment
Share on other sites

Added some code examples.

I assumed you had made your own beebox prefab which spawns bees, either within its own code, or using a component.

You should be able to make most of the same changes using the mod API (to edit existing prefabs, instead of making your own prefab). It depends on exactly which changes you want to make and which functions are public or local in the components you've edited.

Edited by Ultroman
Link to comment
Share on other sites

So yes, I have a second bee box prefab, which I can attach. It's a copy of the original/vanilla bee box prefab but with names, stats, and textures changed. I tried putting the example you gave in several different places in the royalbeebox prefab file, all of which either crashed with an error or did nothing. I tried both above and below the part that checks for TheWorld.ismastersim. I unfortunately did this all super late at night and forgot to record what kind of errors I got so I'm going to do that now but suffice to say the places I tried have not yet worked.

royalbeebox.lua

Edited by Mayli-Song
Link to comment
Share on other sites

Instead of trying to paste it into every spot you can think of, you should figure out where your bees are being spawned. There is no SpawnPrefab() call in the beebox code, but if you study it a bit, you'll notice it has a component called "childspawner", which handles a population of whatever prefab you tell it to. This is code from your royalbeebox.lua. Read my comments.

// REMOVE THIS - START
local bee = SpawnPrefab("bee")
if TheWorld.ismastersim then
    bee.components.pollinator.collectcount = 3
    bee.components.locomotor.walkspeed = 5.5
    bee.components.health:SetMaxHealth(70)
    bee.components.combat:SetDefaultDamage(7)
    bee.AnimState:SetBuild("honeybee_build")
end
// REMOVE THIS - END

if not TheWorld.ismastersim then
	return inst
end

---------------------  
inst:AddComponent("harvestable")
inst.components.harvestable:SetUp("honey", 12, nil, onharvest, updatelevel)
inst:ListenForEvent("childgoinghome", onchildgoinghome)
-------------------

-- Your childspawner is set up here, with the prefab name of the child, etc.
inst:AddComponent("childspawner")
inst.components.childspawner.childname = "bee"
inst.components.childspawner.allowwater = true
SeasonalSpawnChanges(inst, TheWorld.state.season)
inst:WatchWorldState("season", SeasonalSpawnChanges)

if TheWorld.state.isday and not TheWorld.state.iswinter then
	inst.components.childspawner:StartSpawning()
end

Now, the childspawner component has a function appropriately called DoSpawnChild, which calls this line when it has spawned a child:

self.onspawned(self.inst, child)

The onspawned function is not used by default. It's there for you to use. That said, we never know if another mod is also using onspawned for bees, so we make sure to extend the function if there's one already. Put this in after the other inst.components.childspawner lines in master_postinit.

local oldOnspawned = inst.components.childspawner.onspawned
inst.components.childspawner.onspawned = function(childspawner, child)
    child.components.pollinator.collectcount = 3
    child.components.locomotor.walkspeed = 5.5
    child.components.health:SetMaxHealth(70)
    child.components.combat:SetDefaultDamage(7)
    child.AnimState:SetBuild("honeybee_build")
	
	if oldOnspawned ~= nil then
		oldOnspawned(inst)
	end
end

 

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

And this is why you shouldn’t attempt to troubleshoot code late at night. That seems so blatantly obvious, of course SpawnPrefab isn’t called, but somehow I could not see it. Thank you, sorry for the obvious mistake there but I appreciate you bearing with me.

  • Like 2
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...