Public Group

# Scripting vs Self-documenting code

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

## Recommended Posts

I'm making a CRPG and I'm having trouble translating textual script files into actual "virtual machine" instructions. Here we go! :-D I have a CPerson class that has CAttribute members for each attribute I want to track (strength, magick, agility, etc.) It also contains CSkill members that measure skill levels (sword, thief, black magick, whatever). The effectiveness of an ability is based on the governing attribute(s) and governing skill(s). I store the effects of a particular ability in a script; ah-like so:
Ability : Steal
Skill : Thievery
Attribute : Agility
Coefficient : 2.5 (used to balance gameplay)

Accuracy = Skill * Skill * Attribute * Coefficient

I want this to translate into C++ as something like:
Action.Steal.accuracy =
Person->Skills->Thievery *
Person->Skills->Thievery *
Person->Attributes->Agility *
Action.Steal.Coefficient

Now, I could parse through a script and send it through a sprawling god-awful switch, but I'd like to streamline the approach to make it more readable / maintainable in case I have to update a skill that's way too (in)effective. Can anyone share some experience in scripting? Am I going about this the right way?

##### Share on other sites
Quote:
 Original post by templewulfAbility : StealSkill : ThieveryAttribute : AgilityCoefficient : 2.5 (used to balance gameplay)Accuracy = Skill * Skill * Attribute * CoefficientI want this to translate into C++ as something like:Action.Steal.accuracy = Person->Skills->Thievery * Person->Skills->Thievery * Person->Attributes->Agility * Action.Steal.CoefficientNow, I could parse through a script and send it through a sprawling god-awful switch, but I'd like to streamline the approach to make it more readable / maintainable in case I have to update a skill that's way too (in)effective.

You can't really do this directly in C++ without doing it as some kind of compile-time process - make a compiler for your script file that translates it into C++ source, for example, and then include that file in the game compilation. The problem is that attribute "names" like 'Skills' don't exist at runtime (just like variable names); in the source code, they're just symbols that let the coder know what corresponds to which, and in compiled code, they just become labels for / offsets into chunks of memory.

One option is to use a real, pre-existing scripting language for doing the scripting stuff, and embed it into your C++ app. (Conversely, you could write the program in a "scripting language" like Python, figure out where the bottlenecks are, and reimplement those in C++.) This requires a fair amount of setup work, though, to interface between code in the two languages.

Another option, that I'd recommend here, is to make the names exist at runtime, by building an "associative array" (also called a map, dictionary, or possibly by some other names, depending on who's talking). Some languages provide this as a built-in type. C++ provides a templated library class, std::map, which does the job. What this thing is, is basically a mapping from "keys" of some type to "values" of some (possibly but not necessarily the same) type. This is basically implemented by making structures that contain a key-value pair, and then organizing them in some way that makes it easy to find a structure given a key. (The std::map does this with a variety of binary searching tree.)

Anyway - it's a fair bit of work to do yourself, but the standard library class makes things quite convenient. All we need is a key type that can be sorted (compared for equality and order) - std::string will do the job, and is also appropriate since we want textual "keys" - and any sort of value type. Then for example we can do:

struct Action {  std::string name;  std::string skill;  std::string attribute;  int coefficient;};std::map<std::string, Action> Actions;struct Player {  // key is skill name; value is the player's level of skill  std::map<std::string, int> Skills;  std::map<std::string, int> Attributes;}// Replace this code with code that parses the script and creates the Action// appropriatelyAction a = { "Steal", "Thievery", "Agility", 2.5 };Actions["Steal"] = a;// Later on, we can do something like:Action desired = Actions[getInput()];// Determine the chance of success.Player* Person = getPlayer();// The Action object tells us the names of the skill/attribute to look up,// and then we check in the Player's maps for the int values.int successRate = Person->Skills[desired.skill] *                   Person->Skills[desired.skill] *                  Person->Attributes[desired.attribute] *                  desired.coefficient;

Actually representing the *formula* in a script is *considerably* harder, and would be a good reason to go with the "embed a real scripting language" solution.

##### Share on other sites
I'm not sure at what you're aiming at here, but to me it would seem that you can have this functionality with a hash table.

##### Share on other sites
This is what Lua was designed for.

##### Share on other sites
Thanks for all the help, guys. Sorry I haven't been back in a few days, but you know how it gets when you have girl troubles.

I wouldn't mind taking the time to integrate python, because I'm just doing this as a learning exercise anyway. Plus, python is getting to be so widespread that it'd be a good thing to pick up anyway.

As to Lua, I've heard that it leaves a smaller footprint and runs a bit faster for most functions you're likely to use in games, but I think I'd better go with python just because I hear it's getting popular. (I hear that's what google uses for its search engine.)

Anyway, what are some better articles for integrating python with c++ in a game engine?

P.S. What do most professionals use to script things in 2d/3d CRPGs?

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 13
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633321
• Total Posts
3011351
• ### Who's Online (See full list)

There are no registered users currently online

×