• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[vJASS] Spacebar Detection (Multiplayer)

Requires:
  1. SyncInteger

Code:
JASS:
library SpacebarDetect initializer Init requires SyncInteger
/***************************************************************
*
*   v1.0.3 by TriggerHappy
*   ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
*
*   Allows detection of when the spacebar is pressed. This library does not
*   work with a locked camera. You must also use SetCameraQuickPositionEx
*   if you want to set a custom spacebar point.
*   _________________________________________________________________________
*   1. Installation
*   ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
*   Copy the script and it's requirements to your map and save it (requires JassHelper *or* JNGP)
*   _________________________________________________________________________
*   2. API
*   ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
*       function TriggerRegisterSpacebarEvent takes trigger t returns nothing
*       function OnSpacebarPress takes filterfunc func returns triggercondition
*       function SpacebarRemoveEvent takes triggercondition func returns nothing
*
*       function SetCameraQuickPositionEx takes real x, real y returns nothing
*
***************************************************************/
    globals
        // config
        private constant real CHECK_INTERVAL = 0.03
       
        private constant real DETECT_X = 0.142 // random
        private constant real DETECT_Y = 0.241
        // end
   
        private real FireEvent = 0
        private trigger EventTrig = CreateTrigger()
   
        private integer NotifyID = 0
        private real QP_X = DETECT_X + 0.1
        private real QP_Y = DETECT_Y + 0.1
       
        private SelectionSync Synchronizer = 0
    endglobals

    function OnSpacebarPress takes filterfunc func returns triggercondition
        return TriggerAddCondition(EventTrig, func)
    endfunction

    function SpacebarRemoveEvent takes triggercondition func returns nothing
        call TriggerRemoveCondition(EventTrig, func)
    endfunction

    function TriggerRegisterSpacebarEvent takes trigger t returns nothing
        call TriggerRegisterVariableEvent(t, SCOPE_PRIVATE + "FireEvent", EQUAL, 1)
    endfunction

    function SetCameraQuickPositionEx takes real x, real y returns nothing
        set QP_X = x
        set QP_Y = y
    endfunction

    private function FireEvents takes nothing returns nothing
        set FireEvent = 1
        set FireEvent = 0.0
        call TriggerEvaluate(EventTrig)
    endfunction

    private function OnSync takes nothing returns boolean
        if (GetSyncedInstance() == Synchronizer) then
            call FireEvents()
        endif
       
        return false
    endfunction

    private function OnUpdate takes nothing returns nothing
        local real x = GetCameraTargetPositionX()
        local real y = GetCameraTargetPositionY()
       
        call SetCameraQuickPosition(DETECT_X, DETECT_Y)

        if (x == DETECT_X and y == DETECT_Y) then
            //call BJDebugMsg("yes")
            call SetCameraPosition(QP_X, QP_Y)
           
           call Synchronizer.syncValue(GetLocalPlayer(), 1)
        endif
    endfunction

    private function Init takes nothing returns nothing
        set Synchronizer = SelectionSync.create() // creates many unused dummy units, will fix in future
   
        call TimerStart(CreateTimer(), CHECK_INTERVAL, true, function OnUpdate)
   
        call OnSyncInteger(Filter(function OnSync))
    endfunction

endlibrary

Example:

JASS:
scope SpacebarTest initializer Init

    private function Actions takes nothing returns nothing
        local player p = GetTriggerPlayer()

        call BJDebugMsg(GetPlayerName(p) + " pressed the spacebar.")
    endfunction

    //===========================================================================
    private function Init takes nothing returns nothing
        local trigger t = CreateTrigger()
        call TriggerRegisterSpacebarEvent(t)
        call TriggerAddAction(t, function Actions)
    endfunction

endscope
 

Attachments

  • spacebar.w3x
    42.9 KB · Views: 192
Last edited:
Level 3
Joined
Dec 19, 2013
Messages
32
When I try to save, I get an error saying "User is not a type that allows . syntax" on the function OnMapStart.
 
Top