Monday, October 13, 2008

Scratch

Sorry its been a while, but I've been playing around with a nifty little tool called "Scratch." scratch.mit.edu Yip, you read that right, MIT developed it to introduce people to programming ideas. Though it is a bit limited, it is still pretty awesome. So check out SCRATCH

Learn more about this project

Monday, September 29, 2008

Switch Statements

So its been a while since my last blog entry. Sorry about that, as usually happens, life sucks. So we move on.

Last time we talked about If Statements and nested If Statements. As you could tell, nesting if's can get really confusing. Switch statements can help a lot with that.

Here is the syntax for a Switch statment:

switch (selection variable)
{
case label1: statement1;
case label2: statement2;
case labelx: statementx;
default: default statement;
}

The selection variable may be either a number, a string, or a variable that is equal to either a number or string. The switch statement then selects the correct case where the label = the selection variable. If no case is found that matches, then the default is used.

Here is an example

switch (%spellnumber)
{
case 1: %spellname = "Fire Ball";
case 2: %spellname = "Poison Cloud";
case 3: %spellname = "Sleep";
case default: %spellname = "Magic Missle";
}

The equivalent if statement would look like this:

if %spellnumber == 1
%spellname = "Fire Ball";
elseif %spellnumber == 2
%spellname = "Poison Cloud";
elseif %spellnumber == 3
%spellname = "Sleep";
else
%spellname = "Magic Missle";

As you can see, for ever 1 line of a switch statement, the if statement requires 2. It might not seem that bad with only 4 selections to choose from, but imagine if there were 50, or 100! And remember, we are looking to make a game here. The more choices for the player the more likely the are to keep playing.

Saturday, September 13, 2008

Why this blog? (Sorta off topic)

So I was recently asked: "Why are you making this blog? Are you going to make money at it?"

Well no, I do not plan on making money at it. I don't even know if anything will ever come of this blog. So far nobody has gone out of their way to comment on it beyond the above question. But that is ok, it isn't really for them, this blog is for me.

So, anyways, here is the "Why" behind this blog:

Once, I had a dream. In that dream the Navy would teach me all about computers and technology. Yeah, that didn't happen. Then I thought I would go to college to learn about computers (Dot Com crash, anyone?). So yeah, that didn't happen either.

Every few years it seems I would make a good start on getting my programming up to speed, but then something would happen that would make me lose heart. Or I would get to busy. Or the kids would need me. Or my software would be out of date.

Too many excuses. Not enough reasons. We've all been there.

So here I am. Writing this blog. I've got the tools to program (finally). I've been building the knowledge for years. I just need to apply it and get it to stick in my head. I once read that the best way to learn a topic yourself is to teach it to someone else.

And so: This Blog!

This blog is about forgotten dreams, lost years, and hopes that are still alive. It is about making me listen to myself. "Damn the torpedoes, full speed ahead!" comes to mind. Or maybe "STFU if you don't wanna read my blog, you don't have to."

But yeah, not gonna make any money on it. Never planned to, never hope to. But thats ok, just writing the blog is its own reward.

And besides: if someone else learns something from it, bonus!

Tuesday, September 2, 2008

Things to remember

While working on determining the fate of Wally and Ralph I have stumbled on a few things that are important to remember. I've gone over them in the last week, but I figure it doesn't hurt to share them again (plus it will help me remember):

Arrays:

$char1[1] is a valid array, $char1(1) is not. (notice [] vs. () )
Return statements that do not pass a value do not need ()
Pay attention to boolean arguements || means OR in torque script OR means nothing

Work on Wally and Ralph continues. I have learned a lot and am working on making the script more efficient. While it runs almost instantaneously the way it is, you can imagine how much it would slow TGE down if it were being run for 1000 characters instead of just 2.

Friday, August 29, 2008

Thinking Ahead: Design

The last few days have really put a lot of information forward. We know how to handle a lot of different variable: text, number, arrays, local, and global. We know how to pass them to and from different functions. We even know how to use them to determine what the program should do next.
But we still do not know enough to make a game. We really won't for a while yet, but I thought an exercise in what we have learned so far might be a little fun. So that is what we are going to do. We are going to SIMULATE a game in the console window.
First though, we need an idea. That is where thinking ahead comes in. If we plot out enough information into our "Design Document" it will make the programming a lot easier. Soooo...here it goes:
============================================================================
Game Title: Arena Duel
Synopsis: Wally the Warrior and Ralph the Rogue have been friends since they first met at the melee academy. Unfortunately, at the graduation ceremony they both realized that they were in love with the same girl. As part of graduation they must pick an opponent from their class and fight to the death. The winner graduates, and in the case of Wally and Ralph, gets the girl.
Game Play: In this case both Wally and Ralph will be played by the computer. The computer will attempt to make the most of the attack moves available to Wally and Ralph to determine the winner. No maneuvering is allowed in the arena, so movement is not an option.
Output: Text will describe the location, health, and status of each character. It will also describe combat as it progresses until ultimately only the winner remains.
Character Info:
Wally the Warrior
Health: 120
Armor: 100
Dodge: .1
Parry: .1
Attack: 5
Special 1: Shield Stun (Stuns for 3 rounds, 10 round cooldown)
Special 2: Healing Potion (Heals 25 health single use)

Ralph the Rogue
Health: 80
Armor: 30
Dodge: .3
Parry: .2
Attack: 8
Special 1: Bleeding Gash (Target takes 2 dmg per round for 5 rounds. Prevent Healing. 10 round cool down)
Special 2: Healing Potion (Heals 25 health single use)


Order of Events:

