Charas-Project

  • Home
  • Help
  • Search
  • Calendar
  • Login
  • Register
*
Please login or register.

Login with username, password and session length
 

News:

Click here to join us on IRC (#charas on irc.freenode.net)!



  • Charas-Project »
  • Off-Topic »
  • Archive »
  • Really Old Stuff »
  • General programming »
  • C++ loops and switches
« previous next »
  • Print
Pages: [1]

Author Topic: C++ loops and switches  (Read 5534 times)

Offline Prpl_Mage

  • Administrator
  • Sage
  • *
  • Posts: 7,645
  • The Administrator Mage
    • Check out our itch website
C++ loops and switches
« on: October 12, 2008, 07:13:52 PM »
Ok, this have been bothering me for quite some time now.

Is there a way to make loops like in rpgm2k3?
Up till today I've been using while commands (It's pretty much a common event with parralel prcess in rpgm2k3) and it works, but sometimes not.
It's kinda annoying and I'm just wondering if anyone knows if there is a better way?

What about the switches?
Yeah, same here. I'm using variables at the moment. Simply making them equal to a number and then have the if command react if the varaible is that specific number.

And any help on easier ways to do random numbers would be welcomed.

Edit: Anyone who could give me some tips on how to take care of all the If/If else/else branches( {, & }, ) in a better and or smarter ways have my deepest gratitude.
Logged
Cool RPGM Project!
Sprite till you die

Oh my god, this was ...10 years ago...

Offline coasterkrazy

  • June 2005 - September 2008... January 2011?
  • Exemplar
  • *
  • Posts: 1,915
  • Hello...
Re: C++ loops and switches
« Reply #1 on: October 12, 2008, 11:14:04 PM »
Quote from: Prpl_Mage on October 12, 2008, 07:13:52 PM
What about the switches?
Yeah, same here. I'm using variables at the moment. Simply making them equal to a number and then have the if command react if the varaible is that specific number.

Shouldn't switches just be booleans (true/false)? I believe it works like a normal variable, but rather than numbers, it can either be true or false. Then again, I only know Java, but I would assume a language like C++ has booleans. They've been around way before C++.
Logged

Offline Prpl_Mage

  • Administrator
  • Sage
  • *
  • Posts: 7,645
  • The Administrator Mage
    • Check out our itch website
Re: C++ loops and switches
« Reply #2 on: October 13, 2008, 07:44:13 PM »
I havn't read anything about booleans actually. But I've realised that varaibles works pretty well as switches.
Kinda like, you pick up an item: "itempot" increases by 1.
In battle, you use the potion: itempot decreases by 1.
If "itempot" is less than/or equal to 0, cout << "You don't have any potions left!" << endl;

So yeah, I'll look into the booleans might just find it usefull.

As for loops, I figured that I just need to cut down on the {and }, Somewhere along my events, they got messed up.

But yeah, any other advice is appreciated.
Logged
Cool RPGM Project!
Sprite till you die

Oh my god, this was ...10 years ago...

Offline Osmose

  • So freakin' inactive
  • Royal
  • *
  • Posts: 3,041
Re: C++ loops and switches
« Reply #3 on: October 13, 2008, 09:05:25 PM »
For starters, stop thinking of C++ in terms of RPG Maker (If anything it should be the other way around :P).

I don't quite understand what you're having a problem with, so I'll just go over the two concepts and then see what you're having trouble with. :P

DISCLAIMER: I make no guarantee that any of this is truly valid C++ syntax, but I'm pretty sure it should work. If you try any of this sample code and it doesn't work (Beyond where I put stuff like "put code here" and you just leave it as is instead of putting some real code there) then ask and I'll test/clarify.

Loops

There are two common forms of loops in programming: For and While Loops. A while loop will execute a block of code as long as a certain condition is met. In C++ I believe the syntax in C++ is

Code: [Select]
while (some condition) {
  some executable code;
}

What this code does is first check the condition (Which is usually something along the lines of "a > b"). If that condition is true, it executes the block of code and then re-checks the condition. If the condition is false, it stops executing the block and moves to the next instruction (The block itself is all of the code in between { and }).

A similar form of this is a Do While loop. It's syntax should be

Code: [Select]
do {
  some executable code;
} while (some condition)

This code is very similar to the code above. The key difference is that the block of code will run at least once before the condition is checked. With the while loop above, the condition is checked first, which means the block of code may never even be run, but a do while loop is always run at least once.

The third loop is known as a for loop and is really just a convinient way of writing a specialized while loop. It's syntax is generally as follows

Code: [Select]
for (int k = 0; k < someNumber; k++) {
  some executable code;
}

