Scripting - University of Calgary in...
Transcript of Scripting - University of Calgary in...
![Page 1: Scripting - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/585_W13/d104_scripting.pdf · 2013-01-03 · • Lisp, Scheme, etc. – Very expressive syntax • Not](https://reader033.fdocuments.us/reader033/viewer/2022050506/5f976376b7bd7135a851b715/html5/thumbnails/1.jpg)
Scripting
1
![Page 2: Scripting - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/585_W13/d104_scripting.pdf · 2013-01-03 · • Lisp, Scheme, etc. – Very expressive syntax • Not](https://reader033.fdocuments.us/reader033/viewer/2022050506/5f976376b7bd7135a851b715/html5/thumbnails/2.jpg)
Overview
• Rationale• Division of labour between script and C++• Choice of language(s)• Interfacing to C++
– Reflection– Bindings– Serialization
• Performance, memory
![Page 3: Scripting - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/585_W13/d104_scripting.pdf · 2013-01-03 · • Lisp, Scheme, etc. – Very expressive syntax • Not](https://reader033.fdocuments.us/reader033/viewer/2022050506/5f976376b7bd7135a851b715/html5/thumbnails/3.jpg)
Rationale
• C++ isn't the best choice for all problems– Complicated feature set, syntax– Low-level, dangerous– Lengthy shutdown/compile/link/rerun development process
• Many game projects use scripting to address some of these problems
![Page 4: Scripting - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/585_W13/d104_scripting.pdf · 2013-01-03 · • Lisp, Scheme, etc. – Very expressive syntax • Not](https://reader033.fdocuments.us/reader033/viewer/2022050506/5f976376b7bd7135a851b715/html5/thumbnails/4.jpg)
Advantages of Scripting
• Expressiveness– Scripting languages employ higher level constructs than C++
• Reduced iteration time– Interpreted, or byte-code compiled on-the-fly
• Useful features– Automatic resource management (garbage collection)– No pointers– Safe data structures– Dynamic typing
• Accessibility– Easier for casual programmers to grok than C++
• Customizable– Write your own language, or change an existing one to suit your
needs
![Page 5: Scripting - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/585_W13/d104_scripting.pdf · 2013-01-03 · • Lisp, Scheme, etc. – Very expressive syntax • Not](https://reader033.fdocuments.us/reader033/viewer/2022050506/5f976376b7bd7135a851b715/html5/thumbnails/5.jpg)
Problems
• Additional work to bind the chosen language to C++– These bindings are often non-trivial
• Standard C++ tools are not script aware– Debugger, compiler, profiler– Script is a “black hole” to C++
• Multi-language debugging is harder• Script code runs slower
– But remember the 90/10 rule• Harder to keep memory usage in check• Having non-programmers write code can be a disaster
![Page 6: Scripting - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/585_W13/d104_scripting.pdf · 2013-01-03 · • Lisp, Scheme, etc. – Very expressive syntax • Not](https://reader033.fdocuments.us/reader033/viewer/2022050506/5f976376b7bd7135a851b715/html5/thumbnails/6.jpg)
Division of Labour
• Script can can be employed with varying levels of commitment– Loading only, scripts just wire up C++ components at load time– Script high level game flow, only one script running– Event handlers scripted
• Handlers affect the game flow and/or entity behaviour– Entity behaviour scripted
• Each entity is running a little script (usually a state machine of some sort)
– Write the whole game in the script language• Not really scripting any more then, is it?• Use C/C++ only to accelerate the performance-critical stuff
![Page 7: Scripting - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/585_W13/d104_scripting.pdf · 2013-01-03 · • Lisp, Scheme, etc. – Very expressive syntax • Not](https://reader033.fdocuments.us/reader033/viewer/2022050506/5f976376b7bd7135a851b715/html5/thumbnails/7.jpg)
Scripting Philosophy
• Where a team stands on this depends on– the type of game they are writing– the programming ability of the scripters, and– how much performance they are willing to trade for the benefits of
scripting• Remember the 90/10 rule
![Page 8: Scripting - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/585_W13/d104_scripting.pdf · 2013-01-03 · • Lisp, Scheme, etc. – Very expressive syntax • Not](https://reader033.fdocuments.us/reader033/viewer/2022050506/5f976376b7bd7135a851b715/html5/thumbnails/8.jpg)
Concurrency
• It is often convenient to model an entity’s behaviour/brain/state machine as an independent thread of execution
• Scripts provide a convenient mechanism to support this• Often built in language support
– E.g. Lua co-routines, UnrealScript “latent functions”• Script threads are co-operatively scheduled, so there are
fewer issues compared to pre-emptive OS-level threads– No locking primitives, race conditions, etc.– Better performance
• Cheap context switch
![Page 9: Scripting - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/585_W13/d104_scripting.pdf · 2013-01-03 · • Lisp, Scheme, etc. – Very expressive syntax • Not](https://reader033.fdocuments.us/reader033/viewer/2022050506/5f976376b7bd7135a851b715/html5/thumbnails/9.jpg)
Choice of Languages
• Lua– Widely used in the game industry– Designed for embedding– Byte-code interpreted– Dynamically typed– Very flexible tables (associative arrays) as the fundamental data
structure– Simple, clean syntax– Garbage collection– Not natively object-oriented, but can fake it effectively– Good performance (for a scripting language)
![Page 10: Scripting - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/585_W13/d104_scripting.pdf · 2013-01-03 · • Lisp, Scheme, etc. – Very expressive syntax • Not](https://reader033.fdocuments.us/reader033/viewer/2022050506/5f976376b7bd7135a851b715/html5/thumbnails/10.jpg)
Choice of Languages
• Java / C#– Basically, C++ with pointers removed, garbage collection added– Statically typed– Very rich standard libraries– Object oriented– Object reflection, serialization– Properties (C#)– Good performance
• Some safety benefit over C++, but not much gain in expressiveness
• Generally these languages are too close to C++ to offer huge benefits as a scripting language– A decent choice as an alternative to C++
![Page 11: Scripting - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/585_W13/d104_scripting.pdf · 2013-01-03 · • Lisp, Scheme, etc. – Very expressive syntax • Not](https://reader033.fdocuments.us/reader033/viewer/2022050506/5f976376b7bd7135a851b715/html5/thumbnails/11.jpg)
Choice of Languages
• Perl / Python / Ruby– All have been used in games at one time or another– Very different syntax, but similar feature sets– Rich standard libraries– Dynamically typed– Byte-code interpreted– Object oriented to one degree or another– Garbage collection– Generally pretty slow– Large memory footprint
• All have ambitions to be full-blown application languages– Fairly hostile to embedding in C++
• Optimised for text processing, which isn't a big concern for games
![Page 12: Scripting - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/585_W13/d104_scripting.pdf · 2013-01-03 · • Lisp, Scheme, etc. – Very expressive syntax • Not](https://reader033.fdocuments.us/reader033/viewer/2022050506/5f976376b7bd7135a851b715/html5/thumbnails/12.jpg)
Choice of Languages
• Lisp, Scheme, etc.– Very expressive syntax
• Not terribly readable, though– Dynamically typed– Compiled, or interpreted– Easy to write an interpreter for– Garbage collection– Decent performance (if you have a good implementation)– Doesn't play well with C++– Hard for people trained in imperative programming styles to learn
• Full-blown Lisp works better as an application language– No support on consoles though– Lots of implementations out there
![Page 13: Scripting - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/585_W13/d104_scripting.pdf · 2013-01-03 · • Lisp, Scheme, etc. – Very expressive syntax • Not](https://reader033.fdocuments.us/reader033/viewer/2022050506/5f976376b7bd7135a851b715/html5/thumbnails/13.jpg)
Choice of Languages
• PHP, JavaScript, Small, Squirrel, Io, interpreted C (LCC), Tcl, etc.– There are a lot of choices out there
• Roll your own?– Not for this project, but...– Get exactly the mix of features you want– Seamless C++ bindings, state-machines, concurrency, event
handling, resource management– Write your own native-code compiler!– UnrealScript: Java with game-oriented features– GOAL: Great until your Lisp guy retires...
• Writing a language is not a task to take lightly– Be careful, or you will end up with Perl, but not as useful
![Page 14: Scripting - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/585_W13/d104_scripting.pdf · 2013-01-03 · • Lisp, Scheme, etc. – Very expressive syntax • Not](https://reader033.fdocuments.us/reader033/viewer/2022050506/5f976376b7bd7135a851b715/html5/thumbnails/14.jpg)
Binding to C++
• Simple attribute binding• C-function binding• Object reflection
![Page 15: Scripting - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/585_W13/d104_scripting.pdf · 2013-01-03 · • Lisp, Scheme, etc. – Very expressive syntax • Not](https://reader033.fdocuments.us/reader033/viewer/2022050506/5f976376b7bd7135a851b715/html5/thumbnails/15.jpg)
How to Bind
• By hand– No
• Macros– Clunky, but proven
• Templates– boost::python– Hard to write, convenient to use
• IDL– Complex, but language independent
• Code parsers– SWIG– Complex, but convenient
• Symbol file parsers
![Page 16: Scripting - University of Calgary in Albertapages.cpsc.ucalgary.ca/~bdstephe/585_W13/d104_scripting.pdf · 2013-01-03 · • Lisp, Scheme, etc. – Very expressive syntax • Not](https://reader033.fdocuments.us/reader033/viewer/2022050506/5f976376b7bd7135a851b715/html5/thumbnails/16.jpg)
Summary
• It's worth using multi-language development to gain some productivity back that C++ takes away
• Scripting in a game can take on many forms and this influences language selection
• Lua seems to be the most popular choice currently• Custom languages are still popular, however