• Advertisement
Sign in to follow this  

Scripting system

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

Hey, I want to implement a scripting system in my engine that will control game logic and such. In the scripts I want to have a C++/Java-like syntax so I decided to include functions and classes. So here comes my question, each function will take parameters just like any other language. I want to be able to call these functions from inside the script and from inside the game. How should this work? Right now I have a class called ScriptFunction that holds a list of 'statements' to be executed. How can I make it so that I can call this function (with a function like ScriptFunction::Execute( ).. ) but also make it take an arbitrary number of parameters? I was thinking of using a base parameter class and deriving it for each set of parameters.. but it needs to be known at run-time, not compile-time. Thoughts?

Share this post


Link to post
Share on other sites
Advertisement
You might take a look at a 3rd party scripting language like Python, Lua, Squirrel, Angelscript, GameMonkey, etc...

All of those languages have the ability to call a function from c++.

Share this post


Link to post
Share on other sites
that seems to be the standard answer around here, x already did it, use theirs. lots of people have also written engines, but i'd like to write my own. no offense to the person who was nice enough to reply to my post.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You will probably need a way to convert the parameters into your script languages internal format. Look at the way Python has C++ calls to build parameter lists and to pass to a python subroutine call. VB uses Variant types so it can be passed lists that are a close approximation of the list structures used in Python.

It all depends what your own script language is like as to how complicated the mapping back and forth will be (ie- fixed parameter lists and basic types like chars/ints/floats versus the compound list based system that script languages like Python use)

Share this post


Link to post
Share on other sites
Quote:
Original post by MilfredCubicleX
that seems to be the standard answer around here, x already did it, use theirs. lots of people have also written engines, but i'd like to write my own. no offense to the person who was nice enough to reply to my post.

No offense, but if you need to ask basic questions about how to create a scripting language, you're probably not quite at a level where you can realistically accomplish that goal.

At the machine code level, when you need to pass more parameters to a subroutine or system call than can fit in the appropriate registers, you use the stack. So instead of accessing the arguments directly, push them all onto a stack and pass the stack. A vector or array can work, too.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by drakostar
Quote:
Original post by MilfredCubicleX
that seems to be the standard answer around here, x already did it, use theirs. lots of people have also written engines, but i'd like to write my own. no offense to the person who was nice enough to reply to my post.

No offense, but if you need to ask basic questions about how to create a scripting language, you're probably not quite at a level where you can realistically accomplish that goal.

At the machine code level, when you need to pass more parameters to a subroutine or system call than can fit in the appropriate registers, you use the stack. So instead of accessing the arguments directly, push them all onto a stack and pass the stack. A vector or array can work, too.


100% agreed, the OP might want to spend some time with wikipedia/google.

Share this post


Link to post
Share on other sites
Quote:
Original post by MilfredCubicleX
that seems to be the standard answer around here, x already did it, use theirs. lots of people have also written engines, but i'd like to write my own. no offense to the person who was nice enough to reply to my post.


If you would like to write your own scripting language instead of plugging another in and saving months of work, it is your rightful and possibly justified decision (and I regularly take similar ones every so often).

If you would like to write your own scripting language without even taking a look at how other existing and successful languages solve some of the problems you encounter, that is unnecessary and unjustified masoschism. The best place to look for answers to questions such as yours would be in the source code scripting language interpreters, such as Python (boost::python), Lua (Luabind) or others.

In an unrelated note, C++/Java-like syntax does not imply functions and classes (procedures and structs are an alternative). C++/Java semantics do involve functions and classes. But that's just nitpicking.

To answer your question myself (although possibly the source above would be of far greater use, correctness and precision), every single virtual machine I have implemented so far used closures with boxed parameters.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by ToohrVyk
In an unrelated note, C++/Java-like syntax does not imply functions and classes (procedures and structs are an alternative). C++/Java semantics do involve functions and classes. But that's just nitpicking.

he probably meant to say ECMA-like scripting (?)

Share this post


Link to post
Share on other sites
Quote:
Original post by thre3dee
you might want to check out this book then, Game Scripting Mastery
A little off topic, but I'd recommend this book too. I'm currently reading it (About 90% done), and it's taught me a lot of useful stuff about writing a scripting language. There's also a lot of bits in it that make me go "Eww, that's a horrible way of doing things", but meh [smile]

