Jump to content
  • Advertisement
Sign in to follow this  
MilfredCubicleX

Scripting system

This topic is 4391 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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!