It looks a bit complex, but it's really simple. The statement in the parenthesis is actually three special statements: an initial value, a conditional statement, and an incrementor. The Initial Value statement is evaluated once at the beginning of the loop, and is usually used to set an initial value for a counter. The Conditional Statement is evaluated much like the condition in a while loop: it is checked once per loop and stops the loop when it evaluates to be false; The Incrementor is run once at the end of each loop before the condition is checked again, and is generally used to increment a counter.

In the example above, the Initial Value just creates a variable named "a" and sets it equal to 0.

The Conditional Statement checks if a is less than 5. Once a is greater than or equal to 5, the loops stops running.

The incrementor is run once per loop (After the block of code). a++ is shorthand for adding 1 to a. It's the same as saying a = a + 1. So each time the loop is run, a increases by 1. In this way you can control the loop and cause it to be run 5 times.

As you can see, a for loop can repeat a block of code a specific amount of times very easily, and is best used when you know how many times you want to run a block of code.

Boolean Values

Variables in most programming languages have to have a certain type which distinguishes what data they are supposed to store, such as an integer (Non-decimal number) or a String (Text). Types are distinguished between each other based on what they hold and how much they can hold. For example, an integer and long both hold non-decimal numbers, but a long can hold much larger numbers than an integer.

A Boolean variable is another type of variable that only has two possible values: true and false. They are either one or the other, and usually default to being false if not set. All conditional statements (a < b, c == d, etc) evaluate to a boolean value of either true or false, making the following code valid:

Code: [Select]
bool booleanVariable = (a < b);

If a is really less than b, the value of booleanVariable will be true, otherwise it will be false.
« Last Edit: October 13, 2008, 09:24:54 PM by Osmose »
Logged
Hrm.

Offline Prpl_Mage

  • Administrator
  • Sage
  • *
  • Posts: 7,645
  • The Administrator Mage
    • Check out our itch website
Re: C++ loops and switches
« Reply #4 on: October 16, 2008, 03:55:45 PM »
Allright, the project me and my friend are working on have finally come to the point where my classmates insists in the use of graphics rather than my awesome dragons made of:
 ____
/  O  \
vvvv
^^^
\____

Anyway, we were gonna try using flash. But then we noticed that Flash uses Java and not c++.
Did we screw up utterly or is there any not-so-very-complicated ways to make it happen?
I found some directx tutorial but I'm just wondering if there's some easy way like importing or so.
« Last Edit: October 16, 2008, 05:42:26 PM by Prpl_Mage »
Logged
Cool RPGM Project!
Sprite till you die

Oh my god, this was ...10 years ago...

Offline Osmose

  • So freakin' inactive
  • Royal
  • *
  • Posts: 3,041
Re: C++ loops and switches
« Reply #5 on: October 19, 2008, 07:57:05 AM »
...Flash uses Java? What? No!

Flash movies are scripted using Actionscript. The hell did you read they use Java?

Graphics work is... well, let's just say you're not going to be able to move that fast for your project.
Logged
Hrm.

Offline Gesser

  • Initiate
  • *
  • Posts: 4
Re: C++ loops and switches
« Reply #6 on: November 27, 2008, 03:41:02 AM »
I assume you want to write in C++... C++ has graphics librarys a very nice free one is allegro (www.allegro.cc) I use it to write my game engines.  The code will take you a little bit the graphics will take you forever to find years upon years if your doing it for fun... Funny thing about your dragon it reminds me about my first RPG and the code you ware trying to use is similar as well... If you are going to use C++ keep in mind you can use classes and learn to use them they help alot especially when you start writing a scripting language or using another one thats on the net somewhere... While flash is fun to play with C++ has more versatility overall in terms of applications and nicer looking games.  Depending on how much work you are wanting to put into this project of yours you don't want to hit a wall with flash and then need to rewrite in C++.
Logged

Offline fruckert

  • Star-Star-Star-Star
  • Sage
  • *
  • Posts: 8,148
  • Not intended for public consumption
Re: C++ loops and switches
« Reply #7 on: November 27, 2008, 03:50:35 AM »
Fairly old topic, man
Look at the "posted on" thing
Logged
Quote
Ellie: I had a slice of ham in my hand. I was going to drop it, so I slapped it hard. It attached itself to the wall

  • Print
Pages: [1]
« previous next »
  • Charas-Project »
  • Off-Topic »
  • Archive »
  • Really Old Stuff »
  • General programming »
  • C++ loops and switches
 

  • SMF 2.0.10 | SMF © 2015, Simple Machines
  • XHTML
  • 2O11
  • RSS
  • WAP2
  • Simple Machines Forum