Sunday 28 August 2016

If else...OR ELSE!!!



I'm sorry if the title is a little aggressive. I don't know what came over me.

Had a little bit of time yesterday so I did another lecture on my Unity course. This one focused on if and else if statements. I have worked with these before when doing the Codecademy tutorials for JavaScript, but I was a bit rusty so it was a good refresher.

In fact I came across if and else if when working on Pet Me! the other day, writing probably my most complicated piece of code so far. I wanted to create a puzzle where the cats won't come to you until you have wiggled a wiggly stick (a cat toy, this is what I call them...) three times. Each time you wiggle it the cats respond in a different way. I had managed to find a script which specified that the first time you wiggled the stick a certain thing would happen, and then any wiggle after would make a different thing happen.

But I wanted the player to wiggle the stick three times before the cats would be pet-able. So this didn't work for me because the second and third wiggles produced the same outcome.

In my own very cack-handed style I thought I could circumvent this by adding in an if statement. So I already had code that said the second time the stick was wiggled that the cats would come out of their hiding place and therefore be visible. Then I told the game that if the cats were visible and the stick was wiggled then the cats should be pet-able.

I thought I was being all clever with my if statement.

But it didn't work! (This happens a lot!)

The way it was written, the game was resolving the if statement at the same time as the instruction before it because it became true. So as soon as I wiggled the stick the second time, the game printed the text for the second and third wiggles.

Pah.

So I must admit I got a little bit of help on this one, as my husband happened to come home at that moment. "What you need," he said, "is an else if statement".

Now I had seen if and else if statements and I hadn't understood the difference between them. On the face of it they seemed to do the same thing (in Quest at least) so I wasn't sure why this would make the difference.

So imagine I had a key which would unlock a door with one turn, and then open it with another turn. A simple (but incorrect) example of what I was trying to do would be:


if the door is locked and I use the key then unlock the door and print "The door is unlocked"
if the door is unlocked and I use the key then open the door and print "The door is opened"

If I ran this statement then both sets of text would print because as soon as the door is unlocked it would read the second line and carry out the command. This isn't what I want, because I wanted it to take two turns of the key to open the door.

So in this case an else if statement would be just the ticket, because then it would only carry out one of the two options.

1. if the door is locked and I use the key then unlock the door and print "The door is unlocked"
else
2. if the door is unlocked and I use the key then open the door and print "The door is opened"

This would result in either the first or the second statement to be printed, depending on whether the key had already been used on the door or not.

Else if statements solve the problem of having a series of if statements in a row, where more than one of them can resolve at the same time, and you want one thing OR another thing but never both or all.
Right! So I started to use else if statements instead. But else if what? I needed some kind of way of telling the game that the stick had been wiggled 1, 2 or 3 times. Again some husbandly help was useful here as he asked if there was an object counter function.

I found that and added it, so now I could base my else if statement on whether the stick has been wiggled 1, 2 or 3 times.

Basically -

If stick wiggled =0 then print text 1
elseif stick wiggled =1 then print text 2 and make cats visible
elseif stick wiggled =2 then print text 3 and make cats pet-able
elseif stick wiggled ≥3 then print text 3

Rather than what I had before which was-

if player wiggles the stick for the first time print text 1
if it is not the first time the player has wiggled the stick then print text 2 and make the cats visible
if the cats are visible then print statement 3 and make them pet-able straightaway

So the beauty of else if is that it checks each statement starting at the top and working its way thorough until it finds one that matches the number of times wiggled, carries out the instruction, and then waits for further player input before it checks them all again. This avoids the issue I had where the game would resolve each instruction because the if statements were both true.

The last thing I had to implement was the object counter to increase after each pass, otherwise without it the game would still not know if the stick had been wiggled, 1, 2, 3 or 1,000000000 times.


Here it is!


In Number Wizard, my else if statement was actually a lot simpler, but nonetheless as necessary. You may remember that I was at the point where the game prompted the player to press the up arrow or down arrow to state whether the number they had thought of was higher or lower than 500. As it was written, you could accidentally press both up and down at the same time and it would resolve both. The else if statement here was used to make sure that only the very first key pressed would be resolved, and it would not log the second key unless it was pressed at the start of a new frame.

if, else if


I realise that maybe this would all be a lot easier to explain if I had more screen shots to show the dodgy code as it was. I didn't think to screenshot them at the time, and there is no way I'm going to change it back, and then back again just for a screengrab! So I promise I will screen grab as I go along in future!

I hope your brain is intact after all that.

Bye for now!

Sunday 21 August 2016

Pet Me!


Pet Me!


