Starting A Python Script?

Post Reply
defaultUser
Posts: 1
Joined: 2021-10-18 19:51

Starting A Python Script?

Post by defaultUser »

Greetings, not sure if its the right place to ask since this is for 'vanilla' bf2 and not PR,
but I really don't know where to ask else and Im trying get a 'Hello World' python script done.

First I created a folder inside the mods/bf2mod/python/game/gamemodes/helloworld
threw '__init__.py' and 'helloworld.py' into it,
then edited the 'gpm_coop.py' in the parent folder 'gamemodes'
added 2 lines to it, 'import helloworld' under line 'from bf2 import g_debug'
and 'helloworld.init()' at the top in the 'def init()' function

__init__.py

Code: Select all

import helloworld

def init():
	helloworld.init()
	
def deinit():
	helloworld.deinit()
helloworld.py

Code: Select all

import host
import bf2
from bf2 import g_debug

def init():
	host.registerGameStatusHandler(gameStatusChanged)
	global g_debug
	g_debug = 1
	if g_debug: 
		print "helloworld.py initialized"

def deinit():
	host.unregisterGameStatusHandler(gameStatusChanged)
	print "testrotz.py deinitialized"
	
def gameStatusChanged(status):
	if status == bf2.GameStatus.Playing:
		host.rcon_invoke("echo HelloWorld")
Now if I start a game nothing, in the console will show up, first I thought it was a
'rcon' error since I wasnt logged in as an admin but that wasn't the case,
I suspect my script wont even load since my game wont crash on load if
I place a wrong Tabulator in the script for example.

What did I missed? Thanks in advance.
AlonTavor
PR:BF2 Developer
Posts: 2991
Joined: 2009-08-10 18:58

Re: Starting A Python Script?

Post by AlonTavor »

For printing and debugging, there's some code in ROOT/python/__init__.py that redirects all stdout, so if you haven't disabled that print wont work. Regardless, echo always works unless you have unprintable characters in text.

We use it like this to work with spaces.

Code: Select all

host.rcon_invoke('echo "%s"' % text)

I recommend using the dedicated server to test. It launches much faster. Then you can quickly figure out what gets run and what doesn't.
Post Reply

Return to “Coding”