Page 1 of 1

trigger creation

Posted: Sat Apr 21, 2018 8:53 pm
by malt
Hi,
I'm trying to convert a zone creation plugin to sourcepython. I'm having an issue with creating triggers.

The following sourcemod code is what I've been using. Its based off this.

Syntax: Select all

int trigger = CreateEntityByName( type );
DispatchKeyValue( trigger, "spawnflags", "1" );
DispatchKeyValue( trigger, "targetname", name );

if ( filterName[0] ) {
DispatchKeyValue( trigger, "filtername", filterName );
}

DispatchKeyValue( trigger, "wait", "0" );

DispatchSpawn( trigger );
ActivateEntity( trigger );

TeleportEntity( trigger, mid, NULL_VECTOR, NULL_VECTOR );
SetEntityModel( trigger, "models/error.mdl" );

SetEntPropVector( trigger, Prop_Send, "m_vecMins", min );
SetEntPropVector( trigger, Prop_Send, "m_vecMaxs", max );
SetEntProp( trigger, Prop_Send, "m_nSolidType", 2 );

int iEffects = GetEntProp( trigger, Prop_Send, "m_fEffects" );
iEffects |= 32;
SetEntProp( trigger, Prop_Send, "m_fEffects", iEffects );


The sourcepython version is

Syntax: Select all

def create_trigger(start: Vec3, end: Vec3):
end.z += 100
center = ((end - start) * 0.5) + start
start -= center
end -= center
start = start.abs()
end = end.abs()
start *= -1.0

entity = Entity.create('trigger_multiple')
entity.spawn_flags = 1
entity.target_name = 'nametest'
entity.spawn()

entity.origin = center.to_vec()
entity.model = error
entity.maxs = end.to_vec()
entity.mins = start.to_vec()
entity.solid_type = SolidType.BBOX
entity.effects |= 0x020

When I loop through the entities, the trigger appears - with the values set correctly. The problem is that OnEntityOutput isn't called when walking through where the trigger should be. I don't really see a difference in the code, except for ActivateEntity, which doesn't seem relevant to the issue. Any ideas of what this could be?

Thanks, and sorry if I'm missing something obvious. I've been watching sourcepython for a while, but this is my first time ever using it.

edit* I reinvented the wheel a bit with the Vec3 class, it converts to the expected Vector format correctly, but I'll change that later.

Re: trigger creation

Posted: Sat Apr 21, 2018 9:14 pm
by decompile
Hey Malt,

nice to see you here!

My guess would be, that your vectors arent a Vector Instance, which might would lead the trigger to be spawned but not on the right position.

So try to convert your vectors to a Vector.

Syntax: Select all

from mathlib import Vector

my_vector = Vector(x, y, z)


You can also simplify your trigger creation with:

Syntax: Select all

def create_trigger_multiple(vector1, vector2, filter_name):
# Get Min And Max Coords
origin = vector1.min(vector2)
max = vector1.max(vector2)

# Get Maxs And Mins
maxs = max - origin
mins = NULL_VECTOR

# Create Trigger Multiple
trigger_multiple = Entity.create('trigger_multiple')
trigger_multiple.model = trigger_model
trigger_multiple.origin = origin
trigger_multiple.maxs = maxs
trigger_multiple.mins = mins
trigger_multiple.solid_type = SolidType.BBOX
trigger_multiple.effects |= EntityEffects.NODRAW
trigger_multiple.wait = 0
trigger_multiple.spawn_flags = 257
trigger_multiple.filter_name = filter_name
trigger_multiple.spawn()

return trigger_multiple

Re: trigger creation

Posted: Sun Apr 22, 2018 1:19 am
by decompile
An Update:

I helped him trying to figure out the problem and it ended up Entity.spawn() being the problem.

I noticed, in my code above, setting the spawn() BEFORE setting the model or AFTER setting maxs/mins, it doesnt "spawn". It actually calls OnEntitySpawned Listener but hooking start_touch or OnEntityOutput, it doesnt call any output_name such as OnStartTouch, Touch, OnEndTouch etc..

Is this a bug on Source.Pythons end? Why does it behave like this? Wouldnt a "ActivateEntity" help here but how do you call it?

Re: trigger creation

Posted: Mon Apr 23, 2018 7:23 pm
by Ayuto
No, it's not a bug in SP and I think it makes completely makes sense to spawn it after everything of the entity has been initialized.

SM's ActivateEntity is calling a virtual function, which requires additional data, which I would like to avoid. You might be able to achieve the same by calling the input Enable or Activate (if it exists) or waking it through our physics module.

Re: trigger creation

Posted: Thu Apr 26, 2018 6:33 pm
by jayess
I've had success setting the 'model' keyvalue, and spawning it before setting the other attributes:
(might be some errors since I adapted it to fit decompile's code without testing)

Syntax: Select all

def create_trigger_multiple(vector1, vector2, filter_name):
# Get Min And Max Coords
origin = vector1.min(vector2)
max = vector1.max(vector2)

# Get Maxs And Mins
maxs = max - origin
mins = NULL_VECTOR

# Create Trigger Multiple
trigger_multiple = Entity.create('trigger_multiple')
trigger_multiple.set_key_value_string('model', 'models/props_junk/gascan001a.mdl')
trigger_multiple.spawn()
trigger_multiple.origin = origin
trigger_multiple.maxs = maxs
trigger_multiple.mins = mins
trigger_multiple.solid_type = SolidType.BBOX
trigger_multiple.effects |= EntityEffects.NODRAW
trigger_multiple.wait = 0
trigger_multiple.spawn_flags = 257
trigger_multiple.filter_name = filter_name

return trigger_multiple