Talking to the Rubber Duck

Talking to the Rubber Duck:

  1. Within public class, making it available globally within the entire script.
    a. Identifying the number of steps as an integer and in increments of one.
    b. Using vector math, identifying the location of Home and Player.

  2. The start function is calling the Welcome Message.

  3. The welcome message is comprised only of print statements. Since the problem is that the game continues after it completes, then this area cannot be the problem.

  4. The update is called every frame which is essentially every time the player moves one step.
    a. If the input is down, up, left, or right arrow respectively then calculate the current player location using the player location, plus increment in whichever direction the player moved. The variables applied with vector math gives a new value which is printed to the console.

  5. The PrintUpdateandContinue function is called every frame, after the player location is worked out.
    a. When the game is over we have a print statement which is designed to print to the console ā€œGame Overā€. This must be where the problem is because it simply is printing the words to the console and nothing to end the program.

So, we need something to prevent the user the ability to continue the game. This is different than terminating the game with the ESC key, which can be added later unless using iOS. Time to look through Unity Docsā€¦

I found a reference at the following url:

However, the solution seems to be beyond the scope of our project so I will post this and have a look at the answer in the course video. I donā€™t currently understand what is needed to resolve the problem.


Update: After watching the course video, I see that the solution is very easy. LOL.

1 Like

ā€¦and all the while the Duck Sensei sat there patiently, listening to you, hearing your words and thinking, ā€œSoon, he will watch the course video and realise how very easy the solution isā€¦ā€

:smiley: :smiley: :smiley:

LOL, I hear ya. It was painful for me to do it that way rather than just looking ahead. But, I played the game and actually glad that I did because I discovered a few things in Unity Docs, and learned how to write a short script to close the game using the ESC key.

1 Like

Heheā€¦ well done - always awesome to learn knew stuff :slight_smile:

1 Like

Thatā€™s why struggling through is always better!

Love the rubber duck @Rob. Looks like we need to be making some sort of branded rubber duck merch.

1 Like

hehe, I did suggest it a little while ago @sampattuzzi, itā€™s under the branding topic in the moderators forum :slight_smile: Think Ben had some concerns regarding shippingā€¦ would be kind of unique though :slight_smile:

My rubber duck is always listening :wink:


:+1: Fail Faster - there is no instant win :+1:
Twitter: @GamedevCala - Blog: nerd-time.com - Twitch: GamedevCala

4 Likes

:smiley: :smiley: :smiley:

He looks like heā€™s cooking up some solutions :wink:

1 Like

My rubber duck:

2 Likes

I see he has started producing paper based notation as his mind of awesomeness has begun to overfillā€¦ :slight_smile:

Ok, just paused the video for this challenge and had myself a think.

  1. The Update function is polling the arrow keys every frame, regardless of the game state (player has reached home or player has not yet reached home)
  2. So far, weā€™ve learned about conditions and booleans
  3. We need to figure out a way to stop listening to the keys once the goal has been reached and exit that state either by calling a ā€˜blankā€™ method, or ā€˜stopping the gameā€™
  4. Figure out how/where to set a condition when the player has not yet reached home. There is a gameOver boolean in the method called by each of the Update conditions, but I donā€™t like that this boolean is being declared in this method. Not sure if it should be declared at the top-level or just set another one.

So, after a few minutes of tinkering, this is what I came up with:

  1. Initialized a new boolean gameWin = false up at the root so it is accessible by all functions below
  2. Before polling the keys in Update, I set the condition to check if (gameWin == false) and nested all the arrow keys conditions within this boolean check. As long as gameWin has a value of false, then check the keys during the Update method. I didnā€™t put any code to execute when the condition of this boolean evaluates to true
  3. In the PrintPosition function, if the gameOver condition evaluates to true, I added the gameWin = true expression after the print statement so that on the next updated frame, the keys will no longer be active.

Hope that made sense, and hopefully Sam did something similar :slight_smile:

Canā€™t we give a break command or Exit command. Just paused and posted this thing.

Pretty much exactly right. Out of interest, have you programmed before? :wink:

Unfortunately we arenā€™t actually in any loop. We could delete the game object as a way of not getting called and essentially ā€œbreakingā€ out of the loop.

@sampattuzzi I can translate, read and ā€œspeakā€ code, but I have never been a ā€œproper programmerā€. Iā€™ve been taking all of Benā€™s Unity courses to get me some C# scripting experience. I canā€™t program my way out of a paper bag; I am still learning, and it is usually the syntax of the (any) language that trips me up.


I must be old school, instead of a rubber duck we used to call it talking to a wooden Indian.

2 Likes

ā€¦bit harder to put on your desk though :wink:

Everyone has it different. My tale used to be a potted plant outside a professors office.

1 Like

syntax is what Google and the compiler are for!

Yes, I agree. Also the Unity scripting manual.

A better example of my coding level/experience would be if I were in Germany speaking with someone who only spoke German. I could understand most of what is being said, but, besides being able to say ā€œHelloā€, ā€œGood Morningā€, ā€œpleaseā€, ā€œthank youā€ and the basics, I would have difficulty having a full conversation in German without a translator/help. I simply need more practice and repetition and exposure to the C# language.

2 Likes

Privacy & Terms