game 1024: advanced game programming lesson 5: scripting your code away by kain shin

13
GAME 1024: Advanced Game Programming Lesson 5: Scripting Your Code Away By Kain Shin

Upload: cecilia-stone

Post on 25-Dec-2015

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: GAME 1024: Advanced Game Programming Lesson 5: Scripting Your Code Away By Kain Shin

GAME 1024: Advanced Game ProgrammingLesson 5: Scripting Your Code Away

By Kain Shin

Page 2: GAME 1024: Advanced Game Programming Lesson 5: Scripting Your Code Away By Kain Shin

Quiz1. What’s your full name

2. What can a “call stack” tell you?

3. What can a “watch window” tell you?

4. How do you set a breakpoint in your IDE?

5. How do you step the instruction pointer in your IDE?

6. How do you set the instruction pointer in your IDE?

7. Name a situation where you would want to use an “assert”

Page 3: GAME 1024: Advanced Game Programming Lesson 5: Scripting Your Code Away By Kain Shin

Bonus Topic: Common Data Structures Array – Sequence of elements occupying adjacent positions in memory (example:

std::vector) Pros: Fast Random Access. Cons: Expensive to expand/contract Use when adds/removes are rare

Linked List – Elements linked by pointers (example: std::list) Pros: Trivial to expand/contract Cons: Slow Random Access. Designed to be iterated in a for loop Use when you need flexibility in a container’s size.

Associative Containers – Elements are paired with a key value for the purpose of being sorted by keys Binary Search Tree - std::map (Red-Black Tree) Hash Map – Array of Linked Lists

Bonus Homework: Look Up Big O Notation. ACC should be teaching you this, but just in case, use google in addition to clicking here:http://www.fredosaurus.com/notes-cpp/algorithms/big-oh/bigoh-examples.html

Page 4: GAME 1024: Advanced Game Programming Lesson 5: Scripting Your Code Away By Kain Shin

What Is Scripting? Designer’s Definition: Scripting is game functionality that allows non-

programmers to associate a series of game actions to one or more game conditions

Programmer’s Definition: Scripting is a custom compiler within the application that dynamically associates words to function pointers (such as script commands) as it interprets asset files written outside of the exe codebase

Kain’s Definition: Scripting is a programming language written with the intention of being easier to use than the language it was written in (i.e. C/C++)

Advanced scripting modules, such as Python and Lua, maintain a persistent state. States allow permanent changes to a script’s table of existing variables and their current values. A function is treated like an object with a value. So what this means is that persistent states let you declare variables, set variables, and bind functions on the fly within the scope of that maintained script state.

Page 5: GAME 1024: Advanced Game Programming Lesson 5: Scripting Your Code Away By Kain Shin

Why Script? Let creative designers SAFELY craft “fun” into a player experience

instead of making expensive programmers hard code the “fun” When gameflow is controlled by assets instead of being part of the EXE,

then those assets can be… separately managed so that not all scipts are loaded for every level of

the game (taking up RAM) updated as new content (i.e. expansion packs)

Fast iteration for creatives on the team Possible user-created content (i.e. Neverwinter Nights modules) Forces you to organize code functionality more cleanly by separating

gameplay functionality into flexible and reuseable blocks of code

Page 6: GAME 1024: Advanced Game Programming Lesson 5: Scripting Your Code Away By Kain Shin

Drawbacks of Scripting Performance – Scripting causes an extra layer of abstraction

for code execution Integration/Tools Support Required – A programmer has to

spend the time writing and supporting the tools for scripting. Hopefully, this tools support will save development time in the long run

Hard to debug – C++ IDEs have had a lot of time to evolve to make life easier for programmers. Tools you write for scripting will not be so lucky.

Page 7: GAME 1024: Advanced Game Programming Lesson 5: Scripting Your Code Away By Kain Shin

Class Mental Exercise If you were to write your own scripting

system, what would your system need to do? How does a file parser work? How does one declare and set variables?s How would your custom C/C++ functions get

dynamically binded to the script’s dictionary of legal words?

Page 8: GAME 1024: Advanced Game Programming Lesson 5: Scripting Your Code Away By Kain Shin

Scripting Language Choices Write your own – just make sure that you write it for designers and not

other programmers Python – Uses a syntax similar to C, been around for a while, has a lot of

available tools, samples, and documentation, but has a large memory footprint compared to…

