Page 1 of 2

[TF2] getting healing target + checking if player is being healed from a dispenser

Posted: Wed Jul 01, 2015 4:02 pm
by nergal
Two code examples for those wishing to go into TF2 :smile:

'get_healing_target' SHOULD return the heal-player of the player's medigun

Then there's 'is_near_dispenser' which checks to see how much stuff is healing the player and sorting it out whether it's from medics or not.

Thus, it works very nicely to determine if it's really from a dispenser or not.

Hopefully these code examples will work with no error. Happy Coding!

Syntax: Select all

def get_healing_target(client): #need player for 
medigun = client.get_secondary()
if medigun is None:
return None

if medigun.classname() == "tf_weapon_medigun":
if medigun.get_property_bool('m_bHealing'):
return medigun.get_property_edict('m_hHealingTarget')

return None


Syntax: Select all

def is_near_dispencer(client):
medics = 0
healers = client.get_property_int('m_nNumHealers')

if healers > 0:
for i in range(1, 34):
player = PlayerEntity(i)
if ( player is not None and not player.isdead and get_healing_target(player).userid == client.userid ):
medics += 1

return healers > medics

Posted: Thu Jul 02, 2015 11:09 am
by Ayuto
I'm happy that you play around with SP, but you should really test your code before posting on the forums -- at least when it comes to code examples!

There are some errors in your code.

Posted: Thu Jul 02, 2015 11:43 am
by Mahi
We (Python coders in general) don't use parenthesis around if sentences in Python, unless the if sentence is splitted on two lines like so:

Syntax: Select all

if (player is not None and not player.isdead
and get_healing_target(player).userid == client.userid):
medics += 1
But all other paranthesis should be removed from if senteces, as well as return statements. Just a friendly tip to get you in on the Python habits :)

Posted: Thu Jul 02, 2015 12:20 pm
by nergal
Ayuto wrote:I'm happy that you play around with SP, but you should really test your code before posting on the forums -- at least when it comes to code examples!

There are some errors in your code.


I apologize, I'm used to compiling my code as I did with sourcemod but I don't know how to test-compile with source.python

Posted: Thu Jul 02, 2015 12:47 pm
by Mahi
nergal wrote:
Ayuto wrote:I'm happy that you play around with SP, but you should really test your code before posting on the forums -- at least when it comes to code examples!

There are some errors in your code.


I apologize, I'm used to compiling my code as I did with sourcemod but I don't know how to test-compile with source.python
Install a TF2 decicated server on your local machine, then install Source.Python plugin on it, and simply test your scripts in-game on your dedicated server.

Posted: Thu Jul 02, 2015 1:09 pm
by stonedegg
Yes, all you need to do is loading your script, python does the rest. That's also what I always found annoying with Soucemod, compared to Eventscripts/SourcePython.

Posted: Thu Jul 02, 2015 5:02 pm
by necavi
You may want to look into: http://wiki.sourcepython.com/pages/filters.players

Also typically None is a bit of a special case and is compared using "is" (i.e. variable is None) rather than "==", as None is a singleton.

Posted: Thu Jul 02, 2015 9:26 pm
by nergal
Mahi wrote:
nergal wrote:
Ayuto wrote:I'm happy that you play around with SP, but you should really test your code before posting on the forums -- at least when it comes to code examples!

There are some errors in your code.


I apologize, I'm used to compiling my code as I did with sourcemod but I don't know how to test-compile with source.python
Install a TF2 decicated server on your local machine, then install Source.Python plugin on it, and simply test your scripts in-game on your dedicated server.



There's no way to compile off-hand? Not even as a test way to see if the interpretor can compile it prior to running?

Posted: Thu Jul 02, 2015 9:33 pm
by necavi
No. Although I did already suggest an IDE which will do most of that sort of thing for you.

Posted: Thu Jul 02, 2015 9:37 pm
by Ayuto
That's how Python works. When a Python script is loaded/imported the interpreter will byte-compile it. When there are syntax errors, you will be notified. All other errors (e.g. undefined variables aka NameError) are raised at runtime. E.g. this script would cause a SyntaxError when it's loaded, because a colon is missing

