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

Post Python examples to help other users.
nergal
Member
Posts: 48
Joined: Sun Mar 15, 2015 2:58 pm

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

Postby nergal » Wed Jul 01, 2015 4:02 pm

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
User avatar
Ayuto
Project Leader
Posts: 1579
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Postby Ayuto » Thu Jul 02, 2015 11:09 am

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.
Mahi
Senior Member
Posts: 212
Joined: Wed Aug 29, 2012 8:39 pm
Location: Finland

Postby Mahi » Thu Jul 02, 2015 11:43 am

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 :)
nergal
Member
Posts: 48
Joined: Sun Mar 15, 2015 2:58 pm

Postby nergal » Thu Jul 02, 2015 12:20 pm

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
Mahi
Senior Member
Posts: 212
Joined: Wed Aug 29, 2012 8:39 pm
Location: Finland

Postby Mahi » Thu Jul 02, 2015 12:47 pm

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.
stonedegg
Senior Member
Posts: 141
Joined: Sun Aug 12, 2012 11:45 am

Postby stonedegg » Thu Jul 02, 2015 1:09 pm

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.
necavi
Developer
Posts: 129
Joined: Wed Jan 30, 2013 9:51 pm

Postby necavi » Thu Jul 02, 2015 5:02 pm

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.
nergal
Member
Posts: 48
Joined: Sun Mar 15, 2015 2:58 pm

Postby nergal » Thu Jul 02, 2015 9:26 pm

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?
necavi
Developer
Posts: 129
Joined: Wed Jan 30, 2013 9:51 pm

Postby necavi » Thu Jul 02, 2015 9:33 pm

No. Although I did already suggest an IDE which will do most of that sort of thing for you.
User avatar
Ayuto
Project Leader
Posts: 1579
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Postby Ayuto » Thu Jul 02, 2015 9:37 pm

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
User avatar
satoon101
Project Leader
Posts: 2323
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Thu Jul 02, 2015 9:40 pm

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.
Image
nergal
Member
Posts: 48
Joined: Sun Mar 15, 2015 2:58 pm

Postby nergal » Fri Jul 03, 2015 12:58 am

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.
User avatar
satoon101
Project Leader
Posts: 2323
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Fri Jul 03, 2015 1:09 am

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.
Image
necavi
Developer
Posts: 129
Joined: Wed Jan 30, 2013 9:51 pm

Postby necavi » Fri Jul 03, 2015 3:17 am

Pycharm has a free (and open source) version that basically just cuts out the web development stuff.
nergal
Member
Posts: 48
Joined: Sun Mar 15, 2015 2:58 pm

Postby nergal » Sat Jul 04, 2015 1:52 am

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.
necavi
Developer
Posts: 129
Joined: Wed Jan 30, 2013 9:51 pm

Postby necavi » Sat Jul 04, 2015 11:15 am

I'll try to write something up on it tomorrow.
nergal
Member
Posts: 48
Joined: Sun Mar 15, 2015 2:58 pm

Postby nergal » Fri Jul 10, 2015 9:03 pm

User avatar
satoon101
Project Leader
Posts: 2323
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Fri Jul 10, 2015 9:23 pm

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.
Image
User avatar
BackRaw
Senior Member
Posts: 413
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany

Postby BackRaw » Sun Jul 12, 2015 9:30 pm

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.
My Github repositories:

Source.Python: https://github.com/backraw/backraw.sp
User avatar
BackRaw
Senior Member
Posts: 413
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany

Postby BackRaw » Sun Jul 12, 2015 9:38 pm

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!
My Github repositories:



Source.Python: https://github.com/backraw/backraw.sp

Return to “Code examples / Cookbook”

Who is online

Users browsing this forum: No registered users and 1 guest