• 🏆 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!

How to get selecting player.

Status
Not open for further replies.
Level 12
Joined
Mar 24, 2011
Messages
1,082
  • Events
    • Unit - Tavern Main 0021 <gen> Is selected
The title says pretty much all.



Note: Don't suggest using
  • Player - Player 1 (Red) Selects a unit
  • Player - Player 2 (Blue) Selects a unit
  • . . .
I only ask how to get the selecting player for
  • Unit - Tavern Main 0021 <gen> Is selected
if its possible
 
Level 29
Joined
Oct 24, 2012
Messages
6,543
this should work
u just have to make the event

JASS:
function selected takes nothing returns integer
    local integer p = 0
    loop
        exitwhen p > 17
        if IsUnitSelected( tavern main, Player(p)) // change tavern main to ur tavern
            return p
        endif
        set p = p + 1
    endloop
    return 18
endfunction

function unitSelectedActions takes nothing returns nothing
    local integer p = call selected() // this should return the player selecting the tavern
endfunction
 
Level 12
Joined
Mar 24, 2011
Messages
1,082
Looks promising. Will try.

Edit//
Made a new map only to try bugging with this with 0 success...

Here is what I did and the syntax check:

JASS:
unction selected takes nothing returns integer
    local integer p = 0
    loop
        exitwhen p > 17
        if IsUnitSelected( gg_unit_Hpal_0000, Player(p))
            return p
        endif
        set p = p + 1
    endloop
    return 18
endfunction

function unitSelectedActions takes nothing returns nothing
    local integer p = call selected()
    call SelectUnitForPlayerSingle( gg_unit_Hamg_0001, Player(p) )
endfunction

//===========================================================================
function InitTrig_Untitled_Trigger_001_Copy takes nothing returns nothing
    set gg_trg_Untitled_Trigger_001_Copy = CreateTrigger(  )
    call TriggerRegisterUnitEvent( gg_trg_Untitled_Trigger_001_Copy, gg_unit_Hpal_0000, EVENT_UNIT_SELECTED )
    call TriggerAddAction( gg_trg_Untitled_Trigger_001_Copy, function unitSelectedActions )
endfunction


newpicturem.jpg
 
Last edited:
Level 10
Joined
Dec 15, 2012
Messages
650
[Trigger=Trigger]Trigger
Events
Player - Player 1 (Red) Selects a unit
Player - Player 2 (Blue) Selects a unit
Player - . . .
Conditions
(Triggering unit) Equal to Tavern 0021 <gen>
Actions
Set Player = (Triggering Player)[/Trigger]
Aren't you want this ?? xd
 
Level 12
Joined
Mar 24, 2011
Messages
1,082
Edit// Deleted

I managed the units (I think...) but still crashes.
Now it looks like
  • Untitled Trigger 004
    • Events
      • Map initialization
    • Conditions
    • Actions
      • Set TempUnit2 = Archmage 0001 <gen>
  • Untitled Trigger 002
    • Events
      • Unit - Paladin 0000 <gen> Is selected
    • Conditions
    • Actions
      • Set TempUnit = (Triggering unit)
      • Trigger - Run Untitled Trigger 003 <gen> (ignoring conditions)
This is Untitled Trigger 003 <gen>
JASS:
globals
    unit TempUnit
    unit TempUnit2
endglobals
    
function selected takes nothing returns integer
    local integer p = 0
    loop
        exitwhen p > 17
        if IsUnitSelected( TempUnit, Player(p))
            return p
        endif
        set p = p + 1
    endloop
    return 18
endfunction

function unitSelectedActions takes nothing returns nothing
    local integer p = call selected()
    call SelectUnitForPlayerSingle( TempUnit2, Player(p) )
endfunction

//===========================================================================
function InitTrig_Untitled_Trigger_003 takes nothing returns nothing
    set gg_trg_Untitled_Trigger_003 = CreateTrigger(  )
    call TriggerAddAction( gg_trg_Untitled_Trigger_003, function unitSelectedActions )
endfunction
syntaxcheck2.jpg
 
Last edited:
You CANNOT use the word call in the middle of ANY statement.
It only belongs in the beginning.

Also, every if statement is of the form:

JASS:
if (expression) then
elseif (expression) then
elseif (expression) then
else
endif

where the elseif and else lines are completely optional.

You forgot the then keyword in there.

This should compile for you:

JASS:
globals
    // Global and Local variables are written likeThis.
    // First letter is lowercased and the beginning of every word is capitalized.
    unit tempUnit = null // initialize it
    unit tempUnit2 = null // initialize it
endglobals

// selected isn't a proper name, so I changed the function name.
function GetSelectingPlayer takes nothing returns integer
    // It's better to use variable names like this.
    // If you leave your code and come back to it after 
    // several months, you wouldn't have to spend a lot of time 
    // figuring out what each variable/function was for.
    local integer playerId = 0
    loop
        exitwhen playerId == 16
        if IsUnitSelected(tempUnit, Player(playerId)) then
            return playerId
        endif
        set playerId = playerId + 1
    endloop
    return -1
endfunction

// FunctionNamesAreWrittenJustLikeThis
function UnitSelectedActions takes nothing returns nothing
    local integer playerId = GetSelectingPlayer()
    
    if playerId != -1 then
        call SelectUnitForPlayerSingle(tempUnit2, Player(playerId))
    endif
endfunction

//===========================================================================
function InitTrig_Untitled_Trigger_003 takes nothing returns nothing
    set gg_trg_Untitled_Trigger_003 = CreateTrigger(  )
    call TriggerAddAction( gg_trg_Untitled_Trigger_003, function UnitSelectedActions )
endfunction

edit
What I noticed is the fact that you're managing completely different unit variables.
TempUnit in the GUI trigger is actually supposed to be udg_TempUnit in JASS.
You're declaring TempUnit and TempUnit2 in the globals block, so they're completely different variables and you thus aren't modifying them in the GUI code.

edit
Here you go, this should work:

JASS:
function GetSelectingPlayer takes nothing returns integer
    local integer playerId = 0

    loop
        exitwhen playerId == 16

        if IsUnitSelected(udg_TempUnit, Player(playerId)) then
            return playerId
        endif

        set playerId = playerId + 1
    endloop

    return -1
endfunction

// FunctionNamesAreWrittenJustLikeThis
function UnitSelectedActions takes nothing returns nothing
    local integer playerId = GetSelectingPlayer()
    
    if playerId != -1 then
        call SelectUnitForPlayerSingle(udg_TempUnit2, Player(playerId))
    endif
endfunction

//===========================================================================
function InitTrig_Untitled_Trigger_003 takes nothing returns nothing
    set gg_trg_Untitled_Trigger_003 = CreateTrigger(  )
    call TriggerAddAction( gg_trg_Untitled_Trigger_003, function UnitSelectedActions)
endfunction
 
Last edited:
Level 12
Joined
Mar 24, 2011
Messages
1,082
Thank you ! Works now. Perfectly.
The variables were like udg_TempUnit but when it didn't work I thought to mess a bit with it.

Two questions:
1) A variable name (of local and global) starting with a capital letter isn't a valid variable names ?
2)Function name starting with lowercase letter isn't a valid function name or it's some kind of a standart used by coders ?
 
Status
Not open for further replies.
Top