I want to create a building system based on blueprints. I created everything but I need some logic.
I have an small leaf shelter made of 9 sticks and 19 leaves.
I implemented it like this:
public GameObject[] ConstuctionObjects;
public int[] PlacedObject;
public int[] MaxItems;
public bool[] finished;
public int index;
public GameObject[] TotalObjects;
public Vector3 positions;
public Quaternion rotations;
public Vector3[] scales;
public GameObject currentConstructionObject;
public GameObject currentConstructionObject;
void Update(){
if(nearBlueprint){
foreach (GameObject obj in ConstructionObjects)
{
if (PlacedObjects[Array.IndexOf(ConstructionObjects, obj)] == maxObjects[Array.IndexOf(ConstructionObjects, obj)])
{
finished[Array.IndexOf(ConstructionObjects, obj)] = true;
}
}
foreach (bool boolean in finished)
{
if (boolean == true)
{
currentConstructionObject = ConstructionObjects[Array.IndexOf(finished, boolean) + 1];
break;
}
}
if(Input.GetKeyDown(KeyCode.C)){
if(Inventory.ContainsitemWithTag(currentConstructionObject.tag)){
GameObject PlacedObject = Instantiate(currentConstructionObject, positions[index], rotations[index]);
PlacedObject.transform.parent = transform;
PlacedObject.transform.localScale = scales[index];
}
}
if(index == TotalObjects.Length){
//finished
Instantiate(FinalBuliding, transform.position, transfrom.rotation);
gameObject.SetActive(false); //disable the bliueprint ghost
}
}
}
Let me explain this because the way of coding is a little bit weird.(Maybe I missed some brackets because I selected only the important parts)
ConstructionObjects[ ] array stores the gameobjects that are used for a building. In this case:
ConstructionObjects[0] is Stick
ConstructionObjects[1] is Leaf
PlacedObjects[ ] array stores the count of placed objects of a specific construction object(the indexes match). in this case:
PlacedObjects[0] is how many sticks you placed
PlacedObjects[1] is how many leaves you placed
MaxItems[ ] array stores the items required of a specific construction objects
MaxItems[0] is how many sticks you need to place until finished[0] will be true
MaxItems[1] is how many leaves you need to place until finished[1] will be true
TotalObjects[ ] array stores the objects in the hierarchy order
currentConstructionObject gameObject keeps track of the current construction objects so first it is Stick and when you finished placing all sticks currentConstructionObject is leaf.
It works but with one problem.
If you have both sticks and leaves in inventory it works nice consuming first sticks and leaves after. but if you have only leaves without having sticks, the currentConstructionObject is sticks because that’s the priority order and leaves are not placed until you place only sticks.
I can do some if statements like this:
if(Inventory.ContainsitemWithTag(“leaf”) && !Inventory.ContainsitemWithTag(“sticks”)){
currentConstructionObject = Leaf;
}
if(Inventory.ContainsitemWithTag(“stick”) && !Inventory.ContainsitemWithTag(“leaf”)){
currentConstructionObject = Leaf;
}
if(Inventory.ContainsitemWithTag(“stick”) && Inventory.ContainsitemWithTag(“leaf”)){
currentConstructionObject = Stick;
}
But If I have 3 or more ConstructionObjects I need to check a lot of things and also I can’t use constants as Inventory.ContainsitemWithTag(“stick”) or Inventory.ContainsitemWithTag(“leaf”) because not all buildings are made of sticks and leaves.
P.S: If you ever played TheForest or GreenHell I want to recreate their building system where you can place blueprint even if you don’t have the required materials. you can place them later. not like in Rust where you need to have the items in inventory.