Difference between revisions of "OnPlayerTarget"

From NWN Lexicon
Jump to: navigation, search
m (Remarks)
m (Remarks)
 
(2 intermediate revisions by the same user not shown)
Line 14: Line 14:
 
====Remarks====
 
====Remarks====
  
'''Note:''' Currently you must use [[SetEventScript]](GetModule(), EVENT_SCRIPT_MODULE_ON_PLAYER_TARGET, "script_name"). You cannot set this in the module properties yet.
+
'''Note:''' A script for the OnPlayerTarget event cannot be specified in module properties.  There is no default script for this event, so no action will be taken unless a script is assigned to this event.  To assign a script to this event, use this code:
 +
<nwscript>
 +
SetEventScript(GetModule(), EVENT_SCRIPT_MODULE_ON_PLAYER_TARGET, "script_name").
 +
</nwscript>
  
There is no default script for this event, and therefore the default behaviour for this event is to do nothing at all.
+
Upon entering targeting mode by using [[EnterTargetingMode]], a player can target any game object or position, as limited by the [[EnterTargetingMode]] object type parameter.  Once an object or position is targeted, of if the player manually exits targeting mode, the OnPlayerTarget event will be signalled.
  
Once set however and [[EnterTargetingMode]] is called the player can target an appropriate ''thing'' or location. This event will fire if the player targets an object or manually exits targeting mode.
+
The return values from the event data access functions will vary depending on the type of object selected.  Generally, a player can only target a visible game object or position. DMs can target triggers if the "Show Triggers" option is selected.
  
This then can do something related to what occured; some ideas:
+
''GetTargetingModeSelectedObject'' will return the targeted object, or if targeting a position, it will return the area object where the position is located.  If the player has exited targeting mode, this function will return an invalid object.  For invalid objects, GetIsObjectValid will return FALSE and GetTargetingModeSelectedObject() == OBJECT_INVALID will evaluate to TRUE.  Items in inventories can be targeted.
  
* Conversation option to "pick an item to upgrade" allows the player to open the inventory next to the conversation, and select an item from it
+
''GetTargetingModeSelectedPosition'' will return the vector position of the targeted object.  If the target is large, such as a placeable, the returned position is the approximate center point of the object.  If the target is a trigger, the returned position is the coordinates of the first point of the trigger (as designed in the toolset).  If the targeted object is OBJECT_TYPE_TILE, the returned position is the coordinates of the targeted position.  If the targeted object is in an inventory or if the player has manually exited targeting mode, the returned position is invalid and GetTargetingModeSelectedPosition == Vector() will evaluate to TRUE.
* A speakstring (hidden/whispered) of a player is captured, and causes this event to fire, eg; to have a visual effect appear at the chosen location
 
* Another event, such as using an item or spell (eg; "Use Item (On Self)"), activates this to target the effects of said item
 
  
To do multi-select, or once a target is checked and is invalid, you could easily have this event call [[EnterTargetingMode]] again meaning it fires again.
+
Since a player can successfully target the origin (0.0, 0.0, 0.0) of the area, the most reliable way to determine if the player has exited targeting mode without selecting a target is to test for object validity and vector value:
 +
<nwscript>
 +
    object oTarget = GetTargetingModeSelectedObject();
 +
    vector vTarget = GetTargetingModeSelectedPosition();
 +
 
 +
    if (!GetIsObjectValid(oTarget) && vTarget == Vector())
 +
    {
 +
        // The player has exited targeting mode
 +
        return;
 +
    }
 +
</nwscript>
  
 
====Known Bugs====
 
====Known Bugs====
Line 35: Line 46:
  
 
====Example====
 
====Example====
 +
<nwscript>
 +
void main()
 +
{
 +
    // Get the last player to use targeting mode
 +
    object oPC = GetLastPlayerToSelectTarget();
 +
 +
    // Get the targeting mode data
 +
    object oTarget = GetTargetingModeSelectedObject();
 +
    vector vTarget = GetTargetingModeSelectedPosition();
 +
 +
    // If the user manually exited targeting mode without selecting a target, return
 +
    if (!GetIsObjectValid(oTarget) && vTarget == Vector())
 +
        return;
  
 +
    // Save the targeting data to the PC object for later use
 +
    location lTarget = Location(GetArea(oTarget), vTarget, 0.0f);
 +
   
 +
    SetLocalObject(oPC, "TARGETING_OBJECT", oTarget);
 +
    SetLocalLocation(oPC, "TARGETING_POSITION", lTarget);
 +
}
 +
</nwscript>
  
 
====See Also====
 
====See Also====

Latest revision as of 07:12, 11 February 2021

Nwnee logo.jpg Note: This article documents Neverwinter Nights: Enhanced Edition new content or changes/updates/fixes to 1.69 functions. These are all listed under the category and patches pages.

The script attached to this event fires when EnterTargetingMode is activated.

Trigger

When EnterTargetingMode is used and the player either selects a target or manually exits targeting mode.

Function(s)

Remarks

Note: A script for the OnPlayerTarget event cannot be specified in module properties. There is no default script for this event, so no action will be taken unless a script is assigned to this event. To assign a script to this event, use this code:

SetEventScript(GetModule(), EVENT_SCRIPT_MODULE_ON_PLAYER_TARGET, "script_name").

Upon entering targeting mode by using EnterTargetingMode, a player can target any game object or position, as limited by the EnterTargetingMode object type parameter. Once an object or position is targeted, of if the player manually exits targeting mode, the OnPlayerTarget event will be signalled.

The return values from the event data access functions will vary depending on the type of object selected. Generally, a player can only target a visible game object or position. DMs can target triggers if the "Show Triggers" option is selected.

GetTargetingModeSelectedObject will return the targeted object, or if targeting a position, it will return the area object where the position is located. If the player has exited targeting mode, this function will return an invalid object. For invalid objects, GetIsObjectValid will return FALSE and GetTargetingModeSelectedObject() == OBJECT_INVALID will evaluate to TRUE. Items in inventories can be targeted.

GetTargetingModeSelectedPosition will return the vector position of the targeted object. If the target is large, such as a placeable, the returned position is the approximate center point of the object. If the target is a trigger, the returned position is the coordinates of the first point of the trigger (as designed in the toolset). If the targeted object is OBJECT_TYPE_TILE, the returned position is the coordinates of the targeted position. If the targeted object is in an inventory or if the player has manually exited targeting mode, the returned position is invalid and GetTargetingModeSelectedPosition == Vector() will evaluate to TRUE.

Since a player can successfully target the origin (0.0, 0.0, 0.0) of the area, the most reliable way to determine if the player has exited targeting mode without selecting a target is to test for object validity and vector value:

    object oTarget = GetTargetingModeSelectedObject();
    vector vTarget = GetTargetingModeSelectedPosition();
 
    if (!GetIsObjectValid(oTarget) && vTarget == Vector())
    {
        // The player has exited targeting mode
        return;
    }

Known Bugs

Version

1.80.8193.14

Example

void main()
{
    // Get the last player to use targeting mode
    object oPC = GetLastPlayerToSelectTarget();
 
    // Get the targeting mode data
    object oTarget = GetTargetingModeSelectedObject();
    vector vTarget = GetTargetingModeSelectedPosition();
 
    // If the user manually exited targeting mode without selecting a target, return
    if (!GetIsObjectValid(oTarget) && vTarget == Vector())
        return;
 
    // Save the targeting data to the PC object for later use
    location lTarget = Location(GetArea(oTarget), vTarget, 0.0f);
 
    SetLocalObject(oPC, "TARGETING_OBJECT", oTarget);
    SetLocalLocation(oPC, "TARGETING_POSITION", lTarget);
}

See Also

functions:

EnterTargetingMode GetLastPlayerToSelectTarget GetTargetingModeSelectedPosition GetTargetingModeSelectedObject