C# Variables imported into a IronPython Script

Started by
1 comment, last by EmpireProductions 13 years, 2 months ago
After I write this I will be fooling around with this myself but in case I can't figure it out how would I get some Variables into a Python Script?

I have a IronPython Scripting Engine that I wrote that I am able to Load Python Scripts into my Server. However now I would like to have the Combat Calculations done with in a python script so I can tweek them with out having to always recompile my server. In order to do that I will need to get the id of the attacker and the victim so I can use it in the script. How would I do that?
In Development:Rise of Heros: MORPG - http:www.riseofheroesmmo.com
Advertisement

After I write this I will be fooling around with this myself but in case I can't figure it out how would I get some Variables into a Python Script?

I have a IronPython Scripting Engine that I wrote that I am able to Load Python Scripts into my Server. However now I would like to have the Combat Calculations done with in a python script so I can tweek them with out having to always recompile my server. In order to do that I will need to get the id of the attacker and the victim so I can use it in the script. How would I do that?


You should be able to create a ScriptScope from the IronPython engine and create / retrieve variables.


See if this helps: http://www.voidspace.org.uk/ironpython/dlr_hosting.shtml

Rainweaver Framework (working title)

IronLua (looking for a DLR expert)




'EmpireProductions' said:

After I write this I will be fooling around with this myself but in case I can't figure it out how would I get some Variables into a Python Script?

I have a IronPython Scripting Engine that I wrote that I am able to Load Python Scripts into my Server. However now I would like to have the Combat Calculations done with in a python script so I can tweek them with out having to always recompile my server. In order to do that I will need to get the id of the attacker and the victim so I can use it in the script. How would I do that?


You should be able to create a ScriptScope from the IronPython engine and create / retrieve variables.


See if this helps: http://www.voidspace…r_hosting.shtml




Figured it out! Thanks!

In case any one else wanted to do something similar here is my code:



public PythonCombatResult doCombat(long victimId, long attackerId)
{
//Put the needed Variables into Python
scope.SetVariable("victimId", victimId);
scope.SetVariable("attackerId", attackerId);

//Execute Python Script
engine.ExecuteFile(_nonImportDirectory + "Combat.py", scope);

//Retrieve the needed Variables from Python
float playerHealth = (float)scope.GetVariable("playerHealth");
bool won = (bool)scope.GetVariable("won");

//Create and populate the result structure
PythonCombatResult result;
result.attackerWon = won;

//Debug Write to Console
Console.WriteLine(playerHealth);

//Return the results
return result;
}





scope.SetVariable(&quot;victimId&quot;, victimId);<br /> <br /> this line takes a C# variable and places it into Python accessable by the string name in this case victimId<br /> <br /> <br /> <br /> float playerHealth = (float)scope.GetVariable(&quot;playerHealth&quot;);<br /> <br /> This line of code gets a Python Variable and puts it into C#<br /> <br /> <br /> <br /> <br />
In Development:Rise of Heros: MORPG - http:www.riseofheroesmmo.com

This topic is closed to new replies.

Advertisement