DMH-Online

Dungeon Masters Helper · Est. 2005

Misc Scripts

NWN Script: manage_action_queue.htm

Author Sticky: Post Working Scripts Here
Disgrace Posted: Sunday, 30 June 07:10AM
Script Name: Zero Based Action Queue

Description: Helps manages the actions assigned to an object

Notes: The only limitation of this method that I know of, is that if the actions are processed in an OnHeartBeat() event, the actions will overflow if any action takes longer than 6 seconds. By overflow, I mean the action may occur more than a single time.

NWScript:
////////////////////////////////////////////////////////////////////////////////
// ZERO BASED ACTION QUEUE
//
// Description: An action queue to manage actions assigned to objects.
//
// Usage:
//
// 1. When you want an object to perform a set of actions, add them to the queue
//     PushAction(ACTION_WALK_TO_STORE);
//     PushAction(ACTION_BUY_SOME_STUFF);
//
// 2. In a heartbeat or a UDE add your action specific code. NOTE: pay special
//    attention to the PopAction() call.
//
//    int nActionToPerform = GetActiveAction();
//    if (nActionToPerform == 0) return;
//    switch (nActionToPerform) {
//        case ACTION_WALK_TO_STORE: {
//            object oStore = GetObjectByTag("TAG_STORE");
//            ActionMoveToObject(oStore);
//            ActionDoCommand(PopAction());
//            break;
//        }
//        case ACTION_BUY_SOME_STUFF: {
//            yada yada...
//        }
//    }
//
//  3. The only limitation of this method that I know of, is that if the actions
//     are processed in an OnHeartBeat() event, the actions will overflow if
//     any action takes longer than 6 seconds. By overflow, I mean the action
//     may occur more than a single time.
//
// Author: Scott Woolcox.
//
// email me scott_woolcox@hotmail.com if you have a better solution, or find any
// problems.
////////////////////////////////////////////////////////////////////////////////
// define the event number you wish to use for execution in an OnUserDefined() event.
int ACTION_QUEUE_UDEVENT_NUMBER = 3000;
/**
 * Adds a new action to the bottom of the qeueu
 */
void PushAction(int nAction, object oTarget=OBJECT_SELF) {
    string sActions = GetLocalString(oTarget, "action_list");
    if (GetStringLength(sActions) == 0) {
        sActions = IntToString(nAction)+";";
    } else {
        sActions = sActions+IntToString(nAction)+";";
    }
    SetLocalString(oTarget, "action_list", sActions);
}
/**
 * Gets the current action that requires processing.
 */
int GetActiveAction(object oTarget=OBJECT_SELF) {
    string sAction = GetLocalString(oTarget, "activeaction");
    if (sAction == "") {
        string sActions = GetLocalString(oTarget, "action_list");
        if (sActions == "") return 0;
        int nIndex = FindSubString(sActions, ";");
        sAction = GetStringLeft(sActions, nIndex);
        if (sAction == "") {
            return 0;
        } else {
            SetLocalString(oTarget, "activeaction", sAction);
        }
    }
    return StringToInt(sAction);
}
/**
 * Removes a processed message from the queue
 */
void PopAction(object oTarget=OBJECT_SELF) {
    string sActions = GetLocalString(oTarget, "action_list");
    int nLen = GetStringLength(sActions);
    int nIndex = FindSubString(sActions, ";")+1;
    sActions = GetSubString(sActions, nIndex, nLen-nIndex);
    SetLocalString(oTarget, "action_list", sActions);
    DeleteLocalString(oTarget, "activeaction");
    SignalEvent(oTarget, EventUserDefined(ACTION_QUEUE_UDEVENT_NUMBER));
}
⌂ All Scripts ← PreviousNext → 👁 Category List

All Misc Scripts

📄 arena 📄 cast_bless_on_PC 📄 change_music 📄 chess_game 📄 climbing 📄 climbing_002 📄 common_includes_002 📄 cone_aoe 📄 cron 📄 cross_country_travel 📄 daoc_style_graves 📄 determine_time 📄 effects 📄 escort 📄 ez_respawn 📄 faction 📄 floating_text 📄 function_includes 📄 gambling 📄 include_sounds 📄 include_time_date 📄 jail 📄 jail_system 📄 journal_entry 📄 logbook 📄 manage_action_queue 📄 mmorpg_vault 📄 mod_intro 📄 mod_intro_002 📄 mod_intro_003 📄 mod_intro_005 📄 pers_world_database 📄 pers_world_database_002 📄 piller_of_fire 📄 plot_item 📄 portal_system 📄 portal_system_002 📄 puking 📄 quest_escort 📄 rep_per 📄 reward_rp 📄 rotation 📄 save_pc_location_002 📄 save_pc_location_003 📄 skill_checks 📄 skill_checks_002 📄 slot_machine 📄 story 📄 swimming 📄 take20_bypass 📄 vis_waypoint 📄 working_temple 📄 working_temple_002 📄 xp_gold_loss 📄 xp_gold_loss_002 📄 xp_loss_action