Sign in to follow this  

[.net] Using C# script in game.

This topic is 4354 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

Hello there. I have got C# scripting up and running in my game (which also is written in C#), but there is some things that I haven't been able to figure out. 1. Is there some smart way to alter variables in the game from the script? 2. Is there a way of knowing what assemblies to add in the CompilerParameters? Adding all assemblies that the game uses seems stupid somehow. I would be really greatful for an answer. / Patrik, Sweden

Share this post


Link to post
Share on other sites
Although it probably does the same thing as what you have, GakScript is my implementation of a .NET scripting engine with multi-language support (VB, C#, Boo, etc). It's much nicer to work with then using the API directly at least. [/SELFPLUG]

IronPython 1.0 Beta 1 just recently came out. It might be better to use an actual script (like Python) rather then a true programming language. LuaInterface would be a good solution as well if you want to use actual scripts. Martin made up a great article here on GameDev about it: Using Lua with C#.

Share this post


Link to post
Share on other sites
Thanks, but I already used Lua and some other scripting languages. I know how to use those but I would like to try to do it in C#, and that's why i asked that specific question. I want to learn it myself, and using someone else's wrappers won't help me with that.

Anyone that has an answer to my questions?

Best regards / Patrik Svensson

Share this post


Link to post
Share on other sites
Well, if you're using GakScript, you can just make up public static properties within your C# code. When setting up the scripting interface, you'd call script.AddReference() to let the script reference the program. Then from the script you can just change those properties how you would normally change them from the code.

Share this post


Link to post
Share on other sites
I don't know if you're doing this already, but you can include the game EXE itself as one of the assemblies in the CompilerParameters. That way, your script can reference any public class/method in your game. Then you can pretty much do anything you need to (you can have script classes inherit from your EXE classes/interfaces, etc). Incredibly powerful.

Share this post


Link to post
Share on other sites
Nypyren:

Sounds interesting. Can you tell me more about how to alter variables or class-members in the game from the script? Maybe you even have an example?

Rob Loach:

I appreciate your help, but as I said before; I want to learn how to do it myself and understand the underlying technology for scripting with C#. If I use someone else's wrappers for that, there will be no learning.

Best regards / Patrik

Share this post


Link to post
Share on other sites
A 'clean' way for your script to access variables/classes that are present in your app is to make a static class:


// C# 2.0 code
using System.Collections.Generic;

namespace MyEXE // Or whatever you want to use
{
public static class ScriptGlobals // you may or may not need the 'static' keyword.
//I don't remember if this affects cross-assembly visibility or not.
{
static Dictionary<string, object> flexibleVariables;

public static Dictionary<string, object> FlexibleVariables
{
get { return flexibleVariables; }
}

static ScriptGlobals() // static constructor
{
flexibleVariables = new Dictionary<string, object>();
}
}
}




That's a class with a simple static string-to-object hash table. In either your EXE or your script, you can add entries to that table, modify the object contents, etc.

In your script file, you would just be able to say:


using namespace MyEXE;

namespace Script1
{
public class Script
{
SomeObject o;

public Script()
{
// find an object in the script global table
if (!ScriptGlobals.FlexibleVariables.ContainsKey("variableName1"))
throw new Exception("Can't find a required script global object");

o = ScriptGlobals.FlexibleVariables["variableName1"] as SomeObject;
if (o == null)
throw new Exception("variableName1 is a different type than expected");
}
}
}




This assumes you compile the script with an assembly reference to the EXE. In this example, the script constructor would look for a specific variable in that lookup table and store the reference for later so that it doesn't have to look it up by a string every time you want to use it.

You don't have to do it this way, either. Let's say you have a script that needs to play a sound. Your EXE could have some kind of SoundManager singleton or static class, and to play a sound, your script would just call SoundManager.PlaySound("somesound.wav") or something like that.


A messier method is to use reflection in your script to find whatever you want from the EXE (kind of like the reflection you use in your EXE to create a new Script instance). You can do that pretty much any way you want.

p.s. the code above might have typos in it. I just typed it into the message without checking to make sure it compiles.

Share this post


Link to post
Share on other sites

This topic is 4354 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.

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