K
K
Konstantin Kitmanov2011-08-03 23:51:00
JavaScript
Konstantin Kitmanov, 2011-08-03 23:51:00

How to store the behavior of non-standard game objects?

Suppose there is some RPG. Most of the weapons have standard behavior and are fully described by the parameters (for example: Rusty Ax of Dullness - damage +5, intelligence -5). All this is perfectly stored in a relational database.

But some of the weapons have some behavior - for example, they work well against this particular boss, or they weigh more in the light, or the damage depends on the character's class. In general, some behavior that, for good, can only be described by a function.

So, where to store this function? In general, there are two options: the first is to make a repository of behaviors and get functions from there by ID. But this will spread the content of the game between the code and the database, it will be more difficult to ensure integrity. The second is to store the function in the database and eval it - but this is ugly, eval is evil, storing code in the database is ugly and all that.

Apparently, it is necessary to store in the database, but not in the main language, but in some kind of DSL - but I'm unlikely to be able to write a DSL.

Technologies - JS and SQLite. Neither one nor the other can be changed according to the conditions of the task :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
TheHorse, 2011-08-04
@k12th

As I understand it, there is some kind of server script that dumps js to the client? In this case, you can store the code in the database, and without eval-s, it can be added to the server response. But in any case, this is not an option.
You must store parameters, values, constants, meta-information in the database, you cannot store logic in the database, this is an error (in this case).
I propose a solution:
1. Make another table that stores all possible influence factors.
2. Describe all possible influence factors somewhere in JS, the function should return the level of influence by the ID / name of the influence factor, depending on the current parameters (weather, impact target, time of the last impact, or something else).
3. For each spec. object in the table add a field containing the string '["id1":"k1","id2":"k2"...]' - a list of influence parameters and their coefficients. Or not extra. field, and additional table (depending on the table normalization level).
Thus, you can store special parameters in the database, and change the main parameters of items / player dynamically using JS. If the number of special factors - dynamic and can change during the game process - DSL or templates - random admixture of different factors of influence.

C
codesnik, 2011-08-04
@codesnik

MUDs, for the most part, store the code for special object behaviors in the database (another thing is that the database is usually textual, and the code is really DSL, not C / C ++). Common behavior - parametrized in flags ("5% chance of poisoning on impact"), special - prescribed in proc and "on_hit, 3% { echo "You heard a terrible crunch"}, saved along with object classes in the same " DB“
This, like, has been working for more than a dozen years.
but Javascript is, one might say, its own DSL. so you don't even have to think about it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question