Question: Dynamic code execution

Hi

I’m currently writing a small game application in ruby.
The problem is, not all of the rules are static, meaning some
of the actions in the game are allowed to change them.
My question is, what is the best and safest way to implement this
feature?
Specifically, I have database of items, which may change parts of
the rules upon activation.
Storing code along with those and eval()ing it at runtime feels
kind of awkward and not really safe, but seems the most flexible
way at a first glance.
Has anybody ideas and/or pointers to this matter?

Thorsten

Thorsten H. wrote:

Hi

I’m currently writing a small game application in ruby.
The problem is, not all of the rules are static, meaning some
of the actions in the game are allowed to change them.
My question is, what is the best and safest way to implement this
feature?
Specifically, I have database of items, which may change parts of
the rules upon activation.

Alright… So store the rules in one or more hashes depending on what
they are and how they can change, and refer to whatever key you need to
read the rule (or setting) in the hash.
That’s one way.

My problem is, how to formulate the rules in ruby?
Let me give an example:
Lets say in an RPG context, the max skill level is 10,
but if the player chooses to be an elf, he gets 11 as a
max skill level in bow shooting. Additionally there
exists something like a special feature which increases
the max skill level by another point.
At the moment I’m thinking about a mini DSL based on
method_missing, but I’m looking for alternatives.

Thorsten H. wrote:

My problem is, how to formulate the rules in ruby?
Let me give an example:
Lets say in an RPG context, the max skill level is 10,
but if the player chooses to be an elf, he gets 11 as a
max skill level in bow shooting. Additionally there
exists something like a special feature which increases
the max skill level by another point.
At the moment I’m thinking about a mini DSL based on
method_missing, but I’m looking for alternatives.

Things which will be crucial to you here are going to be extending
modules and classes, as well as inheritance.

class Race
MAX_SKILL_LEVEL = 10
end

class Elf < Race
MAX_SKILL_LEVEL += 1
end

Race::MAX # => 10
Elf::MAX # => 11

And you can also specify unique skill levels (for instance, your
Swordmanship max skill may be 22 because of a unique sword you have, it
doesn’t raise EVERY skill max…)

Does that help a little?

On Mon, 29 Mar 2010, Thorsten H. wrote:

kind of awkward and not really safe, but seems the most flexible
way at a first glance.
Has anybody ideas and/or pointers to this matter?

Thorsten

You might consider looking at this, which talks about some dynamic game
generation I’ve done (and if you have further questions, I’ve got more):

http://aetherical.com/2008/4/25/classes-on-the-fly

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs