Turn Trigger on/off Script

I need a script to turn on/off a instant death trigger. I’ve seen something similar in several mods, but have not been able to track the script, or it was simply a modified trap. Traps are of no use for the instance I am using as none will be strong enough to kill the characters I am using, so the trigger kills the character no matter what unless they are invincible, but I want to put an off script on a lever. Thanks in advance for the help!

EDIT - ignore this post. Turns out that the OP was asking about NWN 2, this reply assumes NWN 1.

To turn a trigger on,

object oTrigger = GetObjectByTag("tag_of_trigger here");
SetLocalInt(oTrigger, "on", TRUE);

To turn it off, same code, but FALSE (or DeleteLocalInt).

In the trigger OnEnter script,

object oTrigger = OBJECT_SELF;
object oNPC     = GetEnteringObject();
if (!GetLocalInt(oTrigger, "on")) return;
// Kill creature to force OnDeath event (which EffectDeath does not)
SetPlotFlag(oNPC, FALSE);
SetImmortal(oNPC, FALSE);
AssignCommand(GetModule(), ApplyEffectToObject(DURATION_TYPE_INSTANT,
                           SupernaturalEffect(EffectDamage(2 * GetCurrentHitPoints(oNPC), DAMAGE_TYPE_BLUDGEONING)), oNPC));
}

This may be over-complicated in Single Player. If all you need is for the lever to turn the trigger off forever,

DestroyObject(oTrigger);

Proleric’s solution is best for your one off case, but in case someonewanted a more general solution, you can also do something like:

void set_trigger_enabled(object trigger, int enabled) {
    if (GetLocalInt(trigger, "disabled") == !enabled)
        return;

    int i = EVENT_SCRIPT_TRIGGER_ON_HEARTBEAT;
    while (i <= EVENT_SCRIPT_TRIGGER_ON_CLICKED) {
        string varname = "stored_script_" + IntToString(i);
        if (enabled) {
            SetEventScript(trigger, i, GetLocalString(trigger, varname));
            DeleteLocalString(trigger, varname);
        } else {
            SetLocalString(trigger, varname, GetEventScript(trigger, i));
            SetEventScript(trigger, i, "");
        }
        i++;
    }

    SetLocalInt(trigger, "disabled", !enabled);
}

I will try these and thanks to you both!

Okay, Sherincall couldn’t get yours to compile, due to this line DeleteLocalString(trigger, varname); unrec. param or something like that, Prol, only wanted the ones with disable via device/lever/etc, first one didn’t work but the “Destroy Trigger” command worked!

Thanks!

I don’t have a compiler handy but I double checked and it looks correct. One thing I forgot to mention is that it only works on EE - were you trying to compile with DE/1.69? Or nwn2 for that matter?

I’m using NWN 2.

1 Like

perhaps something like this …

// 'deathtrigger_oe'
/*
	OnEnter script for a trigger.

	Kills the entering object if enabled.

	This works in conjunction with 'deathlever_ou'. The variable "enabled"
	starts FALSE ... the state of the activation lever should also start FALSE.
	If things should start TRUE then set "enabled" TRUE on both this trigger and
	start the lever in its Activated position.
*/

void main()
{
	if (GetLocalInt(OBJECT_SELF, "enabled"))
	{
		object oEnter = GetEnteringObject();
//		SetPlotFlag(oEnter, FALSE); // not sure how you want these set ->
//		SetImmortal(oEnter, FALSE);
//		AssignCommand(oEnter, SetIsDestroyable(TRUE));

		effect eHurtbad = EffectDamage(GetCurrentHitPoints(oEnter) * 2,
									   DAMAGE_TYPE_MAGICAL,
									   DAMAGE_POWER_NORMAL,
									   FALSE); // nIgnoreResistances

		DelayCommand(0.1f, ApplyEffectToObject(DURATION_TYPE_INSTANT, eHurtbad, oEnter));
	}
}

 

// 'deathlever_ou'
/*
	OnUsed script for a lever.

	Toggles the enabled state of a deathtrigger.
*/

const string TAG_DEATHTRIG = "tag_deathtrigger"; // <- tag of your deathtrigger (unique across the Module)

