JASS 101 Classroom

Status
Not open for further replies.
JASS 101: Basic Jass Classrom
Lesson 4: Loops & If / Then / Else

Greeting

Here we are already, at the fourth lesson. The last lesson’s assignment was:

Well, this is the end of this lesson (which was surprisingly short), for your "home work" you must create a function that gets inside local variables the triggering unit, the target unit of the spell and it's location as well the owners of both units. After that increase the gold value of the owner of the casting unit by 200, and reduce the owner of the target's gold by 200. Lastly move the caster to the target's location.

The best works were made by:

DrinkSlum
Purgeandfire
Andrewgosu

The all got 100/100 for their assignments.

JASS:

function Trig_Untitled_Trigger_001_Actions takes nothing returns nothing
local unit caster=GetTriggerUnit() // The caster is stored in a local
local unit target=GetSpellTargetUnit() // The target is stored in a local
local player casterOwner=GetOwningPlayer(caster) // The owner of the caster is stored in a local
local player targetOwner=GetOwningPlayer(target) // The owner of the target is stoed in a local
local location targetLoc=GetUnitLoc(target) // The location of the target is stored in a local

call SetPlayerState(casterOwner,PLAYER_STATE_RESOURCE_GOLD,GetPlayerState(casterOwner,PLAYER_STATE_RESOURCE_GOLD)+200) // We set casterOwner’s gold to a new one
call SetPlayerState(targetOwner,PLAYER_STATE_RESOURCE_GOLD,GetPlayerState(targetOwner,PLAYER_STATE_RESOURCE_GOLD)+200) // We set targetOwner’s gold to a new one

call SetUnitLocPositionLoc(caster,targetLoc) // We move caster to targetLoc
endfunction


This was enough to get a perfect grade.

Here is the lesson plan:
Lesson 4: Loops & If / Then / Else
What are Loops?
What is If / Then / Else?
Conclusion
Assignment

I suggest reading the lesson in the order presented, skipping parts may shorten the read but in the future it will only harm you

What are Loops?

In this chapter loops will be explained in detail, what are they, how the can be used and what is the dangers when using the

What are loops? Loops store in themselves actions (you can think of them as mini-triggers) which will run until a condition is met. For example you can create a loop that will run 15 times, and each time it will create a unit.

How do we start loop? Well it’s simple you type in a new line “loop”

JASS:
 loop


After that you need to declare a condition, this can be done like so:

JASS:
 exitwhen condition


Where’s condition is a condition that can change. After that you type the actions that will be repeated and then close the loop by typing endloop. Here is an example:

JASS:

local integer i=3
loop
  exitwhen i>5
  // Here we’ll do some random actions, this part has nothing to do with out chapter
  set i=i+1
endloop


As you can see what this loop will do is run until i (which is 3) will be bigger then 5 where we increase i’s size every run (set i=i+1). This could be done also like this:

JASS:

local integer i_2=-5
loop
  exitwhen i>-3
  // actions
  set i=i+1
endloop


As you can see, the two loops will run the same amount of times (2 times each). The best thing in Jass’es loops is that they are flexible. Unlike GUI loops, where you run until some integer is equal to another you can do anything in JASS loops.

JASS:

  exitwhen GetPlayerState(arguments) == 200
  exitwhen LocalUnitVar==null

etc


Those are just two out of the many examples that can serve as exitwhen condition, the JASS loops can introduce you to a whole new world of possibilities when working with loops and totally edit them, doing whatever you want.

However, while JASS loops are great there are 2 problems with them. The first one is that that the loops might cause world editor to crash if you forget to add at the end of the loop ‘endloop’. Secondly a bad exitwhen condition will cause the game to crash because the loop will last forever. Here is an example

JASS:

loop 
  exithwen 5555 > 9090905
  // Blah blah
endloop


5555 will never be bigger then 9090905, thus the loop will run until the game will crash.

Challenge 1: Create a loop that will run until a unit steps into a region.




If you did the following you get 100 points:

JASS:

loop
  exitwhen RectContainsUnit(SomeRect, SomeUnit)
endloop


Challenge 2: Create a loop that will create 5 soldier for player.



Solution:
JASS:

local integer i=1