Share this post


Link to post
Share on other sites
I've written my own scripting engine before (aswell as the game engine to go with it) and actually made a game with it.

However, if I had it to do over again (which I have in my new project) I would use Lua (which I do).

While I understand your drive, understand that in such an undertaking (both game and scripting engine from scratch) you could potentially be looking at three+ years of work if you ever finish it at all.

That is the cost, and while doing it and succeeding will gain you an incredible amount of knowledge, you need to ask yourself is that knowledge really important to you, or would you rather have a finished game faster.

The biggest hurdle you have to face is not giving up.

Food for thought :)

Good Luck,

Share this post


Link to post
Share on other sites
I'm in no rush to finish anything, I'm just messing around. In fact, i haven't touched this source code in about a week because i've been swamped with work. Thanks to those who helped with some ideas.

Share this post


Link to post
Share on other sites
This problem has a realy nice solution in C++. I've recently made a VM and a C++ framework capable of these kind of things, but it seems not so many people are interested in what is going on inside of the scripting virtual machine. Take a look at this topic http://www.gamedev.net/community/forums/topic.asp?topic_id=413653 and check out my webpage, you will find a complete solution there.

Share this post


Link to post
Share on other sites
Quote:
Original post by MilfredCubicleX
I'm in no rush to finish anything, I'm just messing around. In fact, i haven't touched this source code in about a week because i've been swamped with work. Thanks to those who helped with some ideas.


This is definately the way to go and to -> learn <-.
Dont listen to those who say : You should go with an existing script. (That do not apply only to script)
YES if you are making something that involve cash and you dont want bugs and have short time to do it. Of course, totally agree with them!

But if you are just messing around for fun, try making it yourself. :)
And I like that mentality of : I want to reinvent the wheel.
Of course you will probably never make a game with it.
But it's better for general knowledge and become a better/faster programmer ;).

Share this post


Link to post
Share on other sites
I'm actually making my own language and vm for c++ as well. its in a thread but no one seems to want to comment on it.

Its changed a bit anyway

import application from "app.e"
macro ESCRIPT_VERSION 0x1000

-- defines the runapp function as a thread thus not requiring a return type
-- since it will not return into a variable
-- the threadname attribute allows functions to grab the thread using its name
-- rather than the handle
attributes thread : threadname ("appthread")
runapp (app ^application)
{
if (app.init () == false) return
f ^io.file = io.open ("log.txt")
f.write ("hahahaha")
f.close ()
while (app.update ()) {
-- do loop
}
}

-- defines the main function with attributes entry point and global (i.e. accessible via entire virtual machine
attributes entry : global
main (nil) int
{
appInst thread = runapp (gc application ("app1"))
yield appInst
while (!system.isKeyDown (system.keys.escape)) wait 1
resume appInst
wait 1000
exit appInst
}

Share this post


Link to post
Share on other sites
Use C# as your scripting language. That fits your "C++/Java-like" requirement. :-) This is what Reality Engine (and thus CellFactor) utilize.

There should be a sample in the DirectX sdk, so check it out. IIRC, the Mono framework can be utilized in the same manner, so it's a crossplatform solution.

Share this post


Link to post
Share on other sites
Quote:
Original post by drakostar
No offense, but if you need to ask basic questions about how to create a scripting language, you're probably not quite at a level where you can realistically accomplish that goal.

At first glance that seems like a logical conclusion, but I will usually undertake a task that that is over my head as a means to learn all of the dependent skills. So just for the record I disagree with this statement, although it's a perfectly reasonable thing to say.

RE: the OP's question... Some keywords that might lead to useful information are: "typelist" (from the book Modern C++ Design), "type polymorphism", and "weak typing".

A straightforward (but potentially messy and inflexible) method could be to have a ScriptFunction contain a list of type ids (e.g. an enum) that represent the types of the function parameters and the number of required parameters. Then to call the function you simply have to convert the passed-in parameters to the expected types (if possible).

I'm assuming you're familiar with the variable argument C construct, as seen in printf. Using the "..." operator is "bad", but it's typically simpler to implement than doing it "right" with a more type-safe approach.

Share this post


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

  • Advertisement