I wanted to introduce you to a project that I started a little while ago, called Pet Me!
It’s a text based adventure game where you are trapped in a house, and you have to pet enough cats to escape. But the cats are not that easy to catch! You have to solve puzzles before they will come to you.

The beginning! This is what the game looks like when you play it.



I am using a website called textadventures.co.uk and a program hosted there called Quest to write this. The website does the complicated work, so you don’t strictly write code while using it, but you do have to understand which commands to use to create the effect you want. I chose this because I wanted to start with something simpler, where I can practise the art of creating puzzles and a little bit of the logic behind coding.

Are you crazy?!?



But I’m kind of pleased to say that it is actually a little more complex than I thought. In doing this I am devising the puzzles, writing the text, programming the game, sourcing assets, bug testing and fixing and project managing myself. One thing I am definitely learning here is that making a game on your own is a very difficult task, and it is clear why the majority of games are made by specialists in teams. Even for my very small and rough game, I am having to develop a huge range of skills

My creativity is being used in many different ways. Sometimes I'm feeling literary, so I will do some writing, sometimes I feel impatient and want to make progress so I do some of the programming, and sometimes I am feeling particularly focused and so I do some of the bug fixing. This is probably the hardest part. It is very east to just plough on when you get stuck and move on to some new part that hasn't become infuriating yet.


Under the hood...how to make a cat purr.


Sometimes I find Quest to be a little rigid in what it can do, the functions that are provided within it will almost do exactly what I want, but not quite. This is probably because of my inexperience. I often find that when I encounter a tricky problem that I need to step right back and start again, because the solution I have gone with is not the right one, and never will be! It is like pushing a square peg into a round hole and thinking that it you rotate it it will make a difference.

So another thing I am learning is that you have to be very patient and not to give up.

I am about 7-10 hours away from making the first version playable, with the win condition being that you have to pet 12 cats. Unfortunately at the moment I can't get the win condition to actually trigger 'winning', so about 5 of those hours will be spent on stepping back from that, and then stepping back in again.

At the moment I have this...but it doesn't work. Maybe you guys have some ideas!


Why. won't. you. just. work.???
Bye for now!

Friday 19 August 2016

Unity - the beginnings




I started an online course in Unity a while back, but as most of the first 10 lectures were about opening the program and saving folders into the right place I got a bit bored and didn't really make much progress.

This is exactly what my students would be like when we would have to start any lessons learning to video edit by painfully explaining what format and resolution the project would need to be, plus where exactly to save footage and where to save the project. Now I realise how dull it was, but to an editing geek it was the the foundations of a zen-like pathway to editing enlightenment and DEATHLY VITAL!!! There is nothing worse than a student losing their work, except for the constipated face of their teacher who had told them EXACTLY what to do at the very start of the project, was inevitably ignored and is currently holding in Vesuvius behind fake 'it's OK we can TRY to fix it' eyes.

But that is all behind me now and those memories only wake me maybe once a week.

Anyway this constant cycle of vital instruction, failure to follow said instruction, and scramble to pick up the pieces has probably left me in good stead for meticulously following the course instructions and actually remembering to do everything right by myself in the future.

So I have learnt that you mustn't move files/folders around unless you have exited Unity (and it's IDE, Monodevelop) first. Good. I won't.

Once all that was out of the way I actually began to use Unity, and on looks alone, it looks a lot like Premiere or Final Cut (the good ones, before X) which I am happily familiar with. So maybe I CAN make a game.

My first game is...


In which the computer guesses the number you are thinking of through mystical means!!!! Ooooohhhh!

Well no, mystical means would be pretty hard to code, so actually it will choose half of the range and ask you if your number lies there. Then continue in the same fashion until you finally say 'yes! that is my bleeping number, how did you guess that...?'.

So far I have learned the print function and how to write a string (or technically revisited these concepts as I have learned a little coding before through Codecademy and Khan Academy - maybe I can tell you a little about this another time). I am just starting to learn how to get a key press as an input and the difference between start and update commands so currently my code looks like this:

33 lines of joy

I hope you find that lovely too.

The course teaches you how to look up things that you don't know, rather than just telling you exactly what you have to write, so I already know how to google the exact term for something (which I know is something that some professional developers will approve of!)

And it works...so far.

Hello!



Hi there!

I want to make video games! So I'm gonna bloody do it. Oh yes I am.

And I'm gonna put snippets of my progress on here so that I can see what I have achieved, and you can too.
I want you (YES YOU) to help me out...encourage me, guide me, laugh with me, cry with me, and most of all enjoy this with me.

Mandoid. Developer.