Jump to content
  • Advertisement
Sign in to follow this  
Wyzfen

Scripting from .NET

This topic is 4553 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I've recently started thinking about scripting for my managed engine. One of the key things is that I need to be able to say how long a script is allowed to run, before it returns. And then continue it again in the next update if its not done. Problem is, I dont know what this is called (re-entry is my best guess) so i cant search for it. Where should I be looking for information on this ? (Books, sites, keywords etc) ? Also, is that a good idea - i'll have many scripts running in parallel (as far as my engine is concerned, which is why i want to do time-divided multitasking) and so i figured that would be the fairest way. Finally, what scripting languages support this kind of idea ? and are they supported with .NET ? thanks, Wyzfen

Share this post


Link to post
Share on other sites
Advertisement
I think I understand what you mean. You are trying to emulate a multi-threaded environment. The easiest, and probably your best option is to simply run each thread on their own Process. That was the OS will start and stop the scripts itself etc.... If you still want to time each script's run-time, then simply use a Timer and then call Process.Kill() to stop the respective process.

Have the process timing on it's own thread or the main thread. Each script process should have a 'runTime' variable that gets incremented on each interation through that process. The process timer will check this variable (via lock() -- C# -- unsure about VB) and if it is to great, then kill the thread that you are checking.

By the way, LUA is now .NET language I believe

Share this post


Link to post
Share on other sites
What you want is a form of preemptive multithreading in your script engine. I don't know a script engine that provides that. Lua and python use coroutines, (the script returns at a yield() call, yield can be called at the return of C functions). If you have many scripts, running each script in it's own OS level thread will be impossible (huge overhead, locks in the scripting engine).

I think scripting language creators have a good reason to not include this feature. They do not want their users to experience the synchronisation nigthmare that would result from true concurency.

Share this post


Link to post
Share on other sites
From what you said, it seems like you want to limit how long a script is run for. You can easily do this by creating a Thread, start a timer, and then execute the script. If the execution time of the script gets out of control, you could easily stop its execution.

As for .NET scripting, using .NET's CodeDom, you could use C# or VB.NET as a scripting language and compile the code at runtime. Another option would be to use a language like IronPython or Boo, both of which are amazing languages. All .NET code can be read in by the .NET CodeDom and compiled during runtime making scripting in .NET extremely powerful.

Quote:
Original post by rpg_code_master
By the way, LUA is now .NET language I believe
What you are thinking of is LuaInterface which simply implements a layer ontop of Lua and doesn't actually get compiled into the .NET CLI. Although it works, it doesn't allow you to do some stuff that you really need in a .NET scripting engine (like access your .NET classes directly from the scripting language without having to write a wrapper for it).

Share this post


Link to post
Share on other sites
Thanks for the ideas :)

I had a brief look at lua.Net and at c# scripting before I posted - but realised that the multitasking was the most important feature and it'd be easier to find a scripting language that supported it. Also, I do prefer the ease of accessing my engine from C# vs wrapping it in Lua.Net.
I only heard of IronPython today - its what prompted this question :) - its still in alpha tho, so i'm not comfortable using it at this stage.

I had considered threads, but given how many scripts i'm wanting concurrently, I dont think that'll work. Also, I dont want to kill a process, i want it to be able to return cleanly when its told to, possibly with intermediate results, and then re-enter at the same point. I'm toying with the idea of rolling the scripts into some sort of manager which is then threaded. Must think on that.

I have programmed with coroutines before (on a zx180 chip using uC++) - i was originally thinking of doing my scripts a similar way - writing a 'yield' function that stores the state of the script and returns if necessary.
The problem with that is I'd need to call it after every significant function. And since i want users to be able to write scripts, it leaves it in their hands - which I think is a bad idea :(

Overall, I think i'll still tending to c# scripts and write my own Yield functionality. I cant see any practical way to re-enter a script at the exit point tho ! If Lua supports yield, that might have to be the way to go.

{EDIT} .NET 2.0 Supports the yield command in iterators - so if i make my script interface implement IEnumerable, i should be able to yield as though they were co-routines - then my engine can just use GetNext() and Current to execute the scripts.
{/EDIT}

so, thanks for the help - coroutines and yield were the keywords I wasnt thinking of :)

Thanks again,

Wyzfen

[Edited by - Wyzfen on May 31, 2006 11:31:31 AM]

Share this post


Link to post
Share on other sites
My advice would be to stay away from Lua when looking to make a scripting interface in .NET. When you're making a scripting engine, you want the script to have the ability to access your C# code. With LuaInterface, the only way you can do this is to write function wrappers and even then you'll still have troubles with implementing wrappers for all of your classes.

IronPython and its cross-platform cousin, Boo, are stable enough to be used in applications. Either use those or implement your own CodeDom wrapper for C# scripts. I wrote my own CodeDom wrapper a while ago named GakScript in which you provide the input code and the language and then compile it. I haven't continued support for it in a while though so you might be better to take a look at the source and see how it's done.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!