- Joined
- Mar 11, 2017
- Messages
- 587
Preparation
Following global variables must be declared and used (<type> , <name>):
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.
Following global variables must be declared and used (<type> , <name>):
- integer Counter[array]
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]
Attachments
Last edited: