Changes between Version 2 and Version 3 of documentation/tutorials/PythonEventSystem

Show
Ignore:
Author:
lloydw (IP: 202.68.89.228)
Timestamp:
01/09/08 11:20:58 (10 years ago)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • documentation/tutorials/PythonEventSystem

    v2 v3  
    5353PythonInterface::Shutdown should be called after you've released all contexts but before you call Rocket::Shutdown(). This ensures all python objects are released before rocket does its final cleanup. 
    5454 
     55At this point you'll need to add the relevant Python and Boost::Python build paths to your project and then compile and link your project. 
     56 
    5557== Step 2: Replacing the Event System == 
    5658 
    57 We can now completely remove the exiting event system from ''RocketInvaders'' as the Python bindings will do all the event management for us. We will however need some way of starting invaders from script. I suggest you do this with an ''autoexec.py'' script, that would look something like this: 
     59We can now completely remove the exiting event system from ''RocketInvaders'' as the Python bindings will do all the EventManagement for us. Remove all source and header files that begin with Event. You'll also need to comment out some code in GameElement.cpp and Game.cpp that call the EventManager directly. We'll get back to those later. 
     60 
     61Also remove all the EventManager initialisation from main.cpp as we'll replace it with a new Python script. I suggest you name it ''autoexec.py'' and place it in a ''python'' subfolder. It should look something like this: 
    5862 
    5963{{{ 
    7377    if (!module) 
    7478    { 
    75         PrintError(); 
     79        PyErr_Print(); 
    7680        return false; 
    7781    } 
    8690}}} 
    8791 
    88 At this point the ''RocketInvaders'' will now run, however you'll get a nasty script error as Python attempts to execute the ''onload'' event in mainmenu.rml. Update the ''onload'' and ''onunload'' events to use Python script which will correctly display and hide the logo. 
     92At this point the ''RocketInvaders'' will now run, however you'll get a script import error because ''autoexec'' cannot be found. To fix this we'll need to add the ''python'' folder to our Python search path. 
     93 
     94''NOTE: On Windows this error will go to stdout, which will not be visible. I suggest you grab a copy of DoAllocConsole() from the pyinvaders sample and drop it into your project. Call DoAllocConsole() at the start of your main function and you'll get a console for reading python error messages.'' 
     95 
     96My PythonInterface::Initialise now looks like this: 
     97{{{ 
     98bool PythonInterface::Initialise() 
     99
     100        // Initialise Python. 
     101        Py_Initialize(); 
     102 
     103        // Setup the Python search path. 
     104        const char* python_path = Py_GetPath(); 
     105        char buffer[1024]; 
     106        snprintf(buffer, 1024, "../samples/invaders/python;%s", python_path); 
     107        buffer[1023] = '\0'; 
     108        PySys_SetPath(buffer); 
     109 
     110        // Import Rocket. 
     111        if (!Import("rocket")) 
     112                return false; 
     113 
     114        return true; 
     115
     116}}} 
     117 
     118This will get us further, you should see the main menu load, however you'll notice a python error on your console when Python attempts to execute the old ''onload'' event in mainmenu.rml. Update the ''onload'' and ''onunload'' events to use Python script which will correctly display and hide the logo. 
    89119{{{ 
    90120<body template="window" onload="document.context.LoadDocument('data/logo.rml').Show()" onunload="document.context.logo.Close()">