[HELP] BF2 Python related questions

Making or wanting help making your own asset? Check in here
Post Reply
UTurista
PR:BF2 Developer
Posts: 985
Joined: 2011-06-14 14:13

[HELP] BF2 Python related questions

Post by UTurista »

I didn't find a thread like this and I'm predicting I'm going to do a lots of questions regarding this.

And to start off, a simple "Hello World", how can I achieve this?
I changed the" __init__.py" in phyton/game

Code: Select all

import realityinit
import helloWorld
realityinit.init(False)
helloWorld.init()
Added my "helloWorld.py" module

Code: Select all

import host
def init():
	host.rcon_invoke("game.sayAll Hello World")
But whener I try it in-game, nothing appears and in the console I get "rcon: you can only invoke login" (or something like that)

BF2.timer, how does this works?
I created the following module

Code: Select all

import bf2
def init():
	bf2.Timer(battleTimmer, 5, 1)

def battleTimmer():
	 host.rcon_invoke("game.sayAll Timmer")
	 for p in bf2.playerManager.getPlayers():
		p.score.kills += 1
First I get a crash, why any ideias? And second does this means every 5(seconds/minutes/days/years?) everyone gets one kill?

How do I lower the health of a player?

More to be asked, but for the moment these are fine.
Last edited by piepieonline on 2015-05-31 11:46, edited 1 time in total.
Image


Dont question the wikipedia! Just because it reports different things on different languages does not make it unreliable source!
AfterDune
Retired PR Developer
Posts: 17094
Joined: 2007-02-08 07:19

Re: [HELP] BF2 Phyton related questions

Post by AfterDune »

Your hello world is displayed upon mapload, not when you're in-game. You could try to run a dedicated server and keep an eye out on the consol.
Btw, unsure if game.sayAll displays it in the console - perhaps it's the "print" command that does that.


If you're testing your Python scripts, always run in windowed mode. Only in windowed mode will you be able to see the error messages. If you've got a Python error, it should be displayed there.
Image
Eskil_swe
PR:BF2 QA Tester
Posts: 64
Joined: 2012-06-05 13:42

Re: [HELP] BF2 Phyton related questions

Post by Eskil_swe »

host.rcon_invoke('echo "Hello World!"') would show in the server console
and the timer needs the "data" item other wise it will not work.

Code: Select all

import host
import bf2
import bf2.Timer

def debug(e):
	host.rcon_invoke('echo "DEBUG: %s"' % e)		# Displays in server console
	host.rcon_invoke('game.sayall "DEBUG: %s"' % e)		# Displays ingame

# Init
def init():
	host.registerHandler('PlayerSpawn', OnPlayerSpawn)
	host.registerHandler('PlayerDeath', OnPlayerDeath)
	host.rcon_invoke('echo "Loaded!"')


# testTimer
def TestTimer(data):
	try:
		host.rcon_invoke('game.sayall "Hello World!"')

	except Exception, e:
		debug(e) # Error msg


# OnPlayerSpawn
def OnPlayerSpawn(playerObject, soliderObject):
	try:	
		# How you would lower the player health
		playerObject.getVehicle().setDamage(0.000000001)	# player health
	
	except Exception, e:
		debug(e) # Error msg


# OnPlayerDeath
def OnPlayerDeath(playerObject, soliderObject):
	try:
		# Cretes a timer and calls it once. after 1 second.
		data = None
		timer = bf2.Timer(TestTimer, 1, 1, data)
		#timer.setRecurring(10) # Calls it agin after 10 seoconds.

	except Exception, e:
		debug(e) # Error msg
BF2 Technical Information Wiki

Might help you a bit
UTurista
PR:BF2 Developer
Posts: 985
Joined: 2011-06-14 14:13

Re: [HELP] BF2 Phyton related questions

Post by UTurista »

Thanks.

No idea bout the window mode, and timmer is already working.
Image


Dont question the wikipedia! Just because it reports different things on different languages does not make it unreliable source!
UTurista
PR:BF2 Developer
Posts: 985
Joined: 2011-06-14 14:13

Re: [HELP] BF2 Phyton related questions

Post by UTurista »

In BF2 is this possible? Adding a trigger to a player?

Code: Select all

def onPlayerSpawn(playerObject, soldierObject):
		bf2.triggerManager.createHemiSphericalTrigger(soldierObject, myCallbackFunction, '<<PCO>>', 20, (1, 2, 3))
Also, the '<<PCO>>' part can anyone explain it what does it mean?

Edit:
and with a timer and these 4:

Code: Select all

physicalObject.getPosition()
physicalObject.setPosition( (X, Y, Z) )
physicalObject.getRotation()
physicalObject.setRotation( (A, B, C) )
Wouldn't it be possible to create a tow-truck script?
Last edited by UTurista on 2014-01-18 17:13, edited 1 time in total.
Image


Dont question the wikipedia! Just because it reports different things on different languages does not make it unreliable source!
piepieonline
Retired PR Developer
Posts: 433
Joined: 2009-07-22 00:41

Re: [HELP] BF2 Phyton related questions

Post by piepieonline »

Right, sorry I didn't see this thread earlier.
First thing, if you haven't already, I would suggest changing 'BF2Root/python/bf2/init.py':
From:

Code: Select all

def init_module():
	# set up stdout and stderr to map to the host's logging function
	sys.stdout = fake_stream('stdout')
	sys.stderr = fake_stream('stderr')

	...
To:

Code: Select all

def init_module():
	# set up stdout and stderr to map to the host's logging function
	logFile = open('mylogfile.log', 'w')
	sys.stdout = logFile
	sys.stderr = logFile

	...
This will cause the file Bf2Root/mylogfile.log to contain information about the previous run - python syntax errors, and other errors if not caught, will be written here. This is also where 'print' will write to.
A major point to note though, however, is that this will not be up to date during run time - so if you need information that is in there, use the console command 'exit' to close the server.

Neither host.rcon_invoke('game.sayall "message"') or print show in the console, but using host.rcon_invoke('echo "message"') will. (But it will not be outputted elsewhere)

Timers are in seconds.

Player objects are the player's 'souls' - use player.getDefaultVehicle() to get the actual soldier object, which then can be operated on like any other vehicle - .setDamage( dmg ) and .getDamage()

Adding triggers to players, no idea, I have never tried it, so post here if you manage to get it working? PCO stands for player controlled object, no idea on what it means here.

And finally, for towing, AFAIK, we have thought about it before, but it doesn't really work too well, and it would increase server strain immensely.
Image
Post Reply

Return to “PR:BF2 Community Modding”