- Joined
- Mar 11, 2017
- Messages
- 587
So here's a much lower level-of-skills submission than you're used to see nowadays. A Jass exercise, as simple as it can get. 
Proposed solution
Premise - known errors:
Part 1 - Random footman creation
Part 2- Footman death
Log of issues and problems encountered during typing:
I also made other mistakes of smaller significance that I corrected without describing them up here, and there's still those that I'm not aware of which will be caught by the reviewer. I thank in advance for the corrections and the possibility of learning something bit by bit.
Part 1
Create a (JASS) trigger that runs when a player types "-" as exact string.
The trigger's goal is to create a footman on the map at a random position.
The unit must be created within the map's playable area.
Increase the "Counter" variable by 1 each time the trigger runs.
Part 2
Create a new trigger (in the same trigger sheet) that runs when a unit dies.
When the trigger runs, the "Counter" must be decreased by 1, furthermore
the "Counter" 's value must be displayed on screen to get to know how many footmen are alive.
Requirements
Following global variables must be declared and used (<type> , <name>):
Create a (JASS) trigger that runs when a player types "-" as exact string.
The trigger's goal is to create a footman on the map at a random position.
The unit must be created within the map's playable area.
Increase the "Counter" variable by 1 each time the trigger runs.
Part 2
Create a new trigger (in the same trigger sheet) that runs when a unit dies.
When the trigger runs, the "Counter" must be decreased by 1, furthermore
the "Counter" 's value must be displayed on screen to get to know how many footmen are alive.
Requirements
Following global variables must be declared and used (<type> , <name>):
- integer Counter
- unit u
- string text
- real x
- real y
Proposed solution
Premise - known errors:
- I fail at [requirements.1] making use of a local unit u
- I fail at [Part 2]:"crate a trigger (in the same trigger sheet)"
- [Part 1] I forgot to substitute the local "text" to the literal "-" inside of the event. So it remained:
vJASS:
call TriggerRegisterPlayerChatEvent(t, Player(i), "-", true)
- (The testmap has a hero preplaced, merely to quickly kill footmen for testing, in a completely triggerless way)
Part 1 - Random footman creation
vJASS:
globals
integer Counter = 0
integer FtUniTyp = 'hfoo'
endglobals
function RndmFootActions takes nothing returns boolean
local player p = GetTriggerPlayer()
local real x = GetRandomReal(GetRectMinX(bj_mapInitialPlayableArea), GetRectMaxX(bj_mapInitialPlayableArea))
local real y = GetRandomReal(GetRectMinY(bj_mapInitialPlayableArea), GetRectMaxY(bj_mapInitialPlayableArea))
//The trigger's goal is to create a footman on the map at a random position. The unit must be created within the map's playable area. Increase the "Counter" variable by 1 each time the trigger runs.
// Counter isn't player-specific
call CreateUnit(p, FtUniTyp, x, y, GetRandomReal(0., 359.))
set Counter = Counter + 1
call BJDebugMsg("A "+ GetObjectName(FtUniTyp) +" was created. There are now " + I2S(Counter) + " units of type: " + GetObjectName(FtUniTyp))
return false
endfunction
function InitTrig_CraCo1 takes nothing returns nothing
local trigger t = CreateTrigger()
local string text = "-"
local integer i = 0
//runs when a player types "-" as exact string. any player
loop
exitwhen i > 11
call TriggerRegisterPlayerChatEvent(t, Player(i), "-", true)
set i = i + 1
endloop
call TriggerAddCondition(t, Condition(function RndmFootActions))
set t = null
endfunction
Part 2- Footman death
vJASS:
function FootDeathActions takes nothing returns boolean
if (GetUnitTypeId(GetTriggerUnit()) == FtUniTyp) then
set Counter = Counter - 1 //counter defined in CraCo1
call BJDebugMsg("A "+ GetObjectName(FtUniTyp) +" died. There are now " + I2S(Counter) + " remaining.")
return false
endif
return false
endfunction
function InitTrig_FootDeath takes nothing returns nothing
local trigger t = CreateTrigger()
call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_DEATH)
call TriggerAddCondition(t, Condition(function FootDeathActions))
set t = null
endfunction
Log of issues and problems encountered during typing:
'unittype's in jass are completely different from 'Unit Type's in GUI. In jass they represent broad categories of unit types based on a property. Therefore I had some difficulties finding out how to obtain what I am used to know as "unit type" from GUI. Found help in the thread: How do I add a "Unit Type" condition to a Jass function?
vJASS:
return IsUnitType(u, 'hfoo') // doesn't do what i hope
vJASS:
return (GetUnitTypeId(GetTriggerUnit()) == 'hfoo') // unittypeID is the equivalent to a GUI unit type. It's an integer
This thread: how to print out names of units, items, etc? was helpful to learn of GetObjectName() to do this task.
Question: is it an optimal way to do it?
Question: is it an optimal way to do it?
I didn't null local triggers t inside the Init at first.
From Converting GUI into Efficient JASS : "created local triggers should be nulled (set t = null)"
Question: should all local nonnatives (i.e. handles) be nulled or otherwise explicitly destroyed at the end of a block?
From Converting GUI into Efficient JASS : "created local triggers should be nulled (set t = null)"
Question: should all local nonnatives (i.e. handles) be nulled or otherwise explicitly destroyed at the end of a block?
From Converting GUI into Efficient JASS
Almia said:
merge the conditions and actions and use conditions rather than action for triggers.
Actions creates new thread, conditions don't.
Note: this might not be the best practice or it may even be an irrelevant measure. Please have a look at the discussion at: Mission Solved - Order Tracking - Submission and following interventions.
Almia said:

merge the conditions and actions and use conditions rather than action for triggers.
Actions creates new thread, conditions don't.
Note: this might not be the best practice or it may even be an irrelevant measure. Please have a look at the discussion at: Mission Solved - Order Tracking - Submission and following interventions.
this was a really stupid issue. I didn't know what were the correct restrictions on the naming of the InitTrig_X function. It functioned correctly after I named it like InitTrig_[name of the trigger page].
This is also the reason why I couldn't find a way to have the triggers stay in the same page without resorting to different methods of initialization, offered by vjass, which I still need to learn how to use.
Question: asking for more clarification on this topic please.
This is also the reason why I couldn't find a way to have the triggers stay in the same page without resorting to different methods of initialization, offered by vjass, which I still need to learn how to use.
Question: asking for more clarification on this topic please.
I also made other mistakes of smaller significance that I corrected without describing them up here, and there's still those that I'm not aware of which will be caught by the reviewer. I thank in advance for the corrections and the possibility of learning something bit by bit.
Last edited: