I realize these topics may be incredibly huge but I figure that since I really don't know where to start, I figure I'll start somewhere and just keep going
Any input, suggested reading, links, etc. would be greatly appreciated.
Posted 10 September 2011 - 08:21 PM
Posted 10 September 2011 - 08:35 PM
Posted 10 September 2011 - 08:48 PM
I'm slowly getting my head wrapped around programming and I was wondering if there are any "best practices" out there that I should be following.
I know the idea is to be "object oriented" but I am a little fuzzy as to how to apply/organize that. If a game has weapons, are each of those weapons in separate classes (or inherited classes from a "master weapon blueprint"?)
Are the behaviors of these weapons governed by methods/functions?
Are games organized in such a way that objects are "seen" (IE have access) throughout the game code via references or pointers more easily?
Posted 10 September 2011 - 09:34 PM
Posted 10 September 2011 - 11:18 PM
Posted 10 September 2011 - 11:53 PM
Yea, I know everything doesn't need to be global. But I'm noticing as I am creating a simple text-based RPG (as an exercise), I am needing to "see" various classes within my code and am having a tough time doing it sometimes.
So I gave each weapon its own struct with its own stats already initialized when the code was compiled. I wasn't satisfied with that either.
Posted 11 September 2011 - 12:51 AM
// the code might look like Java, sorry <img src='http://public.gamedev.net/public/style_emoticons/<#EMO_DIR#>/tongue.gif' class='bbc_emoticon' alt=':P' />
void shootPlayersGun(){
if(reloadSeconds == 0 && (gun == PISTOL || gun == SHOTGUN || gun == SMG)){
if(gun == PISTOL){
reloadSeconds = 0.5;
createBullet();
bullets--;
} else if(gun == SMG)
reloadSeconds = 0.1
createBullet();
bullets--;
} else if(gun == SHOTGUN)
reloadSeconds = 1;
createBullet();
createBullet();
createBullet();
shells--;
}
} else if(heatPercent < 100 && (gun == BEAMWEAPON || gun == FLAMETHROWER)){
heatPercent++;
if(gun == BEAMWEAPON){
createBeam();
} else if (gun == FLAMETHROWER){
createFlame();
fuel--;
}
}
}player.weapon.fire()Now you can put all your weapon code in different weapon classes that implement the weapon interface and you don't have to wonder what code is associated with you beam weapons and so on. You don't have as many if's so your code is less complicated. To create a new weapon you just create a new weapon class and put your code there. At this point you can also start using inheritance, composition and other OOP stuff to start reducing code duplication too. You might not have less lines of code but this less duplication means smaller chances of bugs and when you fix a bug in one place it gets fixed everywhere.
Posted 11 September 2011 - 02:29 AM
It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.
Posted 11 September 2011 - 02:30 AM
Posted 11 September 2011 - 04:59 AM
Posted 11 September 2011 - 04:40 PM
So I gave each weapon its own struct with its own stats already initialized when the code was compiled. I wasn't satisfied with that either.
Why weren't you satisfied with this? All weapons are going to share a set of stats, so make a Weapon object (class or struct, whatever is appropriate for the language). If a particular weapon type never changes its state (stats) during the game, then you will not need a separate instance for each weapon - just keep a master list of weapon types and their stats, and index into it as needed. If weapons can change state during the game (their stats can change, such as current ammo, upgrades, wear and tear), then each weapon will have its own instance. In this case, the master list of weapon types is like a collection of blueprints, and is used to initialize a weapon instance.
struct Weapon
{
string mName;
Range mRange;
}Posted 11 September 2011 - 04:56 PM
No, I would say that's excellent OO design. Your Range class, for example, should have some functions that operate on it (constructor, copy operators, min(), max(), what have you). that's good encapsulation. Your weapon: it's good encapsulation.Here is a bit of code for how I set up the weapon struct:
struct Weapon { string mName; Range mRange; }
The range links to a Range.h file that has "mLow" and "mHigh" variables that determine the weapon's damage. Like I said, this code worked fine when you selected your class and were given the default weapon and the values for that weapon were set. But like I said, I was unsure how to implement this when I needed multiple instances of them in a player's inventory. This could easily be a case of me not being able to see the forest because of the trees and I am missing an obvious solution right in front of me. And perhaps I am trying to be too much of a slave to OOP (as suggested by one poster). This is great stuff and I appreciate all the replies!
Posted 11 September 2011 - 07:15 PM
Weapon weapon; // create weapon object vector<weapon> inventoryWeapons; // create vector array for inventory that holds weapons inventoryWeapons[0] = // not sure what goes here...how can this hold the weapon name and the range?
weapon.mName = "Sword"; weapon.mRange.mLow = 4; weapon.MRange.mHigh = 8;
Posted 11 September 2011 - 07:52 PM
struct Range
{
Range(int low, in high) : mLow(low), mHigh(high) {}
int mLow;
int mHigh;
};
Let's also suppose your Weapon struct looked like this.
struct Weapon
{
Weapon(const std::string& name, int min, int max) : mName(name), mRange(min, max) { }
std::string mName;
Range mRange;
};
Finally, I'll assume you have a Player class that looks like this, eliding stuff like cup size and drinking prowess. I don't know what kind of RPG you like, I'll keep mine a secret too.
struct Player
{
std::string mName;
std::vector<Weapon> mWeapons;
};
Somewhere in you code, you're going to do something like this.
Player player;
// Create some weapons
Weapon intruder("dagger", 1, 4);
Weapon venusButterfly("butterfly", 1, 8);
// Equip the player.
player.mWeapons.push_back(intruder);
player.mWeapons.push_back(venusButterfly);
The code marked Create some weapons creates two weapon objects -- instances of the Weapon struct. The Equip the player lines then add copies of those objects to the players mWeapons member variable. Viola, your player will have his hands busy.Posted 12 September 2011 - 07:32 AM
You can make the inventory as a class , and give it some stats (Item count , Weight , and an Array of numbers)Like I said, I'm not sure how this would go into an array. Especially if there are a handful of other weapons such as daggers and axes. Do each of these different weapons need to be initialized within their own struct or can their values be set up elsewhere? I tried this way as well but again, I didn't know how to use them in arrays. That really seems to be my hangup right there.