[Gcblue-commits] gcblue.com/script_tutorial buildunitmenu1.jpg,NONE,1.1 menu1.jpg,NONE,1.1 menu2.jpg
Status: Alpha
Brought to you by:
ddcforge
|
From: Dewitt C. <ddc...@us...> - 2004-06-20 21:23:51
|
Update of /cvsroot/gcblue/gcblue.com/script_tutorial In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26405/script_tutorial Added Files: buildunitmenu1.jpg menu1.jpg menu2.jpg script_directory.jpg script_exmenu.htm script_exmenu2.htm script_exmenu3.htm script_intro.htm script_location.htm Log Message: --- NEW FILE: script_exmenu3.htm --- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><!-- InstanceBegin template="/Templates/stut.dwt" codeOutsideHTMLIsLocked="false" --> <head> <!-- InstanceBeginEditable name="doctitle" --> <title>Untitled Document</title> <!-- InstanceEndEditable --> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <!-- InstanceBeginEditable name="head" --> <!-- InstanceEndEditable --> </head> <body> <p><strong><em><font size="+2" face="Arial, Helvetica, sans-serif"><img src="../images/cvbg_blue.jpg" width="613" height="92"></font></em></strong></p> <p><strong><em><font size="+2" face="Arial, Helvetica, sans-serif">GCB Scripting Tutorial</font></em></strong> </p> <hr> <!-- InstanceBeginEditable name="EditRegion3" --> <p><font face="Arial, Helvetica, sans-serif"><strong><em>Popup menu scripting (continued)</em></strong></font></p> <p><font face="Arial, Helvetica, sans-serif">The ReverseOurCourse script can be added to the Menu.py file, but to keep it with the other unit command scripts we will add it to the UnitCommands.py file. </font></p> <p><font face="Arial, Helvetica, sans-serif">Lets add it at the end after the ToggleRadars script. To reverse course, first we get the current heading of the platform, and then we set a new heading that is 180 degrees away. In script form this looks like this:</font></p> <pre><font face="Courier New, Courier, mono">def ReverseOurCourse(UI): current_heading = UI.GetHeading() # get current heading in degrees UI.SetHeading(current_heading + 180) # set new heading 180 deg away</font></pre> <p><font face="Arial, Helvetica, sans-serif">The UI parameter is a PlatformInterface object for the current hooked unit. It serves as the interface to the game engine script methods such as GetHeading and SetHeading. Menu items added with the AddItem method will always have command scripts of this form i.e. "def <command name>(UI):" . Any parameter name can be used for the PlatformInterface object as long as it is consistent throughout the body of the script function, e.g. </font></p> <pre><font face="Courier New, Courier, mono">def ReverseOurCourse(UnitInfo): current_heading = UnitInfo.GetHeading() # get current heading in degrees UnitInfo.SetHeading(current_heading + 180) # set new heading 180 deg away</font></pre> <p><font face="Arial, Helvetica, sans-serif">is also okay.</font></p> <p><font face="Arial, Helvetica, sans-serif">After saving the updated UnitCommands.py file, the "Reverse course" menu command should now work.</font></p> <p> </p> <pre> </pre> <p> </p> <p> </p> <!-- InstanceEndEditable --> <p> </p><hr> <p> </p> <table width="622" border="0" align="center"> <tr> <td align="center" bordercolor="#999999"><a href="script_intro.htm">Scripting intro</a> - <a href="script_location.htm">Script files</a> - Menu scripts <a href="script_exmenu.htm">1</a> <a href="script_exmenu2.htm">2</a> <a href="script_exmenu3.htm">3</a></td> </tr> <tr> <td align="center" bordercolor="#999999"> <hr> <font size="1" face="Arial, Helvetica, sans-serif">Copyright ©2002-2004 by Global Conflict Blue Team. All rights reserved.</font></td> </tr> <tr> <td width="622" align="center" bordercolor="#999999"> <div align="center"><font size="-2" face="Arial, Helvetica, sans-serif"><a href="mailto:de...@tw...">Contact us</a> </font></div></td> </tr> </table> <p> </p> </body> <!-- InstanceEnd --></html> --- NEW FILE: script_intro.htm --- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><!-- InstanceBegin template="/Templates/stut.dwt" codeOutsideHTMLIsLocked="false" --> <head> <!-- InstanceBeginEditable name="doctitle" --> <title>Untitled Document</title> <!-- InstanceEndEditable --> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <!-- InstanceBeginEditable name="head" --> <!-- InstanceEndEditable --> </head> <body> <p><strong><em><font size="+2" face="Arial, Helvetica, sans-serif"><img src="../images/cvbg_blue.jpg" width="613" height="92"></font></em></strong></p> <p><strong><em><font size="+2" face="Arial, Helvetica, sans-serif">GCB Scripting Tutorial</font></em></strong> </p> <hr> <!-- InstanceBeginEditable name="EditRegion3" --> <p><font face="Arial, Helvetica, sans-serif"><strong><em>GCB Scripting Overview</em></strong></font></p> <p><font face="Arial, Helvetica, sans-serif">Python (<a href="http://python.org">www.python.org</a>) is embedded into GCB for scripting using the <a href="http://www.boost.org/libs/python/doc/index.html">Boost Python</a> library. This allows text script files to be used and modified to control game behavior. Currently scripts are used for two functions:</font></p> <ul> <li> <font face="Arial, Helvetica, sans-serif">Interface definition - Customization of game menus and some interfaces.</font></li> <li><font face="Arial, Helvetica, sans-serif">AI behavior - Description of behavior of game units through a set of rules and doctrines</font></li> </ul> <p><font face="Arial, Helvetica, sans-serif">To learn more about Python and how to write scripts in Python see <a href="http://www.python.org/topics/learn/">http://www.python.org/topics/learn/</a> . </font></p> <p><font face="Arial, Helvetica, sans-serif">Script files can be opened and edited in any text editor; however, downloading and using the Python IDLE editor is recommended to make the job a little easier. The IDLE editor is included with the <a href="http://www.python.org/2.3.3/">Python release download</a>. Since GCB embeds Python within the application, the main Python binaries are not required.</font></p> <p> </p> <p><font face="Arial, Helvetica, sans-serif"><a href="script_location.htm">Next page >><em> </em></a></font></p> <p><em><br> </em></p> <p> <code><em><var><font size="-1"></font></var></em></code></p> <!-- InstanceEndEditable --> <p> </p><hr> <p> </p> <table width="622" border="0" align="center"> <tr> <td align="center" bordercolor="#999999"><a href="script_intro.htm">Scripting intro</a> - <a href="script_location.htm">Script files</a> - Menu scripts <a href="script_exmenu.htm">1</a> <a href="script_exmenu2.htm">2</a> <a href="script_exmenu3.htm">3</a></td> </tr> <tr> <td align="center" bordercolor="#999999"> <hr> <font size="1" face="Arial, Helvetica, sans-serif">Copyright ©2002-2004 by Global Conflict Blue Team. All rights reserved.</font></td> </tr> <tr> <td width="622" align="center" bordercolor="#999999"> <div align="center"><font size="-2" face="Arial, Helvetica, sans-serif"><a href="mailto:de...@tw...">Contact us</a> </font></div></td> </tr> </table> <p> </p> </body> <!-- InstanceEnd --></html> --- NEW FILE: script_directory.jpg --- (This appears to be a binary file; contents omitted.) --- NEW FILE: script_location.htm --- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><!-- InstanceBegin template="/Templates/stut.dwt" codeOutsideHTMLIsLocked="false" --> <head> <!-- InstanceBeginEditable name="doctitle" --> <title>Untitled Document</title> <!-- InstanceEndEditable --> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <!-- InstanceBeginEditable name="head" --> <!-- InstanceEndEditable --> </head> <body> <p><strong><em><font size="+2" face="Arial, Helvetica, sans-serif"><img src="../images/cvbg_blue.jpg" width="613" height="92"></font></em></strong></p> <p><strong><em><font size="+2" face="Arial, Helvetica, sans-serif">GCB Scripting Tutorial</font></em></strong> </p> <hr> <!-- InstanceBeginEditable name="EditRegion3" --> <p><font face="Arial, Helvetica, sans-serif"><strong><em>Script location</em></strong></font></p> <p><font face="Arial, Helvetica, sans-serif">Scripts are located under the scripts directory. In the directory there will be .py and .pyc files. The .py files are the text script files. The .pyc files are compiled versions of the .py file. Python automatically generates and updates these. </font></p> <p><font face="Arial, Helvetica, sans-serif">For GCB 0.5.1 the script directory looks like this:</font></p> <p align="center"><img src="script_directory.jpg" width="464" height="314"></p> <p align="left"><font face="Arial, Helvetica, sans-serif">The script files are summarized in this table:</font></p> <table width="50%" border="0" align="center"> <tr> <td width="44%"><em><strong><font face="Arial, Helvetica, sans-serif">Script file</font></strong></em></td> <td width="56%"><em><strong><font face="Arial, Helvetica, sans-serif">Function</font></strong></em></td> </tr> <tr> <td><font face="Arial, Helvetica, sans-serif">AI.py</font></td> <td><font face="Arial, Helvetica, sans-serif">AI scripts</font></td> </tr> <tr> <td><font face="Arial, Helvetica, sans-serif">FlightPort.py</font></td> <td><font face="Arial, Helvetica, sans-serif">Flight deck panel interface definition</font></td> </tr> <tr> <td><font face="Arial, Helvetica, sans-serif">HotKey.py</font></td> <td><font face="Arial, Helvetica, sans-serif">HotKey definitions</font></td> </tr> <tr> <td><font face="Arial, Helvetica, sans-serif">Menu.py</font></td> <td><font face="Arial, Helvetica, sans-serif">Popup menu definition</font></td> </tr> <tr> <td><font face="Arial, Helvetica, sans-serif">UnitCommands.py</font></td> <td><font face="Arial, Helvetica, sans-serif">Unit level commands</font></td> </tr> <tr> <td> </td> <td> </td> </tr> </table> <p> </p> <p><font face="Arial, Helvetica, sans-serif">To help keep the script code readable, script files should be kept to a reasonable size. Additional script files can be created and loaded via the <em>Import</em> command.</font></p> <p> </p> <p><font face="Arial, Helvetica, sans-serif"><a href="script_exmenu.htm">Next page >><em> </em></a></font></p> <p> <code><em><var><font size="-1"></font></var></em></code></p> <!-- InstanceEndEditable --> <p> </p><hr> <p> </p> <table width="622" border="0" align="center"> <tr> <td align="center" bordercolor="#999999"><a href="script_intro.htm">Scripting intro</a> - <a href="script_location.htm">Script files</a> - Menu scripts <a href="script_exmenu.htm">1</a> <a href="script_exmenu2.htm">2</a> <a href="script_exmenu3.htm">3</a></td> </tr> <tr> <td align="center" bordercolor="#999999"> <hr> <font size="1" face="Arial, Helvetica, sans-serif">Copyright ©2002-2004 by Global Conflict Blue Team. All rights reserved.</font></td> </tr> <tr> <td width="622" align="center" bordercolor="#999999"> <div align="center"><font size="-2" face="Arial, Helvetica, sans-serif"><a href="mailto:de...@tw...">Contact us</a> </font></div></td> </tr> </table> <p> </p> </body> <!-- InstanceEnd --></html> --- NEW FILE: buildunitmenu1.jpg --- (This appears to be a binary file; contents omitted.) --- NEW FILE: menu2.jpg --- (This appears to be a binary file; contents omitted.) --- NEW FILE: menu1.jpg --- (This appears to be a binary file; contents omitted.) --- NEW FILE: script_exmenu2.htm --- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><!-- InstanceBegin template="/Templates/stut.dwt" codeOutsideHTMLIsLocked="false" --> <head> <!-- InstanceBeginEditable name="doctitle" --> <title>Untitled Document</title> <!-- InstanceEndEditable --> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <!-- InstanceBeginEditable name="head" --> <!-- InstanceEndEditable --> </head> <body> <p><strong><em><font size="+2" face="Arial, Helvetica, sans-serif"><img src="../images/cvbg_blue.jpg" width="613" height="92"></font></em></strong></p> <p><strong><em><font size="+2" face="Arial, Helvetica, sans-serif">GCB Scripting Tutorial</font></em></strong> </p> <hr> <!-- InstanceBeginEditable name="EditRegion3" --> <p><font face="Arial, Helvetica, sans-serif"><strong><em>Popup menu scripting (continued)</em></strong></font></p> <p><font face="Arial, Helvetica, sans-serif">This script produces a popup that looks like this (SurfAir.py scenario with aircraft hooked):</font></p> <p><img src="menu1.jpg" width="199" height="213"></p> <p><font face="Arial, Helvetica, sans-serif"><em>Example 1: Adding a menu command</em></font></p> <p><font face="Arial, Helvetica, sans-serif">As an example, let's add a new menu command. We will use the UnitMenu.AddItem function to do this. This function needs two parameters. The first parameter is the caption that is displayed in the menu. The second is the name of a script function to execute when the menu item is clicked.</font></p> <p><font face="Arial, Helvetica, sans-serif">Let's add an item that will reverse the course of the platform. This can be done with a command like this:</font></p> <p align="center"><font face="Arial, Helvetica, sans-serif"> UnitMenu.AddItem('Reverse course', 'ReverseOurCourse') </font></p> <p align="left"><font face="Arial, Helvetica, sans-serif">Let's add the command to after the 'Change heading' item in the BuildUnitMenu script in Menu.py, and then save the file. The modified script looks like this:</font></p> <pre align="left"><font face="Courier New, Courier, mono">###### Unit menu scripts #####</font><font size="-1" face="Courier New, Courier, mono"> </font><font face="Courier New, Courier, mono">def BuildUnitMenu(UnitMenu, UnitInfo): UnitMenu.Clear() UnitMenu.AddItemUI('Change heading','SetHeading','Heading') UnitMenu.AddItem('Reverse course', 'ReverseOurCourse') # new command # Speed submenu UnitMenu.AddItem('Set speed','') UnitMenu.BeginSubMenu() UnitMenu.AddItem('50%','Speed50') UnitMenu.AddItem('80%','Speed80') UnitMenu.AddItem('Max','Speed100') UnitMenu.EndSubMenu()</font></pre> <p>The "# new command" at the end of the line is a comment. Everything after the "#" is ignored by Python.</p> <p><font face="Arial, Helvetica, sans-serif">Restart the game, hook a platform, and right-click to bring up the new popup menu. The new menu looks like this:</font></p> <p><font face="Arial, Helvetica, sans-serif"><img src="menu2.jpg" width="228" height="250"></font></p> <p><font face="Arial, Helvetica, sans-serif">Click the "Reverse course" item. It should produce a ProcessCommand exception in the user console. This is because we have not defined the ReverseOurCourse script yet! </font></p> <p> </p> <p><font face="Arial, Helvetica, sans-serif"><a href="script_exmenu3.htm">Next page >><em> </em></a></font></p> <p> </p> <!-- InstanceEndEditable --> <p> </p><hr> <p> </p> <table width="622" border="0" align="center"> <tr> <td align="center" bordercolor="#999999"><a href="script_intro.htm">Scripting intro</a> - <a href="script_location.htm">Script files</a> - Menu scripts <a href="script_exmenu.htm">1</a> <a href="script_exmenu2.htm">2</a> <a href="script_exmenu3.htm">3</a></td> </tr> <tr> <td align="center" bordercolor="#999999"> <hr> <font size="1" face="Arial, Helvetica, sans-serif">Copyright ©2002-2004 by Global Conflict Blue Team. All rights reserved.</font></td> </tr> <tr> <td width="622" align="center" bordercolor="#999999"> <div align="center"><font size="-2" face="Arial, Helvetica, sans-serif"><a href="mailto:de...@tw...">Contact us</a> </font></div></td> </tr> </table> <p> </p> </body> <!-- InstanceEnd --></html> --- NEW FILE: script_exmenu.htm --- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><!-- InstanceBegin template="/Templates/stut.dwt" codeOutsideHTMLIsLocked="false" --> <head> <!-- InstanceBeginEditable name="doctitle" --> <title>Untitled Document</title> <!-- InstanceEndEditable --> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <!-- InstanceBeginEditable name="head" --> <!-- InstanceEndEditable --> </head> <body> <p><strong><em><font size="+2" face="Arial, Helvetica, sans-serif"><img src="../images/cvbg_blue.jpg" width="613" height="92"></font></em></strong></p> <p><strong><em><font size="+2" face="Arial, Helvetica, sans-serif">GCB Scripting Tutorial</font></em></strong> </p> <hr> <!-- InstanceBeginEditable name="EditRegion3" --> <p><font face="Arial, Helvetica, sans-serif"><strong><em>Popup menu scripting</em></strong></font></p> <p><font face="Arial, Helvetica, sans-serif">The Menu.py file is the main file that defines the popup menu. The interface is (partially) documented in the doxygen doc pages. See <a href="../doxydoc/gcb_05_docs/classScriptInterface_1_1tcMenuInterface.html">menu interface class documentation</a>.</font></p> <p> </p> <p><font face="Arial, Helvetica, sans-serif">The platform right-click popup menu is defined in the BuildUnitMenu script. After each right-click, this function is called by the game engine to construct the unit menu. This allows the menu to change based on the platform type and state.</font></p> <p> </p> <p><font face="Arial, Helvetica, sans-serif">The GCB 0.5.1 BuildUnitMenu looks like this: (<a href="buildunitmenu1.jpg">what it looks like with the IDLE editor</a>)</font></p> <pre align="left"><font face="Courier New, Courier, mono">###### Unit menu scripts ##### def BuildUnitMenu(UnitMenu, UnitInfo): UnitMenu.Clear() UnitMenu.AddItemUI('Change heading','SetHeading','Heading') # Speed submenu UnitMenu.AddItem('Set speed','') UnitMenu.BeginSubMenu() UnitMenu.AddItem('50%','Speed50') UnitMenu.AddItem('80%','Speed80') UnitMenu.AddItem('Max','Speed100') UnitMenu.EndSubMenu() # Target submenu UnitMenu.AddItem('Target','') UnitMenu.BeginSubMenu() UnitMenu.AddItemUI('Select target','SelectTarget','Target') UnitMenu.AddItem('Clear target','ClearTarget') UnitMenu.EndSubMenu()</font></pre> <pre><font face="Courier New, Courier, mono"> # Launcher BuildLaunchMenu(UnitMenu, UnitInfo)</font></pre> <pre><font face="Courier New, Courier, mono"> # Orders test UnitMenu.AddItem('Orders','') UnitMenu.BeginSubMenu() UnitMenu.AddItem('Add patrol order','AddPatrolOrder') UnitMenu.AddItemUI('Add waypoint order', 'AddWaypointOrder', 'Datum') UnitMenu.AddItem('Clear orders','ClearOrders') UnitMenu.EndSubMenu() if (UnitInfo.IsAir()): # Altitude submenu UnitMenu.AddItem('Set altitude','') UnitMenu.BeginSubMenu() UnitMenu.AddItem('High','AltitudeHigh') UnitMenu.AddItem('Medium','AltitudeMedium') UnitMenu.AddItem('Low','AltitudeLow') UnitMenu.AddItem('Very low','AltitudeVeryLow') UnitMenu.EndSubMenu() UnitMenu.AddItemUI('Land at','AddLandingOrder', 'Target')</font></pre> <pre><font face="Courier New, Courier, mono"> # Flight port panel if available if (UnitInfo.HasFlightPort()): UnitMenu.AddItem('Flight deck', 'ShowFlightPanel')</font><font size="-1" face="Courier New, Courier, mono"> </font></pre> <p><font face="Arial, Helvetica, sans-serif"><a href="script_exmenu2.htm">Next page >><em> </em></a></font></p> <p align="center"> </p> <!-- InstanceEndEditable --> <p> </p><hr> <p> </p> <table width="622" border="0" align="center"> <tr> <td align="center" bordercolor="#999999"><a href="script_intro.htm">Scripting intro</a> - <a href="script_location.htm">Script files</a> - Menu scripts <a href="script_exmenu.htm">1</a> <a href="script_exmenu2.htm">2</a> <a href="script_exmenu3.htm">3</a></td> </tr> <tr> <td align="center" bordercolor="#999999"> <hr> <font size="1" face="Arial, Helvetica, sans-serif">Copyright ©2002-2004 by Global Conflict Blue Team. All rights reserved.</font></td> </tr> <tr> <td width="622" align="center" bordercolor="#999999"> <div align="center"><font size="-2" face="Arial, Helvetica, sans-serif"><a href="mailto:de...@tw...">Contact us</a> </font></div></td> </tr> </table> <p> </p> </body> <!-- InstanceEnd --></html> |