Jump to content
  • Advertisement
Sign in to follow this  
Jompe71

Performance help needed on "scripting" rules to code logical statement

This topic is 3156 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

To keep the story short I have a "scripting" text adventure game engine set up for a remake soon to be released. Thus I'm struggeling with a performance issue (or at least a potential one). Below is an example of a line the engine has to interpretate. "RULES": [ "$MASK!", "$STONE", "@TOMB_SMASHED! #HOLE #STICK" ], At initial state this is converted only into a plain String array, pseudo: String[] rules = { "$MASK!", "$STONE", "@TOMB_SMASHED! #HOLE #STICK" }; Now at runtime these rules have to be checked if they all apply at the current state. A rule has an initial identifier ($ = item, @ = event, # = object) and a state ( for items '!' = must wear, for events '!' = must NOT have happended etc. ) So what I need either in code snippets or pseudo or in just plain therory is a fast way of converting these rules into logical boolean factors. example: String[] rules = { "$MASK!", "$STONE", "@TOMB_SMASHED! #HOLE #STICK" }; as. { true, false, true } At this stage I do (not a lot, but minimum required) String manipulation to query the identifier for each rule and the potential state. After that I look up (in hashed maps) either the item, object or event and see if the rule apply. How can this be more efficantly done ?? (Please respond if the english sucks to much or if I can't make myself clear in the matter....) Do I have to say it's urgent ?? :-)

Share this post


Link to post
Share on other sites
Advertisement
None of these steps seem like they would be a big performance hit. Why not profile and see if it's a real problem?

What you can do, however, is build up a map of cached results so that once a rule's value is found, you don't have to look it up again. A simple map of strings to bools would work here, hash_map/unordered_map preferred for speed.

You can also benefit from 'early-out' behaviour, if you aren't already. If the rules are in conjunctive form (ie. each part has to be true for the action to fire) then you only have to check each part if all the previous ones were true. Rearranging your rules so that the clauses that are most likely to be false are at the front will reduce the number of checks you have to do.

If you really need to go further than that, you can factor out common rule clauses and introduce dependencies between them. This allows you to potentially reject multiple rules with single tests, but requires you to have some way of finding these common clauses and creating the dependency structure to relate them.

Share this post


Link to post
Share on other sites
Thanks for your input!

I think I covered you suggestions pretty much, i.e. The rules as of items, object and events are cached in runtime regarding their current state and the overall "check-if-all-rules-apply" code abort early whenever a rule is dissobeyed.
Thus rules are not sorted in anyway according to which is most likelly to happen I'd be honest to say...

As stated my explanation is not to check the state of the item/oblect/evant it's the rumtime code to check WHAT needs to be checked. My engine currently loads/unloads location at runtime to keep a minimal footprint. Note I'm coding for mobile devices as well as for Bluray and inmemory IS an issue.

Therefore each location/generic command rules are interpreted in runtime so to try to explain it better I need a fast way to convert the rules array into booleans to match againts it's matching state from the cache.

Example in pseudo (match):
"$BOOT!" to cached object Boot<Item>.isWearing

It's the fastes way to determine from a String what Object/Item/Event I need to match and what current state I need to match it against.

Hope this shead some lights into the matter...

Share this post


Link to post
Share on other sites
If you create all the objects up front, you could convert at load time from those strings containing object names and symbols to class instances that contain object pointers and other variables, along with a function to query the state. You'll probably want a base class containing just that query function too.

That means once the data is loaded you don't need to do any lookups to find objects, or any string manipulation.


class BaseQuery
{
public:
virtual bool Query() = 0;
};

class ObjectQuery : public BaseQuery
{
public:
explicit ObjectQuery(String s);

virtual bool Query();

private:
Object *m_ThingToBeTested;
bool m_NegateTest;
};

// TODO: Create similar classes for events and items

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!