View Single Post
Old 03-13-2004, 11:33 AM   #3
Yui Unifex
Senior Member
 
Join Date: Apr 2002
Location: Florida
Posts: 323
Yui Unifex is on a distinguished road
Send a message via ICQ to Yui Unifex Send a message via AIM to Yui Unifex
Question

My public domain codebase, Aetas, has embedded Ruby for scripting. It performs admirably for every purpose you list.

Embedding Ruby is not too hard. I'll respond to your requirements in turn with how I implemented it in Ruby.
This is more a function of your codebase than the language you are embedding. My objects keep a list of 'Triggers' that fire upon some event being emitted. The trigger simply contains the text of a Ruby script.

This, also, is more a function of your codebase then the language you use. I use a property list for pretty much all accessible values, e.g., player->get("name") will return the string name of the player, and player->set("hp", "10") will set their hitpoints to 10. It's very easy to wrap these properties in whatever scripting language you choose. I simply provide a 'rubyval' function that converts the value into a Ruby VALUE by using rb_str_new for strings, or INT2FIX for numbers.

I do not have variables that are global to the world; but if I did, I would be using the same system of properties. I'd merely implement them on the global 'Game' class instead of a specific object.

Every object that I use for scripting has a Ruby class defined for it. You can use Data_Wrap_Struct to wrap a pointer into a Ruby class so that 'self' refers to it. In this way you write your Ruby pretty much the same way that you write your C++. For example:
[code] class Element
...
def cmdSay (args)
return if args.empty?

msg = "<#{name}> #{args}\n"
room.broadcast(msg)
room.sendevent(EVENT_TEXT, self, args, args)
end
...[/quote]
Since the 'say' command executes within the context of 'Element' (my toplevel entity), the 'room' in this method refers to the room in which the Element resides. So it functions rather naturally.

Although this sorta feels like an advertisement, implements exactly what you're asking, so feel free to check it out and post any questions you might have.
Yui Unifex is offline   Reply With Quote