Question: Dynamic code execution

T

Thorsten Hater

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
 
A

Aldric Giacomoni

Thorsten said:
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.
 
T

Thorsten Hater

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.
 
A

Aldric Giacomoni

Thorsten said:
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?
 
M

Matthew K. Williams

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top