4 ms·
I realized saying "it's a trivial exercise" without showing some code isn't very useful, so here's everything you need: 1) Add these imports to your class:
by icey 16y ago
I realized saying "it's a trivial exercise" without showing some code isn't very useful, so here's everything you need:
1) Add these imports to your class:
using IronPython.Hosting;
using Microsoft.Scripting;
using Microsoft.Scripting.Hosting;
2) Instantiate your engine & scope in the appropriate location:
mEngine = Python.CreateEngine();
mScope = mEngine.CreateScope();
3) Add variables to your scope like this:
mScope.SetVariable("ThisIsWhatTheScriptSees", mReferenceToYourObject);
mScope.SetVariable("Cars", mCars);
mScope.SetVariable("Dog", mDog);
4) Load your script up:
var source = mEngine.CreateScriptSourceFromString(strYourScript,
SourceCodeKind.Statements);
5) Run it!
source.Execute(mScope);
* You can call the stuff you added to your scope from inside your script:
if Dog.IsTame:
name = Dog.CheckName()
for car in Cars:
car.BeepHorn()
* Alright, this is a little more than 2 lines, but I think you get the drift. Good luck!