Page 1 of 1

Constructors

Posted: Tue Jul 13, 2010 5:06 pm
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?

Re: Constructors

Posted: Tue Jul 13, 2010 5:20 pm
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;
}

Re: Constructors

Posted: Tue Jul 13, 2010 7:50 pm
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

Re: Constructors

Posted: Tue Jul 13, 2010 10:55 pm
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.

Re: Constructors

Posted: Tue Jul 13, 2010 11:33 pm
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.

Re: Constructors

Posted: Wed Jul 14, 2010 10:30 am
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!"