0
\$\begingroup\$

I am making a collectible card game using a command pattern for using card abilities and creating a class for each card text ability. The game is setup to work with an authoritative server structure. I have each cards stat values in a MySQL database. My question is what would be the best way to associate a cards text with the class that executes it?

For example, I have a Card class that pulls card stats from the database for the specific card. I would like to have an entry in the database for each card that determines what ability class the card has. Since, I'm limited to the basic data types in the database I'm trying to find the best way to associate a string or int to a class that executes the cards ability in my code.

My first solution is having each database entry for a card hold a string that holds the code for the class, and compiling that string on the fly. A second solution is having a giant switch statement class that associates each card name with the class of its ability. I feel like there should be a simpler way to get this accomplished though. Anyone have any insights? Thanks

\$\endgroup\$
  • \$\begingroup\$ Could you add a couple of examples please? Right now it is rather unclear what you have and what you want to achieve. \$\endgroup\$ – Kromster says support Monica Nov 14 '14 at 7:20
  • \$\begingroup\$ Have you considered using the Factory pattern to instantiate a Card? \$\endgroup\$ – Scott Downey Nov 14 '14 at 15:06
  • \$\begingroup\$ Compiling the code on-the-fly sounds like a bad idea. 1. it will slow down card loading, 2. you need to handle compiler errors somehow. When you do this, at least implement the card effects with a scripting language. \$\endgroup\$ – Philipp Nov 14 '14 at 16:14
  • \$\begingroup\$ A unique ID should be used for each card. This comes built into SQL as a data type. \$\endgroup\$ – Shroeder Nov 14 '14 at 16:52
1
\$\begingroup\$

Two methods that come to mind:

Rather than a big switch statement, which would be slow, you can use a Dictionary of some kind (probably Trie-based) to associate the string names to the classes for your card behaviors.

Another way that would be more automatic, but possibly slower, would be to use Type.GetType(String classname)

If you give this the full classname in the namespace of your application, you'll get the class specified by the string. That would let you just put the classnames in your database and associate them to card classes without defining the names->classes association beforehand.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.