Skip to main content

CraftingPart

A class in Root.Township, Alta.Crafting.

Network behaviour on an item that can be joined to other parts (blade into guard, guard into handle). Tracks the part tree, size, weight and impactors, and handles craft requests. A pickup reaches it through Pickup.CraftingPart; joining itself goes through JointSlot.

Constructors​

MemberWhat it is
CraftingPart()Unity makes components itself; use AddComponent or spawn a prefab instead of new.

Fields​

19 private and internal fields
MemberWhat it is
private bool addDurabilityToSlotMakes this part's durability attach to the nearest parent part that has its own durability module.
private MethodSyncStruct<uint> attemptCraftNetwork sync that sends a craft attempt (the slot's entity id) to the server.
private bool canBlockDefaultWhether the pickup could block before crafting. Parts that can't are set to block physical only while crafted.
private bool canStoreFlippedWhether the crafted item may be stored flipped, see IsStoredFlipped.
private float currentLengthLength of this part plus the lengths of parts added along its axis. Sets the store scale.
private DurabilityModule durabilityModuleThis part's durability module, if any.
private Component[] hitDependantsChild components that react to the tool's hits, handed to the root's ImpactTool when crafted.
private ImpactorMap[] impactorMapsBacking field for ImpactorMaps.
private ImpactTool impactToolBacking field for ImpactTool. Disabled while this part is a crafted child.
private JointInsert insertBacking field for Insert: the joint that plugs this part into another part's slot.
private bool isBasedOnInteractionWhether grabbing the part checks for crafting connections with the other hand's item. Default true.
private bool isStartedSet once the root changed event first fires, so ListenToRoot can call new listeners at once.
private float lengthBase length of the part along its length axis.
private Vector3 lengthAxisLocal length axis, behind LengthAxis.
private float massRigidbody mass saved on awake, restored when the part is removed and gets a new rigidbody.
private NetworkRigidbody networkRigidbodyThe part's network rigidbody. Its rigidbody is destroyed while crafted and recreated on removal.
private Pickup pickupBacking field for Pickup.
private Rigidbody rigidbodyThe part's current rigidbody, null while it is a crafted child.
private JointSlot[] slotsBacking field for Slots: the joint slots other parts can connect to.

Properties​

MemberWhat it is
CraftingPart ConnectedTo { get; }The part this one is inserted into, or null if it is the top.
ImpactorMap[] ImpactorMaps { get; }The impactor maps found in this part's children.
ImpactTool ImpactTool { get; }The ImpactTool on this part, if any. The top part's tool takes over child impactors.
JointInsert Insert { get; }The JointInsert that plugs this part into another part's slot.
bool IsStoredFlipped { get; }Whether the crafted object should sit flipped in a dock, based on which side holds more size.
Vector3 LengthAxis { get; }The part's length direction in world space.
int PartCount { get; private set; }How many parts are in this part's subtree, including itself.
Pickup Pickup { get; }The pickup on the same object.
CraftingOptions PotentialConnection { get; set; }The connection options currently being shown for this part, if any.
int Size { get; private set; }Total size of this part and everything inserted into it. Starts at the item's size.
JointSlot[] Slots { get; }The joint slots other parts can go into.
float TotalWeight { get; }This part's weight plus the total weight of every part inserted into it.
1 private and internal property
MemberWhat it is
private bool Alta.Crafting.IBlockGenericDecrafting.CanDecraft { get; }Generic decrafting is only allowed while this part's pickup isn't crafted.

Events​

MemberWhat it is
event CraftingChanged OnCraftedRaised when a connection involving this part is made, with whether it came from loading.
event CraftingChanged OnDecraftedRaised when a connection involving this part is removed, with whether it came from loading.
event Action<CraftingPart> RootChangedRaised when the root of this part's crafted object changes.

Methods​

MemberWhat it is
void AddSize(CraftingPart insert, Quaternion rotation, out float dot)Adds an inserted part's size, part count, length and colliders to this part and every part above it. Called by JointSlot on connect.
void AttachDurability(CraftingPart to)If set to pass durability on, links this part's durability to the nearest part above that keeps its own.
virtual void AttemptCraft(JointSlot slot)Asks to connect this part into a slot. The server does it at once; a client sends the request to the server.
static bool CompareBuild(CraftingPart partA, CraftingPart partB)Static: true if two parts are built the same (material, durability stage and the same items in each slot, recursively). Docks use it for stacking.
void Crafted(bool isLoad)Raises OnCrafted. On the server, also broadcasts an ItemCraftedEvent on the township event bus when a player did it.
void Decrafted(bool isLoad)Raises OnDecrafted.
void DetachDurability(CraftingPart from)Unlinks this part's durability from the part it was joined to.
void DisconnectFull()Takes every part off this one, deepest first, then pulls this part out of its slot. The server calls it when the pickup breaks.
CraftingPart GetTopLevel()The root part of the crafted object this part is in (itself if not inserted).
override void Initialize()Sets up the synced craft request message.
override void InitializeAsServer(SpawnData spawnData)Server: hooks the pickup's break event so a breaking crafted item comes apart.
void ListenToRoot(Action<CraftingPart> listener)Subscribes to RootChanged and, if already started, calls the listener with the current root.
void LostChildAsTopLevel(CraftingPart part, JointSlot slot)Called on the root when a part comes off: updates crafted state, scale, impactors and re-grips held hands.
void OnRootChanged(CraftingPart root)Raises RootChanged with the new root part.
void OnWeightChanged()Passes a weight change on to the pickup.
void PrepareForInsert()Undocks the part, makes it non-interactable and crafted, and removes its rigidbody before it is joined.
void PrepareForRemoval()Makes the part interactable again and gives it back a rigidbody before it is taken off.
void ReceivedChildAsTopLevel(CraftingPart part, JointSlot slot, float craftingDot)Called on the root when a part is added: marks crafted, adjusts dock scale, registers impactors and re-grips the hand.
void RemoveSize(CraftingPart insert, LootValue bestValue)Removes a taken-off part's size, count, length and colliders here and above, and updates the loot value.
protected virtual void Awake()Reads the rigidbody mass, sets size from the item and, if crafting is based on interaction, watches grabs to offer connections.
protected void CraftWithSlot(JointSlot slot, bool isIgnoringPreConfirmation = false)Protected: connects into a slot if the slot is free and supports this part's insert type.
12 private and internal methods
MemberWhat it is
private void AttemptCraft(IPlayer player, Stream stream, uint slotIdentifier)Asks to connect this part into a slot. The server does it at once; a client sends the request to the server.
private void Break(Pickup pickup)Server: when the pickup breaks while crafted, disconnects every part below it and itself from its parent.
private void CheckPossibleConnections(Interactable interactable, Interactor interactor)Handler for the pickup's InteractionStarted: if the other hand holds a crafting part, creates CraftingOptions for the two.
private void DrawLength(Transform insertPosition)Draws the part's length along its length axis as a magenta gizmo.
private static void GetMapsOnPart(CraftingPart part, HashSet<ImpactorMap> maps)Collects the impactor maps of a part and of every part connected below it.
private bool HasAllCraftedEventPrerequisites()Checks the pickup, last holder's player, prefab and parent prefab all exist before the crafted event is broadcast.
private void OnDrawGizmosSelected()Draws the length gizmo from the first slot, or from the part itself if it has no slots.
private void OnTopLevelChildrenChanged()Destroys the current crafting preview and checks for new connections if the pickup is held.
private void OnValidate()Empty.
private static void ProcessCraftedEvent(IPlayer player, uint primaryPartHash, uint secondaryPartHash)Server (or local test): broadcasts an ItemCraftedEvent with the two part prefab hashes on the township event bus.
private void Alta.Networking.IEntityReplaceHandler.ReplacedWith(NetworkEntity entity, ReplaceAvoidFlags replaceAvoidFlags)When the entity is replaced, respawns each connected child part and connects it to the matching slot on the replacement.
private void Start()Raises the root changed event with the current top level part.

Used in​

Members of other types that take, return or hold a CraftingPart:

ChiselingFilterSettings.GetBestChiselingTool, AdditionalOwnedDock.RootChanged, AdditionalOwnedDock.tracking, ConfirmationCraftingPart.CraftClear, CraftingChanged.BeginInvoke, CraftingChanged.Invoke, CraftingOptions.CompilePossibilities, CraftingOptions.CraftingOptions, CraftingOptions.Create, CraftingOptions.insertPart, CraftingOptions.Involves, CraftingOptions.slotPart, CraftingPartTracker.CraftingPartTracker, CraftingPartTracker.Part, CraftingTester.GetVariants, CraftingTester.insertMap, CraftingTester.RandomizeSlots, CraftingTester.roots, CraftingTester.workingRandomList, DecraftingComponent.AddPartSlots, DecraftingComponent.currentPart, DecraftingComponent.ReMapSlots, DecraftingComponent.ValidateCraftingPart, DisableLightOnDock.RootSet, GenericAssemblyPart.craftingPart, GenericAssemblyPart.CraftingPart, GenericAssemblyStation.FixPosition, GenericAssemblyStation.ValidateCraftingPart, JointPoint.craftingPart, JointPoint.CraftingPart, JointSlot.AlignAndChild, JointSlot.CalculateConnectedRotation, JointSlot.ConnectedPart, JointSlot.TryConnect, Fire.craftingPart, Fire.RootSet, CraftableImpactDetectorBase.craftingPart, CraftableImpactDetectorBase.RootSet, EmbeddableManager.unEmbedAllOnCrafted, EmbeddableManager.UnEmbedAllOnCrafted, ImpactModifier.basePart, ImpactModifier.OnRootChanged, ImpactTool.UpdateIndexedImpactorsOnCraftingPart, CraftedDocks.ChangedRoot, CraftedDocks.craftingPart, DockedOffset.craftingPart, DockedOffset.RootChanged, DockedOffset.top, OnlyAllowDockingWhenEmpty.craftingPart, OnlyAllowDockingWhenEmpty.RootChanged, OnlyAllowDockingWhenEmpty.top, ArrowFlightModifierComponent.craftingPart, ArrowFlightModifierComponent.OnCrafted, ArrowFlightModifierComponent.OnDecrafted, ItemCraftedStep.craftingParts, DiggingImpactor.OnRootChanged, Explosive.RootChanged, HierarchyInteractionFilter.UpdateParent, Pickup.craftingPart, Pickup.CraftingPart and 4 more.