About 'Defenders Costing Resources'!

In this video (objectives)...

  1. Create a method that returns a bool for whether we have enough resources to buy a defender.
  2. On mouse click, spawn a defender and also reduce our total resources by the cost of the defender.

After watching (learning outcomes)...

Tie everything together so that clicking to place defenders costs resources.

(Unique Video Reference: 29_GL_CUD)

We would love to know…

  • What you found good about this lecture?
  • What we could do better?

Remember that you can reply to this topic, or create a new topic. The easiest way to create a new topic is to follow the link in Resources. That way the topic will…

  • Be in the correct forum (for the course).
  • Be in the right sub-forum (for the section)
  • Have the correct lecture tag.

Enjoy your stay in our thriving community!

I came from a c++ background so my method for completing this challenge may have been influenced by that, however I took a slightly different approach to your AttemptToPlaceDefenderAt(Vector2), that I would like to share.

I started by calling the StarDisplay.SpendStars(int) method in my DefenderSpawner.SpawnDefender(Vector2) method. This leaves me without a check, so that I will always buy, regardless of resource level. If I have 10 resources, I’ll spend 100 and end up with -90.

To add the check, I refactored StarDisplay.SpendStars(int) to return a bool of true if the cost was spent, and false if there weren’t enough resources to spend. Then I ran an if check on the outcome of StarDisplay.SpendStars(int) to spawn my Defender.

Here’s my code.

Before

In StarDisplay.cs
public void SpendStars(int amount)
{
    if (stars >= amount)
    {
        stars -= amount;
        UpdateDisplay();
    }
}

In DefenderSpawner.cs
private void SpawnDefender(Vector2 roundedPos)
{
   Defender newDefender = Instantiate(defender, roundedPos, Quaternion.identity) as Defender;
}    

After

In StarDisplay.cs
public bool SpendStars(int amount)
{
    if (stars >= amount)
    {
        stars -= amount;
        UpdateDisplay();
        return true;
    }
    return false;
}

In DefenderSpawner.cs
private void SpawnDefender(Vector2 roundedPos)
{
    if (FindObjectOfType<StarDisplay>().SpendStars(defender.GetCost()))
    {
        Defender newDefender = Instantiate(defender, roundedPos, Quaternion.identity) as Defender;
    }
}    
3 Likes

I took a very different approach to the spawning of defenders. My approach is largely influenced on the presentation of Ryan Hipple for Unite 2017, where he makes a case for using Game Events and FloatReferences and FloatVariables.

What I did was:

  • created ScriptableObjects to store information about each defender (name, prefab, cost, damage…)
  • created event to notify when a defender button is selected
  • created an IntVariable to store the value of the player’s stars
  • created an event to notify that the player’s stars have been changed

The reasons I took this approach were:

  • to try to keep the information about each defender in a single place and easily modifiable
  • to keep the information about the player’s stars in a single place
  • exercise the use of Game Events

I’m not posting the code since it would take up too much space to post each piece, but I hope this gives some idea of what I’m trying to accomplish.

rick created a whole new attemptToPlaceDefenderAt() function ; when i used this methode bellow; im a begginer and what to know if this would cause any bugs in the future that did not see, or my methode is also just fine and its just a matter of readability of the code !!
private void spawnDefender(Vector2 pos)
{
StarsDisplay starDisplay = FindObjectOfType();
int defenderCost = diffender.cost;
if (starDisplay.haveEnoughStars(defenderCost))
{
DefenderSet defender = Instantiate(diffender, pos, Quaternion.identity) as DefenderSet;
starDisplay.spendingStarts(defenderCost);
}

}

thank rick and all others your courses are just awesome :slight_smile:

Privacy & Terms