[groovy-user] Invoking Groovy functions from Java, but allowing them access to global variables and import statements.

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[groovy-user] Invoking Groovy functions from Java, but allowing them access to global variables and import statements.

Andrew Crowell
Hello, I am somewhat new to Groovy. I'm making a game engine, where
your scripts are divided into to sections, map scripts, and system
scripts.

System scripts are to be always executed, and will setup the core
routines used by the game. The scripts have the ability to access the
native Java methods and engine stuffs. The system scripts are pretty
easy to do, since they can be run line-by-line.

Map scripts specify events, which are to be enclosed in methods, and
they are to have access to any of the variables and methods bound by
the system script, as well as methods built into the actual engine.
When the map is switched, all events and stuff related to the current
map is to be discarded.

It these map scripts I am unsure about. I tried using the classloader
deal that is in the documentation, in order to invoke a method of the
class. However, this seems to shrug off and ignore any other
declarations within the file, outside of that method. All import
statements, as well as variables and other functions that are declared
in that file seem to not be found, and as a result, it gives runtime
errors.

I can, however, invoke closures just fine, using binding.getVariable()
and then calling the closure object. This isn't ideal though, when I'm
sure closures are only meant for events like iterations and such. If
Groovy functions could be gotten in a similar manner, it would be
awesome stuffs.

There's probably something I'm missing. However, I can't seem to
locate this in the documentation. Any help would be appreciated.
Thanks.

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: [groovy-user] Invoking Groovy functions from Java, but allowing them access to global variables and import statements.

tugwilson

On 3 Jun 2006, at 18:59, Andrew Crowell wrote:

> I can, however, invoke closures just fine, using binding.getVariable()
> and then calling the closure object. This isn't ideal though, when I'm
> sure closures are only meant for events like iterations and such.


No Closures are meant to be far more widely used. If fact they are  
extensively used to handle Swing and SWT events.

If Closures work for you then use closures.



John Wilson
The Wilson Partnership
web http://www.wilson.co.uk
blog http://eek.ook.org



---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: [groovy-user] Invoking Groovy functions from Java, but allowing them access to global variables and import statements.

Jochen Theodorou
In reply to this post by Andrew Crowell
Andrew Crowell schrieb:

first of all: it is funny, I thought about creating something like this
last week ;)

[...]
> It these map scripts I am unsure about. I tried using the classloader
> deal that is in the documentation, in order to invoke a method of the
> class.

can you give example code on how you do this? It sounds more complicated
than needed.

> However, this seems to shrug off and ignore any other
> declarations within the file, outside of that method. All import
> statements, as well as variables and other functions that are declared
> in that file seem to not be found, and as a result, it gives runtime
> errors.

and this sounds strange.... you should post a simple example script and
the way you load it as well as the way you execute it.

bye blackdrag

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply | Threaded
Open this post in threaded view
|

Re: [groovy-user] Invoking Groovy functions from Java, but allowing them access to global variables and import statements.

lars thuring
In reply to this post by Andrew Crowell
Andrew Crowell wrote:

...

> It these map scripts I am unsure about. I tried using the classloader
> deal that is in the documentation, in order to invoke a method of the
> class. However, this seems to shrug off and ignore any other
> declarations within the file, outside of that method. All import
> statements, as well as variables and other functions that are declared
> in that file seem to not be found, and as a result, it gives runtime
> errors.

I am also using groovy for a scripting in a game and have implemented the above
using a "glue-code" java class. This class provides functions that can be called
by the scripts (which may be calls to other scripts):

...

GroovyScenarioCard sCard = new GroovyScenarioCard();

...

    if(section['@id'].equals("title")){
       section.each() {entry ->
          if(entry['@name'] != null){
              sCard.getInfo().scenarioName = "${entry['@name']}" as String;
          }
          if(entry['@id'] != null){
              sCard.getInfo().scenarioId = "${entry['@id']}" as String;
          }
       }
    }

...


I am also relatively new to Groovy so this may not be the best way.

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email