project reality header
Go Back   Project Reality Forums > PR:BF2 Mod Forums > PR:BF2 Community Factions > Coding
21 Sep 2024, 00:00:00 (PRT)
Register Developer Blogs Members List Search Quick Links

Reply
 
Thread Tools Display Modes
Old 2021-10-18, 20:36   #1
defaultUser
Question Starting A Python Script?

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:
import helloworld

def init():
	helloworld.init()
	
def deinit():
	helloworld.deinit()
helloworld.py
Code:
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.
defaultUser is offline Reply With Quote
Old 2021-10-19, 07:46   #2
[R-DEV]​AlonTavor
PR:BF2 Developer
Supporting Member
PR Server License Administrator
Default Re: Starting A Python Script?

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:
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.
AlonTavor is offline Reply With Quote
Reply


Tags
modding, python

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



All times are GMT. The time now is 11:39.