Jump to content
Official BF Editor Forums

Beginner Tutorial - How To Use Visual Checkpoints To Track Down Bugs

Recommended Posts

When I first started editing Python code in Battlefield, one of the

dang blasted most frustrating things was that there was no error output

or error console at all. If there was any bug in your code at all, the

script would simply die, and leave you clueless. Did my changes run,

did they break, where is the bug, and so on... it was frustrating as all

hell. It was like working with a bloody black box. You can no

way to get output to you, of what was going on. It turned out, the

installation of modmanager I had done early on, was redirecting all my

print statements into a modmanager_debug.log file, but I didn't know that

at the time.

So I began to code DEFENSIVELY, and I mean, REALLY DEFENSIVELY. What

I mean by that, was one little change at a time, on one line. If the

change broke something, I would know exactly where it was, because it

was the last thing I changed.

You can imagine, how frustrating, and painfully slow that was. Start

the game, log in, look around, see if it crashes the client, etc. In

many ways, after 5 years of working with this system, I still have to

code very defensively. But anyway...

So one of the first things I did, was try and figure out, how to get

some kind of output inside the game, of notices, of where and how far

my code had gotten... to the chat console, when I was laying down

large swaths of new fresh (and just by nature, buggy) code.

This beginner tutorial will show you how to do that, to see in

the chat text area, how far your script has gotten

# Our Test of Variables Output in Game - How to Debug & Use Visual Checkpoints
# Debugging tricks to use, so you can see what your code is doing in the game
# by CHOPPERGIRL <choppergirl@air-war.org>  http://choppergirl.air-war.org

# One of the problems with working with BF2, is you can't see what's going
# on, as there is no error output.  So you have to code very defensively,
# and when you are working on something that is broken or you just wrote,
# write lots of notices in game that your script made it that far, and
# what are the contents of the variables you are working with

import host

host.rcon_invoke('game.sayall "SCRIPT STARTED: Our Test of Variables Output in Game"')

# print out the the game reaches our first check point
host.rcon_invoke('game.sayall "checkpoint 1 reached"')

string = "hello"

host.rcon_invoke('game.sayall "A string is: %s"' %(string))

host.rcon_invoke('game.sayall "checkpoint 2 reached"')

variable = 42

host.rcon_invoke('game.sayall "A number is: %d"' %(variable))

host.rcon_invoke('game.sayall "checkpoint 2 reached"')


	host.rcon_invoke('echo "This is output to the Console"' ) 
	host.rcon_invoke('game.sayall "checkpoint 3 reached"')

	print "This may be also output to the Console"
	host.rcon_invoke('game.sayall "checkpoint 4 reached"')

host.rcon_invoke('game.sayall "checkpoint 5 reached"')

host.rcon_invoke('game.sayall "SCRIPT ENDED: Our Test of Variables Output in Game"')

# When you are debugging, and you have your script running perfectly
# you can go back and remove these checkpoint notices and variable printouts
# or better, do like I do and just comment them out in the script, like this:
# host.rcon_invoke('game.sayall "checkpoint 6 reached"')

If you were to run my sample script, you would see, this in the game:

SCRIPT STARTED: Our Test of Variables Output in Game

checkpoint 1 reached

But nothing else, no further check points? Like, 2, 3,4,5...

Why? What's going on? Because the interpreter died on the script somewhere between

checkpoint 1 and 2.

Since there are only two statements between those checkpoints, it

should be easy to track down the error....

And we find it:

host.rcon_invoke('game.sayall "A string is: %s" %(string)')
should be (not the relocating of the last single quotation mark, that was in the wrong place above:

host.rcon_invoke('game.sayall "A string is: %s"' %(string))
Realizing our error, we can then look down in the code to the next line, and see we did

the same thing again with the last single quote in the wrong place:

host.rcon_invoke('game.sayall "A number is: %d" %(variable)')
should be:

host.rcon_invoke('game.sayall "A number is: %d"' %(variable))
You would of not seen this when writing the code, but using lots and lots of

checkpoints peppered all in your code, makes tracking down these bugs far easier,

because you can locate in the game right down to the line how far your script gets

before it dies.

If the "print" command outputs messages to your server console, this is even

easier to use than seeing these inside the game... My prints are redirected though

into modmananger_debug.log, so I don't use them so much, instead using

host.rcon_invoke('game.sayall "Message"' )

If I want to actually echo to the console since my "prints" are redirected by modmanager,

I have to use this funny work around (which you may also have to use if you have modmanager installed):

host.rcon_invoke('echo "Message"' )

Edited by cobrachoppergirl

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...