loop
  exitwhen 1>5
  CreateUnitAtLoc(RandomPlayer,SomeID,SomeLoc,SomeReal)
  set i=i+1
endloop


One more thing before we can move to if / then / else actions. Unlike GUI you can force a loop to stop. In certain situations this can be extremely helpful to you, since sometimes you would want to “skip remaining actions” but only inside a loop. How can this be done? Easily, remember that we said we need to set a condition for the loop so it will not run forever? Well, how do we set the condition? “Exitwhen some condition”. Understanding that we can say that once we get “exitwhen true” the loop will stop. Thus the line

JASS:

exitwhen true


Will stop the loop immediately. For example this loop:

JASS:

loop 
exitwhen true
// do something
endloop


Will not run even one time because we quit it immediately.

So those are basic loops, not much to say about those because they vary so much, let’s move on to If / Then / Else actions.

What is If / Then / Else

I’ll discuss in this chapter if / then / else action, how to work with it and what is it’s dangers

Loops were easy right? Well so is if / then /else action. If / Then / Else like loops is an action that contain actions in it. The way if / then /else works is like so: You declare a condition that the If / Then / Else action will refer to. If the condition is true it will do one set of actions, if the condition is false it will do other set of actions. Like in loops we need to end the if / then / else were world editor will crash. We use endif instead of endloop. This is how we create if / then /else action:

JASS:

if somecondtion then
  // The actions that will run when the condition is true
else 
  // The other set of actions
endif


There are few ways to work with condition. If you are referring to a Boolean then you can do

JASS:

if SomeBoolean then
etc.

The action that I just showed is exactly like:

JASS:

if SomeBoolean ==true then
etc.


Also

JASS:

if not(SomeBooleab) then
etc


Is

JASS:

if SomeBoolean == false then
etc.


When working with integer / reals you might want to check sizes. This can be done easily following this list:

Code:
== Equal to…
=! Not equal to…
>= Greater or equal to…
=< Smaller or equal to…
> Greater then…
< Smaller then..

Example:

JASS:

if 3 &gt; 5 then
  // These actions will never run because 3 can’t be bigger then 5.
else
  // These actions will run because 3 is smaller then 5
endif


Challenge 3: Create if / then / else action that check which player got more gold, blue or red.



Solution:

JASS:

if GetPlayerState(Player(0),PLAYER_STATE_RESOURCE_GOLD)&gt;GetPlayerState(Player(1),PLAYER_STATE_RESOURCE_GOLD) then
  // Actions
else
  // Other actions
endif


There are two last things you need to know before you have a complete basic knowledge of if / then / else. The first thing is, is that you don’t have to put the else line. For example, you want to have only one set of actions that will run only if the condition is true. Very well no need to add the else line. For example:

JASS:

if condition then
do something
endif


this is legal, what it would do is simply run actions only when the condition is true but will ignore it if it is false.

The other thing is that there is another command called “elseif”. Elseif much like it sounds is else that calls an immediate if / then / else actions.

For example:

JASS:

if condition then
something
elseif condition then
something
endif


This is the exact same thing as this:

JASS:

if condition then
something
else
if condition then
something
endif
endif


And that’s it! You have a basic knowledge of if / then / else actions and loops.

Conclusion

We know then that if / then / else and loops must be closed or they crash world editor.
When working with loops we define a condition that will cause the loop to stop, and the loop will run the actions in it until the condition is met. If / Then / Else actions will get a condition, if the condition is true one set of the actions will run, else other set will run. Loops can be forced to stop easily and if / then / else has few shortcuts to ease our lives.

Assignment

Create a loop that will run until player red has 9000 gold. Each run give him 500 gold more. If he player red has more then 2000 gold, then remove 100 of his gold, if he has over 5000 then remove 300 of his gold if he has over 800 decrease it by 400. In other words:

Code:
Create a loop that will run until player red has more then 9000 gold.
  Each time the loop runs add the player 500 gold.
  Create an if then else action/s,
    If player red has over 2000 gold then remove 100 from his current gold
    If player red has over 5000 gold then remove 300 from his current gold
    Finally if player red has over 8000 gold remove 400 from his current gold
 

Attachments

  • JASS 101 Lesson 4.doc
    41.5 KB · Views: 359
1:45 in the morning??? You ees crazee!
Ok, done. Handing in.

