Author Topic: How to debug Python in RA2  (Read 1124 times)

Online apanx

How to debug Python in RA2
« on: March 07, 2018, 12:03:21 PM »
Enable AI debugging in AI\__init__.py. Replace plus.isDebugMode() with true.

Code: [Select]
class SuperAI(plus.AI):
    "Python base class of the C++ AI."
    name = "No Name"

    debugging = True #plus.isDebugMode()

Enable output of stdout and stderr to a log file by editing Scripts\Main.py
Replace this
Code: [Select]
class giScript_stdout:
    def write(self, s):
        giScript.debug_stdout(s)

class giScript_stderr:
    def __init__(self):
        self.count = 0
    def write(self, s):
        self.count += 1
        giScript.debug_stderr(s)
    def flush(self):
        pass
With this:
Code: [Select]
class giScript_stdout:
    def write(self, s):
        giScript.debug_stdout(s)
        output = file("debug.txt", "a")
        output.write(str(s))

class giScript_stderr:
    def __init__(self):
        self.count = 0
    def write(self, s):
        self.count += 1
        output = file("debug.txt", "a")
        output.write(str(s))
        giScript.debug_stderr(s)

    def flush(self):
        pass
Now all python errors will be logged to debug.txt

Add AIHelpers=1 to RA2.cfg to enable debugwindow for the console key (~)/key left to 1, above Tab.
Saves you from having to write plus.showFPS(0) in the Ctrl-F9 window.

All RA2 AI is created as objects in AI.running_ai, which means you can access other currently running AI object through this list.
The AI.SuperAI class is the parent for most AIs, it is a child of the plus.AI object, which is buried in the RA2 executable.

Arenas work mostly the same way, with the Arena.SuperArena being the parent, and a child of plus.Arena. The current arena is however accessed through Arenas.currentArena.

Also, lrn2python as it will make you understand how to do stuff instead of just doing cargo cult programming. Here is a guide https://www.learnpythonthehardway.org/


Offline Jaydee99

  • Waltuhweight
  • *
  • Posts: 1938
  • Rep: -23
  • :/
    • View Profile
    • Awards
  • See profile for gamer tags: Yes
Re: How to debug Python in RA2
« Reply #1 on: March 07, 2018, 01:28:39 PM »
This is cool but what is the benefit of it?