Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ Programming ➜ General ➜ Combining Ruby and C++

Combining Ruby and C++

It is now over 60 days since the last post. This thread is closed.     Refresh page


Pages: 1  2 

Posted by Nick Cash   USA  (626 posts)  Bio
Date Reply #15 on Fri 04 Apr 2008 04:06 AM (UTC)

Amended on Fri 04 Apr 2008 05:08 AM (UTC) by Nick Cash

Message
I have read a great deal and I fully understand the difference spaces. I think perhaps I need to work on some small example programs to truly understand how it is all going to be put together.

However, based on my reading, there is a big problem. Calling ruby one way never gives control back to the C/C++ program. Calling it the other way (in which you call individual ruby functions it seems) would work ok, however, it seems it wouldn't remember the stack pointer or anything else in the script space between calls.

So, after viewing these methods, I think I'm stuck with threading. I've seen an example where the fellow setup a ruby thread, synchronized it, and periodically had it sleep so the main program could do things. It still seems like there should be a better way.

I guess what I'm envisioning is loading the C++ program, initializing the server and what not, start ruby and initial the rest of the mud (areas, etc), then periodically toss control from ruby over to the core C++ server to process sockets, then head back to ruby to handle the rest of the game. Initially I was thinking I would just attach scripts to certain objects on the c++ side so they could run after certain events, but giving the embeding options I would need to a lot of work to make it workable for NPCs or other in-game entities.

I don't like the idea of scraping what I have because it is pretty good code, but it seems like making a ruby server and merely extending it to handle sockets might be the way to go since database connections can be done easily in ruby as well.

~Nick Cash
http://www.nick-cash.com
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #16 on Fri 04 Apr 2008 05:35 PM (UTC)
Message
Quote:
However, based on my reading, there is a big problem. Calling ruby one way never gives control back to the C/C++ program.

Really? I'd be pretty surprised if it were impossible to pass control back and forth. In Lua and Perl -- the two languages I have experience embedding -- as soon as the function you called returns, control is restored to the C/C++ caller.

Calling the parser might leave control in the scripting language "forever" because it will start parsing standard input, and will only finish when standard input is closed.

Now, I don't have any experience embedding Ruby, but I would be surprised (and disappointed) if you had to use threading and synchronization to get this working.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Nick Gammon   Australia  (23,165 posts)  Bio   Forum Administrator
Date Reply #17 on Fri 04 Apr 2008 08:31 PM (UTC)
Message
Quote:

Calling ruby one way never gives control back to the C/C++ program. Calling it the other way (in which you call individual ruby functions it seems) would work ok, however, it seems it wouldn't remember the stack pointer or anything else in the script space between calls.


Honestly, if this is true, I would ditch Ruby.

Lua at least is designed to be embedded. A whole chapter in the "Learning Lua" book and most of the Reference Manual describes how to get data in/out of Lua.

Once you initially create the script space, you can call Lua functions in it as often as you want, and each time around it remembers how you "left things" from last time. For example, if you make a variable, it stays made.

You can even leave mid-function by using coroutines. By doing a coroutine.yield you are telling Lua that you have reached a logical point to return (to C++ or simply your calling function in Lua). Subsequent calls into that script space can resume that function, when appropriate.

This is how you can and would implement things like doing a login dialog ("what is your name?", "what is your password?" and so on).

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Cash   USA  (626 posts)  Bio
Date Reply #18 on Fri 04 Apr 2008 08:55 PM (UTC)
Message
It is plainly known to ruby programmers (and is written right in the pickaxe) that ruby was not originally designed with embedding in mind, so its embedding interface is seriously hackish and unhelpful. Its extending interface is much better it seeems.

I will look into switching to Lua. There is no point wasting my time trying to embed Ruby when it could be done far easier in another language. Although I like Ruby, it seems it won't suit this purpose as much as I thought it would.

Thanks for the feed back. :)

~Nick Cash
http://www.nick-cash.com
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #19 on Fri 04 Apr 2008 09:01 PM (UTC)
Message
You know, I'm in the process of learning Ruby, and it seems that many of the concepts are very similar. Ruby functions that take blocks, like the 'each' function, are rather like coroutine iterators (the Lua manual explains these). Ruby duck-typing can be achieved by testing if an object field is nil. (This works except in extreme cases of meta-tabling.) Lua doesn't have the direct support for classes that Ruby has, but using metatables you can easily accomplish much of the same functionality: with a little work, you can use metatables to completely replicate a full class system with inheritance etc.

The point of this being that if you know and understand Ruby, it should be fairly easy to adapt to Lua. At least, that's what I'm finding as I go in the other direction. :-)

The book Programming in Lua is excellent for getting an introduction to Lua:
http://www.lua.org/pil/

It's a superb book, I highly recommend it. And it's available online for free (although I have hard-copies of both versions; they're nice to have and I like supporting the development effort even in a small way).

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Nick Gammon   Australia  (23,165 posts)  Bio   Forum Administrator
Date Reply #20 on Fri 04 Apr 2008 09:34 PM (UTC)
Message
This post shows you to embed Lua in your own application:

http://www.gammon.com.au/forum/?id=6400

It shows how in a few lines of code you can be executing a Lua script. And in a few more you can read in a Lua script file and execute it.

Meanwhile this post shows how to add extra functions to the Lua script space (that is, functions written in C):

http://www.gammon.com.au/forum/?id=4915

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


83,425 views.

This is page 2, subject is 2 pages long:  [Previous page]  1  2 

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.