Jump to content

Recommended Posts

So I have a prefab for a custom buildable set up, and it uses prefabutil.lua to create a placeable for it.

However, I cannot seem to figure out how placers work. By placer I mean the green outline you get when building something like a science machine. Adding a recipe to modmain that crafts "custombuilding_placer" (the name of the placer) results in a single framed custom building spawning at the player's location.

 

So how can I go about making the placer thing work?

Any help would be greatly appreciated!

It's hard to give specifics without seeing your prefab code so far, but technically your recipe should be crafting "custombuilding" and also pass the name of the placer.  Your prefab file should return both a Prefab(...) and a MakePlacer(...).   I assume this is what you're doing already.

 

Take a look at recipe.lua for the parameter list of its constructor function:

Recipe = Class(function(self, name, ingredients, tab, level, placer, min_spacing, nounlock, numtogive)

The min_spacing parameter defines how big of an area placement requires.  it defaults to 3.2 if you don't pass this argument.

 

Your building recipe will look something like:

Recipe("custombuilding", {Ingredient("boards", 3)}, RECIPETABS.TOWN, TECH.SCIENCE_ONE, "custombuilding_placer", 4)

-----

 

Your placer will push an "onbuilt" event, so you need your prefab to listen for this if you want an animation when it gets built. Assuming you have called inst.entity:AddAnimState() in your prefab's initialization function with something like local anim = inst.entity:AddAnimState(), and also set the build and bank name, you can do that like this:

inst:ListenForEvent( "onbuilt", function()    anim:PlayAnimation("place")    anim:PushAnimation("idle",false)  -- chains the idle animation onto the end of the place animation     inst.SoundEmitter:PlaySound("path/to/yoursound") -- You have to have added a SoundEmitter prior, naturallyend)

You should take a peek into the placer.lua component to see what's going on behind the scenes.

It's hard to give specifics without seeing your prefab code so far, but technically your recipe should be crafting "custombuilding" and also pass the name of the placer.  Your prefab file should return both a Prefab(...) and a MakePlacer(...).   I assume this is what you're doing already.

 

Take a look at recipe.lua for the parameter list of its constructor function:

Recipe = Class(function(self, name, ingredients, tab, level, placer, min_spacing, nounlock, numtogive)

The min_spacing parameter defines how big of an area placement requires.  it defaults to 3.2 if you don't pass this argument.

 

Your building recipe will look something like:

Recipe("custombuilding", {Ingredient("boards", 3)}, RECIPETABS.TOWN, TECH.SCIENCE_ONE, "custombuilding_placer", 4)

-----

 

Your placer will push an "onbuilt" event, so you need your prefab to listen for this if you want an animation when it gets built. Assuming you have called inst.entity:AddAnimState() in your prefab's initialization function with something like local anim = inst.entity:AddAnimState(), and also set the build and bank name, you can do that like this:

inst:ListenForEvent( "onbuilt", function()    anim:PlayAnimation("place")    anim:PushAnimation("idle",false)  -- chains the idle animation onto the end of the place animation     inst.SoundEmitter:PlaySound("path/to/yoursound") -- You have to have added a SoundEmitter prior, naturallyend)

You should take a peek into the placer.lua component to see what's going on behind the scenes.

Wow. You are a lifesaver. Your assumptions were both correct, a prefab was made, along with a placer(as that's what I was mistakenly building)

 

I understand that the "onbuilt" event is called, I just don't understand what triggers it. Is the recipe.lua responsible?

I understand that the "onbuilt" event is called, I just don't understand what triggers it. Is the recipe.lua responsible?

 

This one is a bit of a rabbit hole, but essentially the event gets pushed by builder.lua, which defines the builder component that is attached to the player in player_common.lua.  That's what handles recipes. You'll notice that it pushes two events since not everything gets every event:

self.inst:PushEvent("buildstructure", {item=prod, recipe = recipe})prod:PushEvent("onbuilt")

 

This one is a bit of a rabbit hole, but essentially the event gets pushed by builder.lua, which defines the builder component that is attached to the player in player_common.lua.  That's what handles recipes. You'll notice that it pushes two events since not everything gets every event:

self.inst:PushEvent("buildstructure", {item=prod, recipe = recipe})prod:PushEvent("onbuilt")

 

Alright, thanks!

Alright, thanks!

 

Acctually I think I made it little easier way:

 

From scratch - create custom item http://forums.kleientertainment.com/topic/29427-tutorial-creating-a-handslot-equippable-item-from-scratch/

 

- POINT 3 for you from this tutorial above- need only "ground item" image any size you need, and of course inventoryimage 64x64pixels.

 

In your character.lua:

 

local youritem_recipe = Recipe("youritem", {Ingredient("rocks", 2)}, RECIPETABS.TOWN, TECH.NONE, "youritem_placer" )
youritem_recipe.sortkey = 2
youritem_recipe.atlas = resolvefilepath("images/inventoryimages/youritem.xml")
STRINGS.RECIPE_DESC.YOURITEM = "it's my item!!!"
part in the red adds the "green shadow" you use to place your items on the ground. Without that character will build item just under his feet. 
 
 
in youritem.lua:
 
 inst.AnimState:SetBank("youritem")
 inst.AnimState:SetBuild("youritem")
 inst.AnimState:PlayAnimation("idle")   <<< animation you made in Spriter
 
Don't really need the "ONBUILT" from what i noticed. I don't have it - and all works fine. 

 

Edited by Foxrai

 

Acctually I think I made it little easier way:

 

From scratch - create custom item http://forums.kleientertainment.com/topic/29427-tutorial-creating-a-handslot-equippable-item-from-scratch/

 

- POINT 3 for you from this tutorial above- need only "ground item" image any size you need, and of course inventoryimage 64x64pixels.

 

In your character.lua:

 

local youritem_recipe = Recipe("youritem", {Ingredient("rocks", 2)}, RECIPETABS.TOWN, TECH.NONE, "youritem_placer" )
youritem_recipe.sortkey = 2
youritem_recipe.atlas = resolvefilepath("images/inventoryimages/youritem.xml")
STRINGS.RECIPE_DESC.YOURITEM = "it's my item!!!"
part in the red adds the "green shadow" you use to place your items on the ground. Without that character will build item just under his feet. 
 
 
in youritem.lua:
 
 inst.AnimState:SetBank("youritem")
 inst.AnimState:SetBuild("youritem")
 inst.AnimState:PlayAnimation("idle")   <<< animation you made in Spriter
 
Don't really need the "ONBUILT" from what i noticed. I don't have it - and all works fine. 

 

 

The other guy already explained it, I just couldn't figure out how to add the placer at first because I didn't know the placer could be used as an argument. I know "ONBUILT"  isn't necessary, but it's convenient to know how things work before I use them later.

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