Jump to content
  • Advertisement
Sign in to follow this  
furin121

simple RPG spell question

This topic is 4250 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, here's the situation. I'm creating a simple text based RPG. Naturally several classes will have the ability to cast spells and each class will have unique spells with the exception of a few utility spells that can be used by multiple classes. My question is, what's the best way to store all possible spells? Right now I'm having to create a new spell object for each spell I want to use when I create the character or hit the appropriate level. Is this the way it's normally done? Seems to me like there should be a way to create all of the spells up front and grant access to them at the specific time. I'm going to be using the same technique for custom skills for each class. If I'm having to create a new object of each spell and / or skill for each character (pc and npc) in the game wont that eat up a lot of resources without good reason? Anyway, short version is this: what's a good way to handle spells / skills in a simple text based rpg? Thanks

Share this post


Link to post
Share on other sites
Advertisement
you could create a class for the player and put all your spell functions / objects in there. If you have a variable which determines which class your character is, then in your spell functions you can use if statments to determine if you can cast the spell or not...

example


class PLAYER {
public:
char PlayerClass[10];
//spells
bool spell();
};

bool PLAYER::spell() {
if (PlayerClass == "mage" || PlayerClass == "otherClass") {
//do spell
return TRUE; //spell succesful
}
else {
return FALSE; //spell can't be cast
}
}


hope this helps

Share this post


Link to post
Share on other sites
Now I don't know if this is the good or best way to do this but here are some ideas.

You could create all the spells upfront not related to any characters, and each character could hold indexes or whatever into the spells. You could also have each character hold spell modifers so that each level gained tweaks the spells base stats and just the modifers can be stored with each character. This would help with spells being reused alot.

simple example:
CorrectSpell = base_spells[FIRE_BALL] * (DAMAGE_MODIFER * CHARACTER_LEVEL)

You could also create a spell factory that when requested would either give you a new spell based on what you asked for or hand you a handle to an already created spell if it was created previously.

simple example:
CorrectSpell = SpellFactory.GetSpell(FIRE_BALL_RANK10)

Like I said don't know if those ideas are good, but just throwing something out there for you to think about.

Share this post


Link to post
Share on other sites
I would do it the way you are doing it -- each spell type is it's own class. Each player would have a spell handler (a SpellBook or something) that can have spells added to it.

So the object relationship topology would be: Player --(one to many)--> SpellBook --(one to many)--> spells.

This just means, each player has one (or more) SpellBooks, and each SpellBook has multiple spells. Doing it this way, you could potentially have multiple spell books (Dark Magic, Light Magic, General Magic whatever).

You said it's a text-based rpg, so if you require the user to type in the name of the spell they wish to cast, the SpellBook could store the spells in a string hash, for quick queries.

Share this post


Link to post
Share on other sites
I'm using the scripting language Lua for all of my game logic. It doesn't natively support OOP concepts, but I put together a script that lets me create classes and implement inheritence. The important thing to remember is that in Lua, I can change the number / type of members of any object, regardless of the type it was originally created from.

I have one script file dedicated to magic, and I create all magic spells there. Then, once I load the main game, I have the interpreter execute that file, creating spell objects in memory. From there, I just store available spells as an array of strings in the character object. You could change this to numbered IDs, but that's harder to debug and I don't feel like it. [lol]

I don't know if this is the best way, but it's the most flexible system I've been able to figure out.

Share this post


Link to post
Share on other sites
Personally I think scripting the spells would be much easier than a seperate class for each spell. In that sense, you wouldn't even have to have a seperate class for skills.

Something like...

enum target{
SELF = 1,
ALLY = 2,
ENEMY = 4,
PARTY = 11, //bit 1 for self, bit 2 for ally, bit 8 to designate them all
ALL_ENEMY = 12, //bit 4 to designate enemies, 8 to designate them all
};

enum classes{
FIGHTER = 1,
MAGE = 2,
ROGUE = 4,
CLERIC = 8,
//...etc.
};

class cSpell{
int id; //if you want to reference it by id
std::string name;
std::string desc;

int min_lvl;
int target;
int damage;
long class_restriction; //use boolean or/and checks on this to figure if a class can use the spell.
//ie: 1=fighter, 2=mage, 4=rogue, 8=cleric, etc. I believe the check would
//just be if(class_restriction & incoming_class) someone will probably correct me on this.

//any other collection of data to customize the options available

//a number of functions, including one to test the lvl and class restrictions
bool objHasAccess(int lvl, long classx);
}



Load all these from a file into an array of cSpell objects, and a quick check through the array using the characters level and class will tell you what spells/skills they can use.

Just a general idea, would need some refining to work well.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!