Sign in to follow this  
desu_ka

Unity What I want in a language

Recommended Posts

Hello! I've been working on a 2D tile-based RPG engine that uses my own scripting language but I've decided that I want to abandon this clumsy, informal language of mine and utilize a 3rd party embeddable scripting language. I've looked at a few possibilities (Lua, in particular) but it's not clear to me how or if these languages are really meant for what I want to do. Here's how my engine has worked so far: - C++ provides a library of general functions that control the game environment: creating characters, moving characters, and other fundamental arbitrary actions. - C++ creates multiple instances of what I've called ScriptHandlers. These objects have an associated script file. - Every frame, C++ loops through the ScriptHandlers. Each SH takes this opportunity to run part or all of its script. These scripts will call C++ functions to supply game-specific activity (creating a particular character, moving a character, displaying some dialogue, etc). There may be logic if/then) and looping involved. At some point, the script may need to halt itself. The program flow returns to the SH; the scripting language does not just call another C++ function! - The next time a particular SH has its turn to run its script, it needs to restart wherever it left off. There are basically three levels of "game control" going on here: Graphics, user input, collision detection, etc (C++) -> Game logic (script) -> -> Actual creation/manipulation of characters, dialogue, etc (C++) The big issue here is that the scripts need to be able to halt their own execution (and control returns to C++), but save their place so that C++ can restart the script where it was left off. Perhaps a virtual machine that can be paused and resumed. I know I've made a few threads on this subject already, but I've found few resources on these languages, unfortunately, so it's difficult to make an educated guess at what's appropriate for me. I appreciate any advice what-so-ever. Thanks!!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:

The big issue here is that the scripts need to be able to halt their own execution (and control returns to C++), but save their place so that C++ can restart the script where it was left off. Perhaps a virtual machine that can be paused and resumed.
You want continuations, it seems, to implement cooperative multitasking.

Or (if they just need to be halted and resumed once) generators.

Any language which provides closures or explicit generator constructs can provide this functionality; python's generators can be very conveniently used for this. So you might embed a python interpreter in C++ (or call your C++ from python, maybe a saner choice) and write stuff like:


def a_script():
do_something1()
do_more()
#return control while saving state
yield
#execution resumes at this point when the generator object's .next() method is invoked
do_something2()
#return control after doing somethng2
yield
#execution resumes here
....

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
you might also want to check out AngelScript, which has its own forum here at GameDev.net

Share this post


Link to post
Share on other sites
I have looked at Angelscript and it's possible that it's just what I want. There's very little in the way of tutorials and such, though. I'll keep poking at it, though. The same goes for GameMonkey, which seems similar in functionality to Angelscript (as far as I've looked into it).

I don't know anything about continuations or generators, but the example you gave is very close to what I want. Is it usually possible for a script to yield within a logical or looping construct and resume the same state it left afterward? What are some languages that have this feature?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:

I don't know anything about continuations or generators, but the example you gave is very close to what I want. Is it usually possible for a script to yield within a logical or looping construct and resume the same state it left afterward?
Of course, that's the whole point. The generator construct takes care of remembering the state so you don't have to.

Let's make two generators, one that makes even numbers and one that makes odd numbers. Notice they use a while loop:


def generate_odds ():
current = 1
yield 1
while True:
current += 2
yield current

def generate_evens():
current = 0
yield 0
while True:
current += 2
yield current



Let's look at an example interaction:


>>> evens = generate_evens()
>>> odds = generate_odds()
>>> evens
<generator object at 0x00B3A1C0>
>>> evens.next()
0
>>> evens.next()
2
>>> odds.next()
1
>>> odds.next()
3
>>> odds.next()
5
>>>



Of course, generators are objects, so we can pass partially-executed scripts around and combine them in different ways. let's make a generator which interleaves the results of two other generators:


def interleave_generators(gen1, gen2):
state = 0
while True:
if state == 0:
state = 1
yield gen1.next()
else:
state = 0
yield gen2.next()



We can call this with two brand new even and odd generators, or with the half-executed generators 'evens' and 'odds' we defined previously (it'll start interleaving them from where we left off, that is, 4 in the evens generator and 7 in the odds one):


>>> all_numbers = interleave_generators(generate_evens(), generate_odds())
>>> all_numbers.next()
0
>>> all_numbers.next()
1
>>> all_numbers.next()
2
>>> previously_defined = interleave_generators(evens, odds)
>>> previously_defined.next()
4
>>> previously_defined.next()
7
...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this