Syntax: Select all

>>> def f()
print(my_undefined_variable)

SyntaxError: invalid syntax
If we add that colon, it will be loaded without any errors.

Syntax: Select all

>>> def f():
print(my_undefined_variable)
However, if you then execute the function, you wil get a NameError, because "my_undefined_variable" was never set anywhere.

Syntax: Select all

>>> f()

Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
f()
File "<pyshell#6>", line 2, in f
print(my_undefined_variable)
NameError: global name 'my_undefined_variable' is not defined

Posted: Thu Jul 02, 2015 9:40 pm
by satoon101
If you have Source.Python specific imports, you need a Source.Python environment for it to compile in, afaik. And even that won't catch all errors. It is highly recommended that to post in this section you should not only make sure the plugin loads, but functions properly. To do that, you need to run it on a server.

Posted: Fri Jul 03, 2015 12:58 am
by nergal
satoon101 wrote:If you have Source.Python specific imports, you need a Source.Python environment for it to compile in, afaik. And even that won't catch all errors. It is highly recommended that to post in this section you should not only make sure the plugin loads, but functions properly. To do that, you need to run it on a server.


How would I test-run my code on a Source.Py environment? I have all files including the C++ sources of source.py

I understand it won't catch all errors but the only errors I'm interested in finding is the syntax errors so that I can then test out for logic errors.

Posted: Fri Jul 03, 2015 1:09 am
by satoon101
If you install Python3.4 or newer on your system, you can use pip to install the same checkers we use with Source.Python:
https://github.com/Source-Python-Dev-Team/Source.Python/blob/master/checker.bat#L9

These checkers will find all syntax issues, as well as doing other standards checks on your code.

You can also use PyCharm, like necavi suggested. I know the cost is great, though, so if you want a cheaper (free) option, look to Eclipse with its PyDev plugin or SublimeText, which has plugins to help with this, as well.

Posted: Fri Jul 03, 2015 3:17 am
by necavi
Pycharm has a free (and open source) version that basically just cuts out the web development stuff.

Posted: Sat Jul 04, 2015 1:52 am
by nergal
I went with the PyCharm since I've heard of it but how would I set up the environment? This would also be a very nice piece in the Wiki as well.

Posted: Sat Jul 04, 2015 11:15 am
by necavi
I'll try to write something up on it tomorrow.

Posted: Fri Jul 10, 2015 9:03 pm
by nergal

Posted: Fri Jul 10, 2015 9:23 pm
by satoon101
You could always use this:
https://github.com/satoon101/PluginHelpers

I haven't tested it on Linux in a while, but I don't think I broke anything Linux related.

Posted: Sun Jul 12, 2015 9:30 pm
by BackRaw
nergal wrote:I went with the PyCharm since I've heard of it but how would I set up the environment? This would also be a very nice piece in the Wiki as well.


I'm on a Mac with PyCharm 3.5, but I'm pretty sure the names of the Preferences menu are the same. For Mac OS X go to PyCharm/Preferences from the top bar, and for Windows/Linux it's File/Project Settings or something like that.

When you're in the Preferences menu, click on Project: <Project Name> and go to Project Structure. In there, you see the path to your project and the folder's contents on the left, plus a + Add Content Root button - click it.
A folder selection dialog should open up where you will look for the source.python root directory where you see something like this:
  • addons
  • cfg
  • resource
  • sound

Go into addons/source-python/packages, select the folder source-python and click OK. Re-click the + Add Content Root button and add the folder site-packages this time and click OK.

After you close the Preferences menu, PyCharm should start indexing the files and as soon as it's ready you're free to go write some code with a little help :)

And as many have already said, you need to setup a dedicated server and test your code in-game. It's actually the best testing method I've ever encountered. No (felt) compile times plus direct feedbacks as they would be on a real server.

Posted: Sun Jul 12, 2015 9:38 pm
by BackRaw
satoon101 wrote:You could always use this:
https://github.com/satoon101/PluginHelpers

I haven't tested it on Linux in a while, but I don't think I broke anything Linux related.


Well this one is awesome, thanks!