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

Exercise 2 - simplest MPI using array

Status
Not open for further replies.
Level 14
Joined
Mar 11, 2017
Messages
587
Preparation

Following global variables must be declared and used (<type> , <name>):
  1. integer Counter[array]
Local variables can be used.
Part 1

Create a (JASS) trigger that runs when a player presses the 'Esc' button (Skip Cinematic).
The trigger's goal is to count how often a player has hit the 'Esc' button, and to print the correct value on screen.
The trigger must be MPI (tip: always search for things you don't know Elemental Coder Guide).
Part 2

If you don't already have done so, do now the "Part 2" without using any "if statement".
Tip: Each player has a unique player number.
Conclusion

When we have same data to work with with multiple instances, then array[] variables should be used to ensure efficient and clean code.
However, we must always ensure we have some kind of unique index that we can use to ensure that we can properly use the array.

I am worried of situations where 2 or more players spam the esc key at high frequency. I thought that making a trigger for each player would make sure that no player stole the counter increment from another, but I need some comments on my logic from the reviewer because I'm not sure that it's sound and meaningful.
The key question imo is: can esc keypresses from multiple players happen at the same moment, causing the trigger to assign the counter increment to the wrong player, therefore causing loss of the desired MPI property?
(attached at the end is a single-trigger version of this same entry. I fear that that version might have the MPI issues that I suspect here above)


vJASS:
globals
integer array Counter
endglobals

function CraCo2Actions takes nothing returns boolean
    local integer i = GetPlayerId(GetTriggerPlayer())
    set Counter[i] = Counter[i] + 1
    call BJDebuMsg("Player number " + I2S(i+1) + " has been pressing the esc key for " + I2S(Counter[i]) + " times now.")
    return true
endfunction

function InitTrig_CraCo2 takes nothing returns nothing
    local trigger t
    local integer i = 0

    loop
        exitwhen i > 11
        set Counter[i] = 0  //init the player-specific Counter[]
        set t = CreateTrigger()
        call TriggerRegisterPlayerEvent(t, Player(i), EVENT_PLAYER_END_CINEMATIC)
        call TriggerAddCondition(t, Condition(function CraCo2Actions))
        set i = i + 1
    endloop

    set t= null
endfunction

vJASS:
globals
integer array Counter
endglobals

function escountActions takes nothing returns boolean
    local integer j = GetPlayerId(GetTriggerPlayer())
    set Counter[j] = Counter[j] + 1
    call BJDebuMsg("Player number " + I2S(j+1) + " has been pressing the esc key for " + I2S(Counter[j]) + " times now.")
    return true
endfunction

function InitTrig_escount takes nothing returns nothing
    local trigger t = CreateTrigger()
    local integer i = 0

    //runs when a player presses esc. any player
    loop
        exitwhen i > 11
        set Counter[I] = 0
        call TriggerRegisterPlayerEvent(t, Player(i), EVENT_PLAYER_END_CINEMATIC)
        set i = i + 1
    endloop

    call TriggerAddCondition(t, Condition(function escountActions))

    set t= null
endfunction[/I]
SPOILER]
 

Attachments

  • JASS Class - Crash course 2.w3m
    23.6 KB · Views: 44
Last edited:
Level 15
Joined
Mar 25, 2016
Messages
1,327
The key question imo is: can esc keypresses from multiple players happen at the same moment, causing the trigger to assign the counter increment to the wrong player?
No.
JASS:
    local integer i = GetPlayerId(GetTriggerPlayer())
    set Counter[i] = Counter[i] + 1
    call BJDebuMsg("Player number " + I2S(i+1) + " has been pressing the esc key for " + I2S(Counter[i]) + " times now.")
    return true
1. Each time the escape button is pressed the function is executed and a new local variable is created. You can't access the local variable from outside of the function itself.
2. Also JASS is sequential, so no two functions can happen at the same time. If two players would press esc at the same time your function would still be exectued twice, but it would finish, before the second one starts.
Usually when a function starts, it runs until the function is finished or interrupted. You can interrupt a function by using wait for example. Another way to interrupt a function is by triggering an event, which causes another trigger to run or explicitly calling another function. Nothing of this can happen in your trigger, so you can be sure that it is not interrupted, so even if you would use a global variable it could not be overwritten, because no function can run, before your function is finished.
 
Status
Not open for further replies.
Top