Example of a few items recently implemented

Post Python examples to help other users.
User avatar
satoon101
Project Leader
Posts: 2384
Joined: Sat Jul 07, 2012 1:59 am

Example of a few items recently implemented

Postby satoon101 » Wed Apr 02, 2014 8:09 am

Syntax: Select all

import time

from conversions_c import index_from_edict
from entity_c import EntityGenerator
from entities.entity import BaseEntity
from events import Event
from memory.hooks import PreHook

# Loop through all hostage_entity edicts
for edict in EntityGenerator('hostage_entity', True):

# Get the hostage's BaseEntity instance
hostage = BaseEntity(index_from_edict(edict)

# We only need to get the first hostage_entity edict
break

'''
Use the entity's OnRescueZoneTouch datamap attribute to prehook that function

The function is not directly a part of the entity itself, but instead
the entity is the first argument when the function is called.
'''
@PreHook(hostage.OnRescueZoneTouch.function)
def pre_hostage_rescue(arguments):
'''With a prehook, you can block the event from
happening by returning a value other than None.

None is automatically returned by a function if no value is explicitly returned
'''

# Has it been 2 minutes since the round started?
if time.time() - start_time < 120:

# Disallow rescuing hostages for the first 2 minutes of a round
return False

@Event
def round_freeze_end(game_event):
global start_time
start_time = time.time()


To get a list of all datamaps for a specific type of entity, make sure one is on the server and use the following code:

Syntax: Select all

from conversions_c import index_from_edict
from entity_c import EntityGenerator
from entities.entity import BaseEntity

# Set to the type of entity you wish to get the datamaps of
entity_type = 'player'

for edict in EntityGenerator(entity_type, True):
entity = BaseEntity(index_from_edict(edict))
break

def list_datamap(datamap, tablevel=0):
for x in sorted(datamap):
class_type = type(datamap[x]).__name__[1:]
print_string = '{0}{1} = Class: {2}'.format(
'\t' * tablevel, x, class_type)
if class_type == 'TypeObject':
print('{0} -> Type: {1}'.format(
print_string, datamap[x].type_object.__name__))
elif class_type == 'DataDesc':
print('{0} -> Type: {1}'.format(
print_string, datamap[x].get_attr.split('_', 1)[1]))
elif class_type in ('Input', 'FunctionTable'):
print('{0} -> Type: {1}'.format(print_string, datamap[x].type))
elif class_type == 'Embedded':
print(print_string)
list_datamap(datamap[x], tablevel + 1)

list_datamap(entity.datamaps)


Embedded ones provide further functionality, like:

Syntax: Select all

<PlayerEntity>.pl.deadflag
The first level is pl, which provides the deadflag boolean value.

Inputs are functions, like my example above. Their "type" is the type of value you need to pass in if you want to call it. If the type is VOID, there is nothing to pass in. For example, InputSetTeam and SetTeam (which are actually the exact same thing) are of type INTEGER. So, you need to pass in an integer value:

Syntax: Select all

<PlayerEntity>.SetTeam(2)

Whereas Ignite is a VOID:

Syntax: Select all

<PlayerEntity>.Ignite()

# You can also specify a time to burn the entity with IgniteLifeTime
<PlayerEntity>.IgniteLifetime(2.0)

Notice in that last example that I used 2.0 (a float) and not just 2 (an integer). You must pass in the proper type, meaning that an integer is not allowed to be used when a float is required.
User avatar
satoon101
Project Leader
Posts: 2384
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Sat Apr 05, 2014 2:28 am

Updated the second script above due to changes just implemented.

Also, a bit more info on a couple of the types. When retrieving a TypeObject, the value is passed as that type. When setting a TypeObject, you must set the value to an object of the proper type. So, for instance:

Syntax: Select all

# This is a "Vector" type object, so a Vector instance is returned
origin = <PlayerEntity>.m_vecOrigin
origin.x += 100
origin.y += 100
origin.z += 100

# The type you set it back to must be a Vector instance
<PlayerEntity>.m_vecOrigin = origin


Also, Input types accept 2 other arguments besides the value, caller and activator. Both default to None. If you wish to set either of them, I believe you should set them to CBaseEntity Pointer instances.

FunctionTables act similarly to Input, except they take no arguments. Their "function" attribute, just like <Input>.function, is a Function instance and can be Pre/PostHooked.

If the Class is DataDesc, it is just using the offset to get/set the type of value. For instance:

Syntax: Select all

<PlayerEntity>.m_iFrags = 7
print(<PlayerEntity>.m_iDeaths)
<PlayerEntity>.gravity = 0.25
<PlayerEntity>.health = 400
8guawong
Senior Member
Posts: 148
Joined: Sat Sep 20, 2014 3:06 am

Postby 8guawong » Thu Sep 25, 2014 1:16 pm

Code: Select all

 player.IgniteLiftetime(0.0)
  File '../addons/source-python/packages/source-python/entities/entity.py', line 114, in __getattr__
    raise AttributeError('Attribute '{0}' not found'.format(attr))

AttributeError: Attribute 'IgniteLiftetime' not found


can't unburn :X
User avatar
satoon101
Project Leader
Posts: 2384
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Thu Sep 25, 2014 1:46 pm

You mis-spelled Lifetime. Also, that implementation will be changing fairly soon with the BaseEntity changes.
Image
8guawong
Senior Member
Posts: 148
Joined: Sat Sep 20, 2014 3:06 am

Postby 8guawong » Thu Sep 25, 2014 2:44 pm

satoon101 wrote:You mis-spelled Lifetime. Also, that implementation will be changing fairly soon with the BaseEntity changes.


i copied your code :cool:
User avatar
satoon101
Project Leader
Posts: 2384
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Thu Sep 25, 2014 3:26 pm

lol, oops. Well, I will have to change that when I get home.

*Edit: fixed above.
Image

Return to “Code examples / Cookbook”

Who is online

Users browsing this forum: No registered users and 1 guest