• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Migi0027

How does scripting work

4 posts in this topic

Hello guys,

 

so for a while now I have been programming in c++ with Direct3D 11, and I know that soon i might reach a point where scripting will be required, but huge performance questions pop up. Most of them are below here, you don't need to answer all if you don't want to, but it would be nice. wink.png

  • How does the program understand the code? Does it read per each line of string (Wouldn't this be SLOW?), or does it semi compile (like setting all the commands into an array from the beginning), or how?
  • Should i just leave it, and use direct c++ (The problem is that i would like to have a way that the user didn't need to compile anything).
  • How (if you imagine you had to compile it, but not in c++) should i handle the script code, or in other words, how should my program try to understand it (e.g. Now there's a bracket, so go to...).

Maybe scripting is too hard (not TO script, but for the program to understand it) as I never had any experience, but I'm willing to give it a try.

 

Thank You

0

Share this post


Link to post
Share on other sites

How does the program understand the code? Does it read per each line of string (Wouldn't this be SLOW?), or does it semi compile (like setting all the commands into an array from the beginning), or how?

Like Jason said, you probably won't be handling the parsing or execution of the scripted instructions. You'll be using a library that does that for you.
Usually, when you ask the library to load a script, it will get compiled into bytecode, but some more advanced techniques exist that will actually compile the script code into native instructions that your CPU can directly execute, which improves performance a lot.
This should all be specified somewhere in your chosen library's feature list.
 

Should i just leave it, and use direct c++ (The problem is that i would like to have a way that the user didn't need to compile anything).

There are various reasons one would choose to use scripting in a project. You should research about why should you use it, and then decide whether you'll get advantages out if it or not. Don't just go and force yourself to use scripts if you don't need to, unless you're just learning and have some spare time.
I personally am using scripting (Mono) in my current game project for various reasons, including:

  • game code separation from engine code
  • easier to maintain and add features
  • very fast compilation times

Some libraries (don't know if all) allow you to distribute precompiled files which contain bytecode. This should reduce your scripts filesizes, and protect them against easy editing.

 

How (if you imagine you had to compile it, but not in c++) should i handle the script code, or in other words, how should my program try to understand it (e.g. Now there's a bracket, so go to...).

That is called Parsing, and the library does it for you.

If interested on how to comunicate with the script methods, you should see what others are doing to get some ideas of the implementations. I have an implementation similar to Unity3D where nodes with attached scripts call the script's "update" function every frame and so on...

Edited by Kamikaze15
0

Share this post


Link to post
Share on other sites

Differently from many, I have wrote my scripting language and the accompanying compiler. So I guess I can give some extra insight.

How does the program understand the code?

It doesn't. Have you ever compiled something by hand, say for a Motorola CPU? A compiler is in fact a table where MOVE from_reg to_reg equals a certain bit pattern. A component in your program knows those bit patterns and evolves its internal state accordingly, just as a CPU would do. More advanced scripting languages compile the script just in time, so the CPU can execute it directly.

Anyway, if you're thinking at scripting for gaming and performance, I'm afraid the design has gone wrong in many ways. Scripting must be implemented in a way that makes it a non-issue performance wise, which typically means being event-driven.

 

Should i just leave it, and use direct c++ (The problem is that i would like to have a way that the user didn't need to compile anything).

You can get a lot of mileage out of C++. Use scripting for "gameplay logic".

 

How (if you imagine you had to compile it, but not in c++) should i handle the script code, or in other words, how should my program try to understand it (e.g. Now there's a bracket, so go to...).

Just in the same way you read the code. Are you aware of formal languages and BNF? Perhaps take a look at them. They're very easy at their core (albeit they will give you headaches).
0

Share this post


Link to post
Share on other sites
FWIW, for my project, I am using my own script language (sort of like JavaScript and ActionScript mixed with C, Java, and C#).


and, generally the way it is run works in a number of stages.

first, there is a parser, which:
starts breaking the code down into individual "tokens", for example, a brace, or an identifier (variable name), or a number, ... each according to their specific rules (numbers contain digits, ...);
starts matching the tokens against various syntax patterns, going down the parts of the tree for which the syntax matches;
in doing so, produces a tree-like structure (an "AST") representing the code it has seen along the way.

the next stage is the front-end compiler which:
walks along this structure, figuring out various pieces of information (where is this variable declared? does this operator have a known type? ...) and starts spitting out appropriate globs of bytecode.

at this point, bytecode may be saved for later, pulled from files, or simply passed to the back-end.


then, we get to the interpreter backend, which in my case currently:
begins by converting this bytecode into a list-like structure (representing individual bytecode operations), usually whenever a function or method is first called;
splits apart this structure into a collection of "traces", which represent various non-branching sequences of bytecode instructions (technically, this is a "Control Flow Graph", or "CFG");
it may either directly execute these traces (via embedded function-pointers), or (if a counter runs out), pass it off to the JIT, which then spits out a mix of function-calls back into the interpreter, and directly compiled machine-code sequences for various operations.


granted, these last stages aren't strictly required, and there are many interpreters which instead directly execute bytecode (by decoding and invoking the logic for each bytecode instruction one-at-a-time).

and, some much simpler interpreters will simply walk over the AST, and some directly on the input text.


the tradeoff mostly has to do with performance, and the relative amount of resources "invested" into a piece of code.
simpler strategies are better when code will likely at most only ever be seen once, and never be run again. typically, execution speeds are "very slow" (often 1000x-10000x or more slower than native).

directly interpreting bytecode is generally better when code will be run more than once (so hopefully isn't "dead slow"), but doesn't need to be "particularly" fast. bytecode interpretation can usually get within about 100x-200x of native speeds (IME with "while()" loops and large "switch()" blocks).

(some people have also had good luck with getting good performance out of "computed goto" and "label pointers", but these are generally GCC specific).


trace-graphs can get a little faster, IME generally around 50-60x of native (though some experimental interpreter designs have run much faster than this, like 8-10x of native, but it seems problematic to replicate this effect in a "real" interpreter). (granted, some could have to do with architecture: my experimental interpreter was more Dalvik like, using untagged registers and three-address operations, whereas my main interpreter is a stack-machine and currently still uses tagged references).

this is partly because a lot of the "figuring out what to do" work can be moved out of the execution path, so largely it becomes a matter of simply calling through function-pointers (and generally using a "trampoline loop").


a JIT can still be faster though, reaching comparable speeds to native C or C++ code (and doing pretty much anything else a traditional compiler can do), though this isn't always necessarily the case, for example, my current JIT is fairly naive and can get within about 3x of native C speeds in some cases, but typically runs often runs a bit slower than this (as much of its operation is generally still handled via interpreter machinery), but often this doesn't matter as much as I am not usually using the scripting-language for speed critical tasks. (basically, it goes fastest if treated like it were a C-like subset of Java, but using it this way is lame, and is not really what it was designed for...). (reaching C-like speeds generally requires things like figuring out how to cache variables in registers, perform register allocation, ... which some of my past JITs have done, but my current JIT doesn't do).


note that, while interpreters can be interesting, writing them can also end up eating up a large amount of time and effort, so are not necessarily a good idea if a person has other things they want to get done (and, it may take effectively years of time and effort invested into them before they stop looking like a joke if compared with more mature pieces of technology). Edited by cr88192
1

Share this post


Link to post
Share on other sites

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  
Followers 0