However I feel that if/then/elses are pretty self-explanatory :p
 
If it weren't what good would it be?
No matter what the 3rd condition would always be true :rolleyes:
So yes, inside the loop.
 
Does it matter?
If it's before it adds like 1 extra loop. That takes what? A millisecond? microsecond???
lol, just bothering you.
Think about it though...
 
He didn't say so, so just assume you don't.
So pretty much you will instantly get 9000 gold or whatever, but it's the fact you did the coding right I guess.
 
"that's what we used in the old days"
..
lol
sad.
Anyways, get JASSCraft or JASSEditor as he said. I don't make anything in WE really...
 
> 1:45 in the morning

It was 10 A.M. here when I posted the thread, we live in different parts of the world. ;)

> Should the if/then/elses be in the loop?

Yes.

> Should it check the gold before you add it or after you add it?

After you add the 500 gold you should check how much gold the player has, but it doesn't really matter, just for pratice.

> DO i need wait on the loop

No need.

Thanks to Tom Jones's comments, the lesson will be updated soon enough, I will bump this thread when it does.

Please excuse me in advanced for double posting.
 
I just have no time to make the assignment.. I'm far away from home right now, so I don't have WE... Anyway, I brought JassCraft with me and created this function... I wonder if its correct.. haven't Tested it...

_____________________________________________________________________________________
Deleted... ^_^
 
Status
Not open for further replies.
General chit-chat
Help Users
  • No one is chatting at the moment.
  • The Helper The Helper:
    Love you man so glad you are still here
  • Varine Varine:
    I live a very boring life otherwise.
  • Varine Varine:
    23AndMe is apparently looking to sell the company. I wonder what that means for all the genetic data they have. I don't have an account for The Atlantic so I can't read the rest of their news, but presumably whoever would buy the company would then have access to that, and I don't think there are any HIPPA or equivalent protections on it
  • Varine Varine:
    I'd bet it would be a treasure trove for pharmaceutical manufacturing and genetic research. I know they sold some genetic information a few years ago but they did that without identifying information attached
  • Varine Varine:
    It's be like a really boring biopunk dystopia. My TV started giving me ads when I turn it on recently (which is real fucking cool), and now I'm imagining it just being like "bioinformation says you have cancer. Get a prescription for Avastin today" and then you get connected to someone that is technically a doctor but really mostly just selling drugs to people right on your TV since they have cameras now to give you a prescription.
  • Varine Varine:
    So, buy stocks in whoever buys them would be my bet
  • Varine Varine:
    I wish I had time to keep up on things like this. My investment portfolio would probably look a bit better.
  • N Natalie the gay boss:
    So I am starting a thread.pls search how to make the teacher mad.
  • Ghan Ghan:
    Hive is 20 years old today.
  • The Helper The Helper:
    No way - holy shiznit!
  • The Helper The Helper:
    love that it is still going strong too!
  • The Helper The Helper:
    OMG I cannot handle this Natalie person I hate to do it but ban hammer activated
    +1
  • The Helper The Helper:
    I would rather not have anyone ever post then those kinds of posts
  • Varine Varine:
    lol what were they doing?
  • Varine Varine:
    3D printer is working again. Had to get a bunch of parts, cuz I forgot I kind of modded it and then never finished modding it. Turns out thermistors aren't all the same
  • Varine Varine:
    And as a result I may have gotten the hot end way too hot and caused a small fire
  • Varine Varine:
    Anyway I have a new not end and this one has the right things
  • Varine Varine:
    Although I get these really weird artifacts, I think its from my bed not being clean. Not really sure, I've seen this before though and I think cleaning the build plate fixed it. Eh, I'll figure it out, worst case scenario it rips the rest of the model off and it catches fire again
  • Varine Varine:
    I'm now realizing I could have cut out like 99% of this print and it would have been fine cuz I just need to see if a frame will fit over an LCD
  • Varine Varine:
    Oh well
  • Varine Varine:
    I'm not an engineer
  • Varine Varine:
    Although this filament is kind of expensive, I should probably be a bit more wary of that.
  • The Helper The Helper:
    I love the whole concept of 3D Printers and am very happy you have one so I can hear about them
  • The Helper The Helper:

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top