• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[Trigger] How to detect spacebar press?

Status
Not open for further replies.

Chaosy

Tutorial Reviewer
Level 41
Joined
Jun 9, 2011
Messages
13,239
There are two ways that I know of. Though one is actually pretty useless in practice.

1 (useless). Use custom keybinds and make an ability with the spacebar, then detect when ability is cast with triggers. There is a tutorial for the keybinds on the site somewhere. http://www.hiveworkshop.com/forums/miscellaneous-tutorials-456/extended-hotkeys-spacebar-etc-245278/

2. Use a loop and check for the camera location, if it is equal to the players starting location space is pressed. There is also a jass recourse for this. http://www.hiveworkshop.com/forums/jass-resources-412/snippet-press-spacebar-event-213395/
 
Level 11
Joined
Oct 9, 2015
Messages
721
Why is the first one useless? Can it be used in multiplayer games ? I ask this because you need to edit some files.

The snippet is useful only in singleplayer mode.

How can I check for the camera location ?

I was in need for a multiplayer function
 

Chaosy

Tutorial Reviewer
Level 41
Joined
Jun 9, 2011
Messages
13,239
err.

If you have a unit with an ability with the spacebar keybind, that unit has to be permanently selected to detect the spacebar being pressed.

The icon of the ability is just an annoying thing but I would prefer if it was possible with an active item ability because it has no icons. Not sure if that exists though, I only know of passive ones.

Also if you manually click on the ability it will yell that spacebar is pressed.
 
Level 11
Joined
Oct 9, 2015
Messages
721
You're talking about the "extended hotkeys" thing, right? It's usable in multiplayer too or only locally (single player) ? I ask this because you need to modify files so that world editor can set different hotkeys in the object editor.
 
Level 11
Joined
Oct 9, 2015
Messages
721
Thanks a lot for your help, I'll try and implement it in my map :D

EDIT: One last question: is it possible to set the numbers (the ones over the letters) as ability hotkeys ? What about the use of itens throught those numbers too ?
 
Last edited:
Level 24
Joined
Aug 1, 2013
Messages
4,658
In the snippet there is a piece of code that executes the event:
JASS:
            if ((x == Space_X) and (y == Space_Y)) then
                call TriggerEvaluate(Space_Actions)
            else

This part runs in local blocks even though you dont see it immediately.
What happens is that "x" and "y" are different for each client (player) and they may be only equal to Space_X and Space_Y for you but not for me.
So the TriggerEvaluate() only runs for you.

However, TriggerEvaluate() may NEVER be used in local blocks cause it executes the trigger and will cause a desync.
But if you instead do not do the trigger evaluation, but do your actions there, you have made it multiplayer compatable.

Be aware that in that part, you are coding within local blocks, so you cant create units, add some gold or whatever.

What you could do is select a unit and order it to cast a specific ability via hotkeys so the event for casting the ability runs for everyone and you have a spacebar event for everyone and for multiplayer, but that messes up your selection whenever you do so... so be careful with what you want.

If you for example only want to select your main unit and set the camera location to it, then you can do that without having to bother with other clients.
 
Level 12
Joined
Mar 13, 2012
Messages
1,121
The snippet works in multiplayer as long as you keep your actions in local player blocks.

streetpunk, Wietlol is talking about the [Snippet] Press Spacebar Event, not about the extended hotkeys.

The extended hotkeys work without any additional work in multiplayer. But mind two things:

1)to my knowledge this was never tested on Mac, so it might not work there.
2)this is not officially supported, so there is a slim chance that future wc3 patches might break this functionality.
 
Level 12
Joined
Mar 13, 2012
Messages
1,121
The AbilityMetaData.slk file you have to change on your PC is only that the Object Editor lets you input numbers in the hotkey field.

Which numbers you type in there is totally up to you and is saved in the map, so your special hotkeys are delivered to every player.
 
