Ruby scripts

Hello-

How do I access the Ruby interpreter in Tribes Next? I can't seem to find a way to get Tribes to run ruby scripts. Perhaps I misunderstood the point of the Ruby interpreter? It would be extremely useful and powerful if mods could use it.

Thanks,
Particle

Comments

  • It looks like he made a eval function to do this. For example,
     rubyEval("puts 2+2");
    
    should give you a response of 4. All script in Torque is injected directly into the engine line by line so if it wasn't typed correctly it give a sentax error. So if you made a .cs file that executed when the game started and was full of ruby eval's you could get it to work I think!
  • edited March 2009
    It looks like he made a eval function to do this. For example,
     rubyEval("puts 2+2");
    
    should give you a response of 4. All script in Torque is injected directly into the engine line by line so if it wasn't typed correctly it give a sentax error. So if you made a .cs file that executed when the game started and was full of ruby eval's you could get it to work I think!
    function rubyexec(%file)
    {
    if (!IsFile(%file))
    return "No such file.";
    
    %object = new fileobject(rubyexec);
    %object.openforread(%file);
    
    while (!%object.iseof())
    {
    rubyeval(%object.readline());
    }
    %object.close();
    %object.delete();
    }
    

    That's how I'd do it, but the Ruby Interpreter may have came with a ruby file compiler of some sort for T2. But if not, that would be better if it was written in Ruby.

    Edit:

    Yes, there's already a ruby file reader built in:

    rubyexec(%file);
  • edited March 2009
    You need to use the "tsEval" Ruby function that Thyth made if you want the game to be able to use anything from Ruby.
    [tt]tsEval('echo("lolhi");')[/tt]
    As you might expect, it runs a console command from Ruby, so you could do something like:
    $temp = "";
    rubyEval("tsEval '$temp=' + (2+2).to_s + ';'");
    echo($temp);
    
    And it should echo "4" to the console.

    Keep in mind, if you make a syntax error in your Ruby code, it will tell you nothing - it just won't work. It was pretty annoying trying to learn Ruby using the TN interpreter with no error handling at all. :(
  • The interpreter interface is full of caveats in RC1, but I can briefly explain how to use it:
    There is a rubyExec("path/to/file.rb") function that is used to load Ruby scripts. It loads both crypto.rb (my RSA, secure random number generator, and SHA1 library) and cerstore.rb (for reading/writing the public.store and private.store files) for the purposes of TN authentication.

    You'll want to be careful if you're using rubyExec, because there is a hacky workaround in RC1 for the interface. There is no newline support in the that version of the interface, so Ruby script lines have comments (crudely) stripped, then all lines are concatenated with semicolon delineation. The comment stripping has the disadvantage of killing any Ruby double-quoted statements that use the #{blah} variable print form. The RC2 interface supports newline transfers.

    Kryand mentioned the lack of error handling, which is somewhat addressed in RC2 as well (it prints a generic runtime-error if there is a problem), but in any case, I strongly recommend developing any Ruby scripts using a stand-alone interpreter.

    If you're using threads, you should know that any calls to print or tsEval in background threads will not be reflected in the game. While I haven't tested this myself, those calls will either vanish into the abyss, never to be seen again, or will all be executed in the TS interpreter on the next rubyEval call. The Torque Script interpreter is single threaded for script execution, so there is no clean way to allow use of tsEval or print (which is implemented using a script call) from a separate thread. This behavior won't change in RC2.

    Finally, the tsEval call does not interleave TS execution with Ruby execution, rather the calls to tsEval are queued for execution at the completion of the call to rubyEval. As a result, I recommend not issuing direct calls to rubyEval from tsEval; scheduled calls should be OK. The tsEval call is really only intended for use when you need to return the result of a computation. This behavior also won't change in RC2.
  • Does our interpreter come with the default gems you'd get with a standalone Ruby install?

    For instance, I'd like to:
    require 'win32ole' but I don't think it's working.

    Thanks everyone for the information provided so far. I think this is the most comprehensive resource of Ruby in TN already. :)
  • No, there aren't any ruby-gems installed...nor will some of them work (net/http for example), as there are a few components missing from the ruby build.
  • It's pretty much just a raw embedded interpreter. Unfortunately, documents regarding how to properly embed Ruby in another application are really sparse (and mostly out of date), so I don't plan on extending it much further than what it is now.
Sign In or Register to comment.