void main()
{
	int bActivate = !GetLocalInt(OBJECT_SELF, "X2_L_PLC_ACTIVATED_STATE");
	if (bActivate)
	{
		ActionPlayAnimation(ANIMATION_PLACEABLE_ACTIVATE);
	}
	else
		ActionPlayAnimation(ANIMATION_PLACEABLE_DEACTIVATE);

	SetLocalInt(OBJECT_SELF, "X2_L_PLC_ACTIVATED_STATE", bActivate);

	object oDeathtrigger = GetObjectByTag(TAG_DEATHTRIG);
	if (GetIsObjectValid(oDeathtrigger))
		SetLocalInt(oDeathtrigger, "enabled", bActivate);
}
1 Like

Oof, sorry, my bad for making dumb assumptions.

no worries from me … a note u might find interesting/amusing is that nwn2 has Get/SetEventHandler()

1 Like

Oh? Then what does it not have, DeleteLocalString? That one we can just replace with SetLocalString(trigger, varname, ""). Feel free to post to post a nwn2 compatible version if you want :slight_smile:

Moved topic to the NWN2 forum.

MODERATOR

1 Like
// Clears or restores a trigger's event-scripts.
//
// IMPORTANT: Scripts for a trigger are typically assigned in its
// toolset-properties; if so an int_variable "enabled" should also be set TRUE
// in its toolset-properties.
// Alternately if the trigger's scriptset is left blanked in the toolset then
// a set of 7 scripts need to be assigned as string_variables:
//   stored_script_0
//   stored_script_1
//   ...
//   stored_script_6
// And the int_variable "enabled" should be left FALSE. tbh only the events
// that need to have script(s) slotted need their string(s) assigned ->
//
// int SCRIPT_TRIGGER_ON_HEARTBEAT          = 0;
// int SCRIPT_TRIGGER_ON_OBJECT_ENTER       = 1;
// int SCRIPT_TRIGGER_ON_OBJECT_EXIT        = 2;
// int SCRIPT_TRIGGER_ON_USER_DEFINED_EVENT = 3;
// int SCRIPT_TRIGGER_ON_TRAPTRIGGERED      = 4;
// int SCRIPT_TRIGGER_ON_DISARMED           = 5;
// int SCRIPT_TRIGGER_ON_CLICKED            = 6;
//
void set_trigger_enabled(object oTrigger, int bEnabled)
{
	if (GetLocalInt(oTrigger, "enabled") != bEnabled)
	{
		SetLocalInt(oTrigger, "enabled", bEnabled);

		string sScript;

		int i = SCRIPT_TRIGGER_ON_HEARTBEAT;
		while (i <= SCRIPT_TRIGGER_ON_CLICKED)
		{
			string var = "stored_script_" + IntToString(i);
			if (bEnabled)
			{
				sScript = GetLocalString(oTrigger, var);
				if (sScript != "") SetEventHandler(oTrigger, i, sScript);

				DeleteLocalString(oTrigger, var);
			}
			else // disabled
			{
				sScript = GetEventHandler(oTrigger, i);
				if (sScript != "")
				{
					SetLocalString(oTrigger, var, sScript);
					SetEventHandler(oTrigger, i, "");
				}
			}
			++i;
		}
	}
}

 
@adam_ii323 if you call set_trigger_enabled() in the lever’s OnUsed script, then the trigger’s OnEnter script doesn’t need to check for “enabled” … although “enabled” will still be set correctly … but it would be redundant

1 Like

By the way, by keeping the local var as “disabled” and not “enabled” you don’t have to set the var in the toolset. The default state without the var set is enabled, and only if you ever disable it does the var get assigned. Relies on the fact that GetLocalInt returns 0 if the var doesn’t exist.

1 Like

This is interesting Kev, since I was wondering how I would reactivate (if needed). The one that worked from Prol, destroyed the trigger, this looks as if I can enable and disable it, which does fit the parameters of my original req.

1 Like

I know this is a bit random but triggers have factions, however I don’t think you can change them I think they’re all set to 1 which isn’t really a faction.

If it can be changed you could reset your lethal trap to only kill enemies.

1 Like

Good to know