Level 24
Joined
Aug 1, 2013
Messages
4,658
JASS:
library SpaceHandlerLocal initializer Init // by D.O.G. version 6.0
    
    private function SpacebarAction takes nothing returns nothing
        call DisplayTimedTextToPlayer(GetLocalPlayer(), 0.0, 0.0, 1.0, "You have pressed spacebar!")
    endfunction
    
    private function BackspaceAction takes nothing returns nothing
        call DisplayTimedTextToPlayer(GetLocalPlayer(), 0.0, 0.0, 1.0, "You have pressed backspace!")
    endfunction
    
    /* SETTINGS */
    globals
        private constant real Period = 0.01562500
        // Unit must have TOWN HALL classification!!!
        private constant integer DummyUnit = 'u000'
    endglobals
    /* SETTINGS END */
    
    globals
        private real Space_X = 1000000000.0
        private real Space_Y = 1000000000.0
        private real Backspace_X = 1000000000.0
        private real Backspace_Y = 1000000000.0
        private real Lock_X = 1000000000.0
        private real Lock_Y = 1000000000.0
        private boolean Space_Enabled = false
        private boolean Backspace_Enabled = false
        private timer Timer = CreateTimer()
        private trigger Space_Actions = CreateTrigger()
        private trigger Backspace_Actions = CreateTrigger()
        private unit Backspace_Unit = null
        private unit Lock_Unit = null
        private integer Lock_Mode = 0
    endglobals
    
    /*
    function AddSpacebarAction takes code a returns triggercondition
        return TriggerAddCondition(Space_Actions, Condition(a))
        return null
    endfunction
    function AddBackspaceAction takes code a returns triggercondition
        return TriggerAddCondition(Backspace_Actions, Condition(a))
        return null
    endfunction
    function RemoveSpacebarAction takes triggercondition c returns nothing
        call TriggerRemoveCondition(Space_Actions, c)
    endfunction
    function RemoveBackspaceAction takes triggercondition c returns nothing
        call TriggerRemoveCondition(Backspace_Actions, c)
    endfunction
    function ClearSpacebarActions takes nothing returns nothing
        call TriggerClearConditions(Space_Actions)
    endfunction
    function ClearBackspaceActions takes nothing returns nothing
        call TriggerClearConditions(Backspace_Actions)
    endfunction
    */
    function EnableSpacebarEvent takes boolean enable returns nothing
        local integer p
        set Space_Enabled = enable
        if not enable then
            set p = GetPlayerStartLocation(GetLocalPlayer())
            call SetCameraQuickPosition(GetStartLocationX(p), GetStartLocationY(p))
        endif
    endfunction
    function EnableBackspaceEvent takes boolean enable returns nothing
        set Backspace_Enabled = enable
        if not enable then
            call RemoveUnit(Backspace_Unit)
        endif
    endfunction
    function LockCameraToUnit takes unit u returns nothing
        set Lock_Unit = u
        set Lock_Mode = 1
    endfunction
    function LockCameraToPoint takes real x, real y returns nothing
        set Lock_X = x
        set Lock_Y = y
        set Lock_Mode = 2
    endfunction
    function LockCameraToPointLoc takes location loc returns nothing
        set Lock_X = GetLocationX(loc)
        set Lock_Y = GetLocationY(loc)
        set Lock_Mode = 2
    endfunction
    function UnLockCamera takes nothing returns nothing
        set Lock_Unit = null
        set Lock_Mode = 0
    endfunction
    
    private function Handler takes nothing returns nothing
        local real x = GetCameraTargetPositionX()
        local real y = GetCameraTargetPositionY()
        if Lock_Mode == 1 then
            set Lock_X = GetUnitX(Lock_Unit)
            set Lock_Y = GetUnitY(Lock_Unit)
        endif
        if Space_Enabled then
            if ((x == Space_X) and (y == Space_Y)) then
                //call TriggerEvaluate(Space_Actions)
                call SpacebarAction()
            else
                set Space_X = x
                if Lock_Mode > 0 then
                    call PanCameraTo(Lock_X, Lock_Y)
                endif
            endif
            set Space_Y = y + 0.01
            call SetCameraQuickPosition(Space_X, Space_Y)
        endif
        if Backspace_Enabled then
            if ((x == Backspace_X) and (y == Backspace_Y)) then
                //call TriggerEvaluate(Backspace_Actions)
                call BackspaceAction()
            else
                set Backspace_Y = y
                if Lock_Mode > 0 then
                    call PanCameraTo(Lock_X, Lock_Y)
                endif
            endif
            set Backspace_X = x + 0.01
            call RemoveUnit(Backspace_Unit)
            set Backspace_Unit = CreateUnit(GetLocalPlayer(), DummyUnit, Backspace_X, Backspace_Y, 270.0)
            call SetUnitPosition(Backspace_Unit, Backspace_X, Backspace_Y)
        endif
    endfunction
    
    private function Init takes nothing returns nothing
        call TimerStart(Timer, Period, true, function Handler)
    endfunction
    
endlibrary

This should work.
Within the first two functions you can do whatever you want to do and they behave exactly like "GetLocalPlayer() blocks" so no net traffic.

Backspace doesnt work for some reason but I think that is because of the example that I used.

Also, that resources should probably use a margin of error to make it also work when the camera is moving... but that is something for other people.

And yes... I was talking about the spacebar one and not the hotkey one.
 
Status
Not open for further replies.
Top