//This is not a compilable example, just bits and pieces
class CPlayer
{
public:
CWeapon WeaponWielded;
CArmor ArmorWorn;
CShield ShieldEquipped;
void* Inventory[10]; // This is where I lose it
};
CPlayer::Createchar()
{
WeaponWielded = WeaponList[0];
Inventory[0] = (CWeapon) WeaponList[1];
//Also tried these
(CWeapon) player.Inventory[0] = WeaponList[1];
Inventory[0] = WeaponList[1];
*Inventory[0] = WeaponList[1];
&Inventory[0] = (CWeapon) WeaponList[1];
(CWeapon) &Inventory[0] = WeaponList[1];
}
It''s the last line that doesn''t work. I declared the Inventory member as a Void Pointer so I could have it point to either a Weapon, Armor, or Shield. My Typecast in Createchar() isn''t working however.
Any Suggestions on how to make this work? or am I going about it all wrong?
I also thought about making all Weapons, Armors, and Items derived from a base class called CItem, and then just making Inventory an array of CItems, but this caused problems with my code at another area that I didnt'' want to mess with.
Any suggestions would be much appreciated!
~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~
Question on using a Void Pointer. Will this work for me?
Hello Everyone!
I''ve stopped on my Tetris clone to do something that I enjoy more, a Console Arena style Combat game!
Here is my setup, and what I''m trying to do.
I have these classes...
CPlayer
CEnemy
CWeapon
CArmor
CShield
CPlayer has a CWeapon, CArmor, and CShield member that points to equipment in a master list.
I have a lists of different equipment such as WeaponList[5] = {{etc}}.
So if I want my player to use a weapon off the list, I would do.
CPlayer player;
player.WeaponWielded = WeaponList[0];
Now, I''m wanting to implement an Inventory Member in the CPlayer class that contains a number of Weapons, Armors, or Shields.
I tried it as follows, but it didn''t work.
WyrmSlayer RPG - In Early Development
I''d try the CItem base class, not void pointer. Though I''d recommend you finish Tetris first.
Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
quote: Original post by siaspete
I'd try the CItem base class, not void pointer. Though I'd recommend you finish Tetris first.
Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
After finishing Tic Tac Toe, I didn't want to work on another game that I didn't really like. I don't think I'll have a problem finishing the Arena game, then I'll try to finish Tetris. My real passion is in RPG's.
Anyway, the CItem base class was causing problems with my Item Lists. Maybe you can tell me how to fix that...
If I have the following code...
class CItem{public: char* name; int weight; int value;};class CWeapon : public CItem{public: int Damage; int Hitbonus; int dmgtype;};
Then how would I setup a list to initialize my weapons?
what order would the variables be in? Would it work like this?
CWeapon WeaponList[2] = {{"Shortsword", 3, 100, 6, 0, 1},{"Longsword", 5, 200, 8, 0, 1}}//This is assuming that the variable order on this class will go//Name, Weight, Value, Dmg, HitBonus, DmgType.////Is this a correct assumption?
Is this will work, then I can use the base class CItem for my Inventory.
~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~
edit - fixed the source tags
[edited by - Radagar on August 20, 2002 12:11:14 PM]
WyrmSlayer RPG - In Early Development
I wouldn''t count on that working. But since you''re doing it in code anyway, why not just have constructors that take all those parameters?
Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
What is it about computer programmer that 95% of them want to make a RPG ?
I feel out of place. I have no desire to make any RPG... sport gamse, action games are interesting to me....
and aobut the newbie teams.. they never work.. but I guess it is all part of learning
If you are just starting out then spend a few months making some sort of game from start to finish and THEN start a team..
I would start a team but I would only want one other person and I hate not being able to do what I want so that is why I go it alone
People start these huge teams and makes these pretty web sites and realize no one really knows what to do.
I feel out of place. I have no desire to make any RPG... sport gamse, action games are interesting to me....
and aobut the newbie teams.. they never work.. but I guess it is all part of learning
If you are just starting out then spend a few months making some sort of game from start to finish and THEN start a team..
I would start a team but I would only want one other person and I hate not being able to do what I want so that is why I go it alone
People start these huge teams and makes these pretty web sites and realize no one really knows what to do.
http://www.mattherb.com now with CATCAM!
quote: Original post by siaspete
I wouldn''t count on that working. But since you''re doing it in code anyway, why not just have constructors that take all those parameters?
Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
I''m not very good at using OOP and Classes yet, but I''m trying to learn. I understand what a constructor does, but I haven''t actually implemented one yet.
How would I use a constructor to define my CItems when they are created?
CWeapon::CWeapon(char* thename, int thedmg, int theweight, int thevalue){ name = thename; dmg = thedmg; weight = theweight; value = thevalue;}//Then in Main, assuming WeaponList is similar to aboveint main(){ Cplayer player; Weapon weapon(WeaponList[0]); player.weaponwielded = weapon;}
Will something like this work? I''m still new to all this... Is there even a need for the WeaponList array? If not, then how should I be doing this =)
WyrmSlayer RPG - In Early Development
Using your example you''d call it like this:
The idea is you put parameters in for it''s creation when you actually instantiate it.
Bear in mind you can actually make multiple constructors, so if you wanted to call it as you have (with the weaponlist[0] parameter) you''d have to make one like:
You''d probably prefer to use a few pointers (or better in C++, use references) in there to avoid copying everything in the parameter.
Hopefully that''ll help you out a bit.
-Mezz
Weapon myweapon("Shortsword", 3, 100, 6, 0, 1);
The idea is you put parameters in for it''s creation when you actually instantiate it.
Bear in mind you can actually make multiple constructors, so if you wanted to call it as you have (with the weaponlist[0] parameter) you''d have to make one like:
Weapon::Weapon(Weapon the_template){ name = the_template.name; dmg = the_template.dmg; weight = the_template.weight; value = the_template.value; }
You''d probably prefer to use a few pointers (or better in C++, use references) in there to avoid copying everything in the parameter.
Hopefully that''ll help you out a bit.
-Mezz
quote: Original post by Mezz
Using your example you''d call it like this:Weapon myweapon("Shortsword", 3, 100, 6, 0, 1);
The idea is you put parameters in for it''s creation when you actually instantiate it.
Bear in mind you can actually make multiple constructors, so if you wanted to call it as you have (with the weaponlist[0] parameter) you''d have to make one like:Weapon::Weapon(Weapon the_template){ name = the_template.name; dmg = the_template.dmg; weight = the_template.weight; value = the_template.value; }
You''d probably prefer to use a few pointers (or better in C++, use references) in there to avoid copying everything in the parameter.
Hopefully that''ll help you out a bit.
-Mezz
Mezz, you''ve come close to answering my problem, but let me explain again..
I DO want to have a list of Items that exist in my game, such as
WeaponList[MAXWEAPS] = {{"Shortsword", 5, 3 ,2},etc.}
I have a CWeapon class, a CArmor class, and a CShield class. Each has a List as above.
My CPlayer class has an instance of each of the above, that is assigned values from the Lists.
I want my CPlayer class to also have an Inventory Array, that contains a number of Weapons, Armors, and Shields.
I thought there would be 2 ways to do this...
Have the Inventory Array be an array of Void pointers to items on any of the lists, and typecasts those pointers to the correct type. I wasn''t able to get this to work.
My second Idea was to make CWeapon, CArmor, and CShield all derive from a base CItem class, then make the Inventory Array of type CItem. I''m assuming this would work, BUT...
If I do that, How do I setup my Master Lists as above to initialize variables from both the Base Class (CItem) and the derived class (CWeapon, CArmor, or CShield).
Any Suggestions?
WyrmSlayer RPG - In Early Development
Radagar, check out the online books Thinking In C++ and C++ In Action. You should be able to find them easily enough using Google.
They''re pretty good, and free!
Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
They''re pretty good, and free!
Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
quote: Original post by Radagar
Any Suggestions?
You have to store pointers to CItem in the array, rather than CItem. This introduces several new issues: CItem should have a virtual destructor to ensure correct polymorphic delete; any other methods which have to behave polymorphically should also be virtual; you have to "new" the objects into existence, and therefore have to remember to "delete" them when they're finished with. There are other issues too, but I guess you can ignore them for now. To help with the memory management issues, you should consider using container classes and smart pointers (a good set of smart pointers can be found at Boost's website. Here's some sample code which demonstrates these techniques working together:
#include <iostream>#include <vector>#include <boost/smart_ptr.hpp>class base{public: virtual void f() const =0; virtual ~base() {}};class derived1 : public base{public: void f() const { std::cout << "derived1::f()\n"; }};class derived2 : public base{public: void f() const { std::cout << "derived2::f()\n"; }};int main(){ typedef boost::shared_ptr<base> itemPtr; typedef std::vector<itemPtr> itemVect; itemVect items; items.push_back(itemPtr(new derived1)); items.push_back(itemPtr(new derived1)); items.push_back(itemPtr(new derived2)); items.push_back(itemPtr(new derived1)); items.push_back(itemPtr(new derived2)); itemVect::const_iterator it = items.begin(); for(; it != items.end(); ++it) { (*it)->f(); }}
It would be well worth your time learning the techniques shown in this code.
[edited by - SabreMan on August 20, 2002 3:49:36 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement