ScriptCS hosting

44 views
Skip to first unread message

Markus Burrer

unread,
Jun 15, 2017, 12:49:22 PM6/15/17
to scriptcs
Hi, I want to write a game in C# and it should be possible to extend the game with C# scripts. I want to use ScriptCS and I take a look at the hosting example from Glenn Block

https://github.com/glennblock/scriptcs-hosting-example

But just execute a script once in not very helpful. I want to load multiple scripts to memory and I want to be able to call functions in the script from the host or the host should provide methodes and data for a script

For example, the host should be able to call functions similar to the Start() and Update() functions in the Unity Engine. Also the script should be able to execute functions and access data from the host.

How can I do that?

Regards
Markus

Glenn Block

unread,
Jun 15, 2017, 1:17:47 PM6/15/17
to Markus Burrer, scriptcs
Hi Markus. The easiest way to do that is to create a script on the fly in memory with a bunch of "#load" directives to load those files then you can execute that string rather than loading a file. 

If you want to call functions in the script, one way is to pass back an object that exposes members which the caller can invoke. 


Markus Burrer

unread,
Jun 15, 2017, 1:28:48 PM6/15/17
to scriptcs, markus...@gmail.com
Hi Glenn,
thanks for the quick answer. It should be possible to parse over a script folder recursively and load for example each script with the name main.csx. Each mod should have it's own subfolder in the script folder.

And do you have an example how to bass back objects?

Glenn Block

unread,
Jun 15, 2017, 1:52:02 PM6/15/17
to Markus Burrer, scriptcs
I don't, though maybe I'll update my sample. The way you do this is you have the last line of the script be the object you want to return, also there's no semicolon.

That object will then get returned back to the client who invoked the Executor. It is the ReturnValue property on the result object: https://github.com/scriptcs/scriptcs/blob/dev/src/ScriptCs.Contracts/ScriptResult.cs#L54

Markus Burrer

unread,
Jun 15, 2017, 2:08:48 PM6/15/17
to scriptcs, markus...@gmail.com
I think, I understand. And I think with this behaviour I have to load each script separate because each script needs it's own object.

I need to parse the directory and create an executor for each script and store it in a dictionary.

Markus Burrer

unread,
Jun 16, 2017, 3:03:35 AM6/16/17
to scriptcs
Or did I misunderstand an any files can return an object, even if they are loaded with #load?

Glenn Block

unread,
Jun 16, 2017, 6:16:05 PM6/16/17
to Markus Burrer, scriptcs
Well you could just have each file set a variable and then have the main script grab all the variables and return them like on an expando object or dictionary.

Glenn Block

unread,
Jun 16, 2017, 6:18:23 PM6/16/17
to Markus Burrer, scriptcs
Basically any vars set in a script are available globally
Reply all
Reply to author
Forward
0 new messages