Lua – Has a small memory footprint. Syntax is simple, and it is specifically designed to be integrated into C/C++ applications.

There are other scripting language choices, too… such as Ruby. The dominant scripting languages used in the industry are Lua and Python

For this class, we will be integrating a variant of Lua called LuaPlus into your engine

Page 9: GAME 1024: Advanced Game Programming Lesson 5: Scripting Your Code Away By Kain Shin

LuaPlus LuaPlus is somebody’s mod of Lua intended to make Lua easier to use Get LuaPlus here:

http://luaplus.org A LuaPlus Overview exists here:

http://wwhiz.com/LuaPlus/LuaPlus.html Learn about Lua syntax here:

http://www.lua.org/manual Lua is a very complex scripting language. For this class, Kain does

not care if you learn Lua. Kain only cares that you can integrate Lua into your engine and expose your C++ code to the script

Page 10: GAME 1024: Advanced Game Programming Lesson 5: Scripting Your Code Away By Kain Shin

LuaPlus and the Class Project Scripting: Hook your own script functions to the game so that a designer can

specify what should happen when the user clicks on an actor. A designer might want the actor to either “become possessed by WASD” and/or “become evil”. So you will have one script file that gets called by code called “OnSelect.lua” and two other script functions that can be used by a designer called “BecomeEvil” and “BecomePossessed”.

Integration of script into your engine means two things:1. Make your code execute a lua file – “When user clicks on an actor, execute

the script file, OnSelect.lua”2. Expose one or more of your C++ functions to script for designers –

“BecomeEvil”, “BecomePossessed” written in C++ on the code side, but binded to Lua as functions that can exist within a lua script file

Expectations: You are expected to research the documentation and source code of LuaPlus

on your own to figure out what you need to do for the RegisterFunction and ExecuteFile methods that you will be writing

You are expected to place your script manager in the engine and not have it execute any game-specific lines of code

Page 11: GAME 1024: Advanced Game Programming Lesson 5: Scripting Your Code Away By Kain Shin

Class cScriptManager You will have a script manager class in your project that encompasses all script

functionality in the game Expose static C/C++ functions to script:

void cScriptManager::RegisterFunction(char* funcName, ??? pFuncPointer)

Executing a script file:void cScriptManager::ExecuteFile(char* fileName)

Hints: Your script manager class can have a ‘LuaStateOwner’ object as a member

variable, and you can think of your script manager as a wrapper around a LuaState object. LuaStateOwner is basically just a smart pointer around a LuaState object

You will definitely be doing things to the global LuaObject that you get from LuaState::GetGlobals() inside your RegisterFunction and ExecuteFile methods

Page 12: GAME 1024: Advanced Game Programming Lesson 5: Scripting Your Code Away By Kain Shin

Other Cool Things You Can Do With Scripts There is a lot more to scripting than I will make you do for the class project…

Direct Event Handling: Make your script manager a listener of events with a map that associates eventIDs to script files; so that events can trigger scripts directly and event data can be read and acted upon by the triggered script. An example would be a script that fires whenever a key is pressed, but reads the key from the event and does different things depending on the key that was pressed

Acknowledging the “Me” Object: Associating scripts to specific actors in the world, instead of making them general-scoped, so that you can write scripts that do things like “When I am clicked, then make me turn evil” Hint: You can integrate this concept into the class project, but I won’t count off if you hack a

workaround to just use the current mouse position to specify the intended target of “BecomePossessed” or “BecomeEvil”

Non-atomic Execution: Implement commands that can pause execution of a script, such as “Wait(float seconds)” so that you can write script that does things like “Spawn a new enemy every 5 seconds”

Human-Proofing: Instead of error-prone humans typing in script by hand, there could be a GUI layer above the scripting layer so that humans can press buttons, fill in blanks, and/or drag icons to write complex script under the hood without worrying about the strict syntax of a scripting language

Page 13: GAME 1024: Advanced Game Programming Lesson 5: Scripting Your Code Away By Kain Shin

Final Thoughts I am not teaching you how to write Lua script in this class

because learning the specifics of one scripting language is not as useful as learning the over-arching concepts behind scripting.

Job interviewers do not care if your school did not teach you something. They only care if you know what they need you to know.

Don’t just sit there and wait for somebody to teach you what you don’t know. You are your best teacher! I can only make you aware of what you don’t know. The rest is up to you.