=1.) Initialize all variables
= a. Wally
= b. Ralph
= c. Variables for combat processing
=2.) Initiative
= a. Check status. Does it prevent from attacking?
= b. Roll random 0-1
= i. If 0, wally attacks first this round
= ii. If 1 ralph attacks first this round
= c. Call function for first attacker
= d. Call function for second attacker
=3.) Combat
= a. Wally attacks
= i. Is Wally below 60 health?
= 1. No, continue attack
= 2. Yes
= a. Is Wally bleeding?
= i. Yes, continue attack
= ii. No, use potion
= ii. Check cooldown on Shield Stun
= 1. If on cool down, regular attack
= 2. If not on cool down, use stun
= iii. Check to see if Ralph is still alive
= 1. If yes, continue
= 2. If no, declare wally the winner
= b. Ralph attacks
= i. Is Ralph stunned
= 1. No, continue attack
= 2. Yes, pass attack turn
= ii. Is Ralph below 40 health?
= 1. Yes, use potion
= 2. No, continue attack
= iii. Check cooldown on Bleeding Gash
= 1. If on cool down, regular attack
= 2. If not on cooldown, us BG
= iv. Is Wally still alive?
= 1. Yes, continue
= 2. No, declare Ralph the winner
=4.) End Round
= a. Is wally bleeding?
= i. Update character status
= b. Is Ralph stunned?
= i. Update character status
= c. Special ability cooldowns
= i. Decrease shield stun countdown by 1 (if it isn’t zero)
= ii. Decrease bg countdown by 1 (if it isn’t zero)


There we go! Now that is a pretty decent little design document for our little skillset showcase! (please forgive the = signs, it was the only way I could get it to save my formatting in Blogger.)

Tomorrow we will move forward with Arena Duel! Lets see if a few little variables, functions, and if statements can determine who will win the girl!

Thursday, August 28, 2008

Conditional Expressions and if-else statements

One of the cool things about games is that the computer makes decisions based on what it happening in the game. Maybe it is a cooldown timer on a super powerful ability, or maybe it is determining if that NPC should cast a heal spell on itself. How does the computer know to do these things? The programmer used conditional expressions.

Basically a conditional expression is a formula that either produces a True (1) result, or a False (0) result. Torque has a full toolset of conditional operators that you can use to make decisions in your programming:

< Less Than
> Greater Than
<= Less Than or Equal To
>= Greater Than or Equal To
== Equal To (since = is used for assigning variable values)
!= Not Equal To
$= String Equal To
!$= String Not Equal To

Additionally, you can tie two or more conditional expressions together to product a True or False result pertaining to BOTH using:

&& AND (both expressions must be TRUE)
|| OR (either expression must be TRUE)

Lastly, There is an operator that gives you the opposite result (True becomes False and vice versa):

! NOT

So, now we have our ability to tell whether an expression is True or False. But what good is it? Well, you could use it to assign a 1 or 0 value to a variable and then use that variable in a computation. OR, you could use it to set up branches in your program using the if-else statement.

So, what is a branch? Basically you are telling the computer to do one of two things. Its kind of like deciding what you want to do for dinner. Do you have enough money to order pizza? If yes, get on the phone! If not, start making that sandwich.

Translated to if-else, this decision would look like:

if (%money > 20)
{
%dinner = "Pizza!"
}
Else
{
%dinner = "Sandwich."
}

Another cool thing about if-else statements is that you can "nest" them. You can have one inside the other. You put the most limiting expression in the first "if" statement and work your way down to the least limiting. You can also have an else statement for each additional "if".

Using nested if-else statements, we can greatly expand our dinner choices:

if (%money > 10)
{
if (%money > 15)
{
if (%money > 20)
{
%dinner = "Pizza!"
}
else
{
%dinner = "Burger!"
}
else
{
%dinner = "Sandwich."
}

Though this is a little hard to read, it is actually very efficient. If %money < 10, then the program just straight to $dinner = "Sandwich". The rest of the if-else structure is only run as needed. Only if %money > 20 does the whole thing run, and even then the "else" statements are skipped completely!

Wednesday, August 27, 2008

Loopity Loops (or Round and Round We go)

Ok, sometimes actions will need to be repeated a certain number of times or if certain conditions exist. Torque gives us the While loop and the For loop to deal with times like these.

NOTE: do NOT capitalize while and for in your code. Just don't. Trust me.

The While loop is used if we want something to occur while a specific condition exists (hence the 'While' loop). Here is the syntax for a While loop:

while (condition)
{
Commands
}

Your never want to get stuck in a loop. If you do, your program will basically stop responding. You should always ensure that a combination of the condition and the commands gives the program a way to get out of the loop.

Same While Loop:

//wailingWhile.cs
//
function oucheffect ()
{
%playerHP = 1; //gives initial player health
$upHPs = 0; //Initialize %upHPs
while (%playerHP <30) //set the condition for the loop
{
echo ("OMG it still hurts!"); //it is called WAILINGwhile afterall
$upHPs = GetRandom(30); //assigns a random number between 0 and 30 to %healPts
%playerHP = %playerHP + $upHPs;//increase %playerHP
echo ("Player healed for "@$upHPts@". Player health now "@%playerHP@".");//gives us some output to see if the function is working
}
}

Go ahead and try that in the Torque console to see what happens.

The next type of loop is the For loop. Basically this is for when you want an action to occur a specific number of times.

For Loop syntax:

for (initial value; state check; update value)
{
commands
}

Here is a simple module using a for loop to print a specific amount of random numbers:

//rndfor.cs
//
function countem ()
{
for (%counter = 1; %counter <= 10; %counter++)
{
Echo(GetRandom(10));
}
}

Give that one a try as well. Always remember: when coding, for and while are not capitalized. It will generate a parser error that will have you looking for the mistake for hours. If you don't know 100% that it should be uppercase, use lower. In my experience so far it is safer.