Constructors

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
TheLividePianoist
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 15
Joined: Thu Jan 29, 2009 5:48 pm

Constructors

Post by TheLividePianoist »

Hey, I was just wondering if there is an easier way to do something like this:

So what I want to do is have player classes (classes that the player can choose such as in a RPG. E.g. Archer, Warrior, etc.) and have the information for those player classes automatically be assigned to the player like take for example I create a class and call it Assault or Rifleman. This "Rifleman" has default weapons and attributes that are different from other classes.

Rifleman
Default Primary -> Standard Assault Rifle
Default Secondary -> Standard Handgun
Health = 100
Speed = 2.5 ; Where as an explosive specialist class would be slower or faster.

Take for example, to figure out what the player's current weapon or loadout is, I'd just call this in main.cpp:

Code: Select all

Loadout Assault(); // () anything in side the parenthesis are for the constructor which is apart of what I need help on. 
player.pLoadout.SetLoadout(Assault);
At first, before I introduced Loadouts into the game, I was just calling a SetWeapon() function in player.pPrimary.SetWeapon(StandardAssaultRifle) to set the weapon which could be changed when a gun is picked up. Now, I'd like to create a function which inside assigns the player's weapon based off the current Loadout. So, whatever the assault's default primary weapon is, that becomes the player's current primary. Or if the player changes classes, it automatically does it.

I'd like to know of easier ways of doing this. Like should I create derived classes of the Loadout Class and then assign the information in each or just have the instance of the Loadout Class and then assign all the information in a constructor?
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: Constructors

Post by K-Bal »

TheLividePianoist wrote: Like should I create derived classes of the Loadout Class and then assign the information in each or just have the instance of the Loadout Class and then assign all the information in a constructor?
I would create a Factory class that creates Player instances with specific settings, like CreateAssaultGuy or CreateSniperDude.

Code: Select all

Player* CreateSpecificPlayer1()
{
  Player* temp = new Player();
  temp->SetL33tness(1337);
  return temp;
}
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Constructors

Post by Ginto8 »

the different types of guns could just have different settings (fire rate, power, knockback, bullets per clip, etc.), and you could have a config file for all the types of guns, and it reads in the data, then sets up the gun choices that way. You could also hardcode those sets of settings, but that's your choice
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
wearymemory
Chaos Rift Junior
Chaos Rift Junior
Posts: 209
Joined: Thu Feb 12, 2009 8:46 pm

Re: Constructors

Post by wearymemory »

Ginto8 wrote:the different types of guns could just have different settings (fire rate, power, knockback, bullets per clip, etc.), and you could have a config file for all the types of guns, and it reads in the data, then sets up the gun choices that way. You could also hardcode those sets of settings, but that's sinful.
Typographical error.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Constructors

Post by Ginto8 »

wearymemory wrote:
Ginto8 wrote:the different types of guns could just have different settings (fire rate, power, knockback, bullets per clip, etc.), and you could have a config file for all the types of guns, and it reads in the data, then sets up the gun choices that way. You could also hardcode those sets of settings, but that's sinful.
Typographical error.
nope. I said hardcode the settings, not hardcode it as classes and the such... that would be sinful.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
wearymemory
Chaos Rift Junior
Chaos Rift Junior
Posts: 209
Joined: Thu Feb 12, 2009 8:46 pm

Re: Constructors

Post by wearymemory »

Ginto8 wrote:
wearymemory wrote:
Ginto8 wrote:the different types of guns could just have different settings (fire rate, power, knockback, bullets per clip, etc.), and you could have a config file for all the types of guns, and it reads in the data, then sets up the gun choices that way. You could also hardcode those sets of settings, but that's sinful.
Typographical error.
nope. I said hardcode the settings, not hardcode it as classes and the such... that would be sinful.
Hardcoding is an anti-pattern. Like Mrs. Twain says, it's "Gonna Getcha Good!"
Post Reply