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

Basics - Loops

Status
Not open for further replies.
Level 2
Joined
Jul 12, 2018
Messages
17

Tasks

Code



Create a function, that runs on map initialiaztion.
The function's goal is to create <Footman Amount> footmen for Player_1 using a loop.


Insert a new condition to exit the loop from Part 1 -- now the loop should (also) exit if Player_1's currently used food level is above "3".


Create a new function, that also runs on map initialization.
The function's goal is to loop through all players, and to create <Footman Amount> footmen units for all of the players with using a loop, as well.



JASS:
scope Part1n2n3 initializer main
    globals
        integer fmanAmount = 5
        integer foodLimit = 3
    endglobals
   
    private function main2 takes nothing returns nothing
        local integer i = 1
        local integer u = 0
        local real x
        local real y
       
        loop
            exitwhen i > bj_MAX_PLAYERS
           
            loop
                exitwhen u == fmanAmount or GetPlayerState(Player(i), PLAYER_STATE_RESOURCE_FOOD_USED) > foodLimit
               
                set x = GetRandomReal(GetRectMinX(bj_mapInitialPlayableArea), GetRectMaxX(bj_mapInitialPlayableArea))
                set y = GetRandomReal(GetRectMinY(bj_mapInitialPlayableArea), GetRectMaxY(bj_mapInitialPlayableArea))
                call CreateUnit(Player(i), 'hfoo', x*Cos(GetRandomReal(0, 359)*bj_DEGTORAD), y*Sin(GetRandomReal(0, 359)*bj_DEGTORAD), .0)
               
                set u = u + 1
            endloop
           
            set u = 0
            set i = i + 1
        endloop
       
    endfunction
   
    private function main takes nothing returns nothing
        local integer i = 0
        local real x = 300 //X distance between footman and center of map
        local real y = 300 //Y "
       
        loop
            exitwhen i == fmanAmount or GetPlayerState(Player(0), PLAYER_STATE_RESOURCE_FOOD_USED) > foodLimit
           
            call CreateUnit(Player(0), 'hfoo', x*Cos(((360/fmanAmount)*i)*bj_DEGTORAD), y*Sin(((360/fmanAmount)*i)*bj_DEGTORAD), .0)
           
            set i = i + 1
        endloop
       
        //I think this 'll do
        call main2()
    endfunction
endscope

 

Attachments

  • [Crash Course] Basics - Loops.w3m
    23.9 KB · Views: 81
  • you can declare global variables as constant if they don't need to be changed
  • in the function main2, Player(0) gets no footman anymore, because loop starts with Player(1)
  • the max foot condition in main2 inside loop is not wanted
  • u is not very intuitive integer name imo. By name one would think of a local unit variable.
But the goal is to show knowledge about loops, and you definitly got it. Solved.
 
Status
Not open for further replies.
Top