RBScript questions


A couple of questions about RBScript/XScript:

1- I am having a problem with RBScript (which I am just exploring for the first time) and would be grateful if anyone could look at my code and tell me what I am doing wrong:

I have an instance of RBScript (RBscript1) on a window (window1), a canvas (canvas1), a property of window1 (myPic as picture) and two pushbuttons. The two pushbuttons just call a method that draws something on a Picture which is then used in the paint event of canvas1. One of the pushbuttons calls the drawing method directly and it works correctly and the other method calls it using RBscript, and it fails to work.

The method that is called by the pushbuttons is:

Sub myDrawRect(PassedPic as Picture) PassedPic.Graphics.DrawRect (10,10,50,50) End Sub

The pushbutton that calls the method directly has this in the action event:

Sub Action() myPic=new Picture(Canvas1.width, Canvas1.height,32) myDrawRect(myPic) Canvas1.Invalidate End Sub

The pushbutton that uses RBScript and fails to do what I expect uses this:

Sub Action() myPic=new Picture(Canvas1.width, Canvas1.height,32) RbScript1.Source="myDrawRect(myPic)" RbScript1.Context=Window1 RbScript1.Run Canvas1.Invalidate End Sub

For this second case I am getting (RBScript) compiler error 11 (Undefined identifier) but I don’t know what it means. If myDrawRect is modified like below it works correctly:

Sub myDrawRect() myPic.Graphics.DrawRect (10,10,50,50) End Sub
Which makes me think that the problem is related to the scope/context of RBScript1, but I don’t understand what the problem is.

2- I am exploring RBScript/Xscript because of the performance improvement since it uses LLVM, but since in the above example I am using a “regular” method outside the RBScript (myDrawRect), I imagine I will not get any increase in speed at execution, or will I?

Thank you,



RB/XojoScript doesn’t support Picture objects; to manipulate a Picture you must write special methods in the script’s Context that can manipulate a Picture stored outside of the script, but not actually referencing the picture.

You might find this useful. A while ago I was playing with RBScript and wrote a scripted canvas project. You can grab the project here. It demonstrates handling the Paint event of a Canvas using a script.

Thank you Andrew, I get it now.

What about my secod question, any ideas?

Thank you again.


I’ve never used RBScript for a performance boost, so I can’t speak from experience. However, it seems to me that it would only be worthwhile for long and computationally expensive operations. Maybe someone else can give a more specific/useful answer :\

This is what I am after, ultimately. I have some code that takes very long to execute right now, and I want to make it even (much) more involved, and execute it in several processes/threads in parallel. Speeding up the execution would be very useful.

By the way, thank you for your code above.


I can’t imagine that XojoScript would be faster for that, though I’ve never tried to run multiple scripts at the same time. I would think they would still live within the same cooperative thread of the main app. To get true parallel processing (i.e. take advantage of the multiple cores in your machine), you’d need to spin off the task into several console apps that your app controls. I believe Paul posted something on the Xojo blog about doing just that very recently.

You might also post a question here about your task that seems slow; often times a little optimization can make a dramatic difference and people here are great at making suggestions (there was a conversation a couple of weeks about loading large files that was made much faster with code provided here).

Thank you for your comments Marc.

This is what I am planning to do. What I would like to know is whether it is worth using XScript as well, in order to speed things up.

I did this some time ago in the old forums, and indeed got great replies from several people: http://forums.realsoftware.com/viewtopic.php?f=1&t=42755


The question there is whether scripts running concurrently each run within their own process like console apps, or if they siphon off the CPU of the main app. I suspect the latter, but I can’t say definitively. The simple thing would be to do a test and see if doing a task with 2+ scripts is faster than doing it with one.

Sorry, I was not clear before. I am going to use several console application running in parallel as you mentioned. My question is whether using RBscript will speed up the execution further.


Ah, that’s interesting Pixie! So each of your console apps will be running their own XojoScript? That should work, but I’m still unsure how much faster it will be. It would be a good test. Let us know what you find out.

[quote=27976:@Pixe inXojo]Sorry, I was not clear before. I am going to use several console application running in parallel as you mentioned. My question is whether using RBscript will speed up the execution further.


It depends how much of your code can run in the XojoScript JIT. If your code is extremely math heavy and you can minimize the number of calls to to the context object from your script, you’ll see a good improvement.

Thanks Joe, I think my code is a good candidate for the perfomance boost then.

I will try do a test it next week, and show the results here.