Basic JASS Help (takes/returns, how to convert GUI?)

Status
Not open for further replies.
Level 17
Joined
Feb 11, 2011
Messages
1,860
Hello guys,

I have been using GUI for a while now and I am pretty good with it. I have decided to learn JASS now. I have been reading a tutorial which is good, but there are a few things I don't understand. I have a few questions:

1. Please explain what is meant by takes.
2. Please explain what is meant by returns.
3. How would I convert this basic trigger into a JASS script (I have tried converting it to a custom text but that's confusing)?

  • Trigger
    • Events
      • Unit - A unit enters Region1
    • Conditions
      • (Unit-type of (Triggering unit)) Equal to Wisp
    • Actions
      • Set Temp_Point = (Position of (Triggering Unit))
      • Special Effect - Create a special effect at Temp_Point using Abilities\Spells\Human\ThunderClap\ThunderClapCaster.mdl
      • Special Effect - Destroy (Last created special effect)
      • Unit - Kill (Triggering unit)
      • Custom script: call RemoveLocation(udg_Temp_Point)
That's it for now. Thanks,

- Mr_Bean
 
Takes is a keyword for the parameters. A function can take certain parameters to begin its functionality.
JASS:
function ExAuror takes unit u returns nothing
endfunction
This function, in order to work, must get a parameter of a unit id. How would you call it:
JASS:
call ExAuror (GetTriggerUnit())

A function can simultaneously take several parameters, all of them getting separated by a comma:
JASS:
function f takes unit u, inter i, real r returns nothing
endfunction
By the time the function "takes" the unit, the syntax allows to a pseudo-store (check: takes unit u), a name to use it as a reference to the parameter you take. That pseudo-variable doesn't need to be nulled, as you would do in other case.

The "returns" keyword is what a function would return. Let's say you want a function to give you a value, when you relate it to a unit. I will not create a custom function for this, I will instead show you the default function of GetWidgetLife().
This function allows you to check the life of any object, item, unit, destructable.
It goes like: function GetWidgetLife takes unit u returns real. When you add the parameter of "u", it checks the life of the unit and returns that value in real. This is also how the GUI works:
  • Set RealVariable = (Life of (Triggering unit))
You need to learn Jass basics, because if I proceed with coordinates, when you are used to "locations", it can be quite complicated.
Use the following references:
http://www.hiveworkshop.com/forums/jass-ai-scripts-tutorials-280/basic-jass-tips-22947/
http://www.hiveworkshop.com/forums/...als-280/beginning-jass-tutorial-series-30765/
http://www.hiveworkshop.com/forums/jass-ai-scripts-tutorials-280/jass-moving-gui-jass-start-40617/ <- This one is really important for you.
 
Level 17
Joined
Feb 11, 2011
Messages
1,860
Okay, thanks for your help. Just a stupid question, I have a script like this:

JASS:
function Leave_Area takes nothing returns nothing
    local location Temp_Point = GetUnitLoc(GetTriggerUnit())
    if GetTriggerPlayer() == Player(0) then
        call DisplayTextToPlayer(GetTriggerPlayer(), 0, 0, "Don't leave!")
        call AddSpecialEffectLoc("AOws", Temp_Point)
        call DestroyEffect(GetLastCreatedEffectBJ())
    endif
endfunction

How would I call this function using triggers? I have something like this, but it gives errors:

  • Leave Area
    • Events
      • Unit - A unit enters Effect <gen>
    • Conditions
    • Actions
      • Custom script: call Leave_Area()
I guess my question is: how to use the "()" in the custom script. Thanks,

- Mr_Bean
 
You are doing it fine. I think you get an error because you call the function before you declare it (ie. : your GUI trigger is placed before the jass function).
If you want to be sure that the worldedit compiles it in the right order, you can both :
- Put your function in the "custom script part", which is available by clicking on the map icon at the top of your triggers.
- Or use the JNGP and put your function into a library :
JASS:
library LeaveArea_Lib

    function Leave_Area takes nothing returns nothing
        ........
    endfunction

endlibrary

Libraries are always placed before the "custom script part" that is itself placed before regular triggers.
 
Level 17
Joined
Feb 11, 2011
Messages
1,860
I don't think that's the problem. I can't save the map after inserting the custom script. Shouldn't I put parameters into the "()"? If so, what should they be in this case? Here is the error message:

JASSError.jpg


Thanks,

- Mr_Bean
 
That IS the problem. Your trigger is somehow at the top of the map's script...
To be clear, this will cause an error :
JASS:
function A takes nothing returns nothing
    call B()
endfunction

function B takes nothing returns nothing
endfunction

You must put them in the right order :
JASS:
function B takes nothing returns nothing
endfunction

function A takes nothing returns nothing
    call B()
endfunction

I don't know what you make to put the function "Trig_Leave_Area_Actions" at top, but it definitely must be put after the function "Leave_Area".

EDIT : too slow ^^.
 
Level 13
Joined
Sep 13, 2010
Messages
550
1. Make sure that the trigger you call the function is downer than that what contains the function

This is not good:
Gui trigger
Jass trigger

Good:
Jass trigger
Gui trigger

2. If still gives error close editor and reopen. It should work.

3. If still gives error then you may not have a jass compatible world editor-> download JNGP

4. If you have JNGP and it isconfigured well then upload your map and we will check
 
Level 17
Joined
Feb 11, 2011
Messages
1,860
Okay, thanks guys, I seem to have sorted it out. One more quick question: What is wrong with this:

JASS:
function Leave_Area takes nothing returns nothing
    local location Temp_Point = GetUnitLoc(GetTriggerUnit())
    if (GetLocalPlayer() == Player (0)) then
        call DisplayTextToPlayer(Player(0), 0, 0, "Hi")
        call AddSpecialEffectTargetUnitBJ("origin", GetTriggerUnit(), "WAR_STOMP")
        call DestroyEffect(GetLastCreatedEffectBJ())
    endif
    call RemoveLocation(Temp_Point)
endfunction

No special effect is played. Even if I remove the DestroyEffect line nothing happens.

- Mr_Bean
 
Level 7
Joined
Nov 19, 2007
Messages
253
Few more tips: dont ever use red functions search for theyr native functions and use those and instead of location use x and y then you dont have to remove location
 
Level 16
Joined
Aug 7, 2009
Messages
1,403
Okay, thanks guys, I seem to have sorted it out. One more quick question: What is wrong with this:

JASS:
function Leave_Area takes nothing returns nothing
    local location Temp_Point = GetUnitLoc(GetTriggerUnit())
    if (GetLocalPlayer() == Player (0)) then
        call DisplayTextToPlayer(Player(0), 0, 0, "Hi")
        call AddSpecialEffectTargetUnitBJ("origin", GetTriggerUnit(), "WAR_STOMP")
        call DestroyEffect(GetLastCreatedEffectBJ())
    endif
    call RemoveLocation(Temp_Point)
endfunction

No special effect is played. Even if I remove the DestroyEffect line nothing happens.

- Mr_Bean

This's how it should look like:
JASS:
function Leave_Area takes nothing returns nothing
    //Use these instead of locals; reals're faster, and you don't have to destroy or null them
    local real x=GetUnitX(GetTriggerUnit())
    local real y=GetUnitY(GetTriggerUnit())
    //However they're not used anywhere in the function; I guess the location
    //was there because you wanted to create the effect at a certain point.
    
    if (GetLocalPlayer()==Player (0)) then
        call DisplayTextToPlayer(Player(0), 0, 0, "Hi")
        //Don't use those red functions, as someone's already mentioned it
        //They're there for GUI users, but they simply call these purple lines
        //so you can call them directly; it's faster.
        call DestroyEffect(AddSpecialEffectTarget("Abilities\\Spells\\Orc\\WarStomp\\WarStompCaster.mdl", GetTriggerUnit(), "origin"))
        //Yes, you can do this; you can destroy it immediately
        //Also, don't forget about the double backslash
    endif
endfunction
 
JASS:
function Leave_Area takes nothing returns nothing
    //Use these instead of locals; reals're faster, and you don't have to destroy or null them
    local real x=GetUnitX(GetTriggerUnit())
    local real y=GetUnitY(GetTriggerUnit())
    //However they're not used anywhere in the function; I guess the location
    //was there because you wanted to create the effect at a certain point.
    
    if (GetLocalPlayer()==Player (0)) then
        call DisplayTextToPlayer(Player(0), 0, 0, "Hi")
        //Don't use those red functions, as someone's already mentioned it
        //They're there for GUI users, but they simply call these purple lines
        //so you can call them directly; it's faster.
        call DestroyEffect(AddSpecialEffectTarget("Abilities\\Spells\\Orc\\WarStomp\\WarStompCaster.mdl", GetTriggerUnit(), "origin"))
        //Yes, you can do this; you can destroy it immediately
        //Also, don't forget about the double backslash
    endif
endfunction

->

JASS:
function Leave_Area takes nothing returns nothing
    if (GetLocalPlayer()==Player (0)) then
        call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "Hi") // GetLocalPlayer() should be faster than Player(0) since it takes no parameters (i think)
        //Don't use those red functions, as someone's already mentioned it
        //They're there for GUI users, but they simply call these purple lines
        //so you can call them directly; it's faster.
        call DestroyEffect(AddSpecialEffectTarget("Abilities\\Spells\\Orc\\WarStomp\\WarStompCaster.mdl", GetTriggerUnit(), "origin"))
        //Yes, you can do this; you can destroy it immediately
        //Also, don't forget about the double backslash
    endif
endfunction
 
Level 14
Joined
Nov 18, 2007
Messages
1,084
JASS:
function Leave_Area takes nothing returns nothing
    if (GetLocalPlayer()==Player (0)) then
        call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "Hi") // GetLocalPlayer() should be faster than Player(0) since it takes no parameters (i think)
        //Don't use those red functions, as someone's already mentioned it
        //They're there for GUI users, but they simply call these purple lines
        //so you can call them directly; it's faster.
        call DestroyEffect(AddSpecialEffectTarget("Abilities\\Spells\\Orc\\WarStomp\\WarStompCaster.mdl", GetTriggerUnit(), "origin"))
        //Yes, you can do this; you can destroy it immediately
        //Also, don't forget about the double backslash
    endif
endfunction
->
JASS:
function Leave_Area takes nothing returns nothing
    local string path = "Abilities\\Spells\\Orc\\WarStomp\\WarStompCaster.mdl"
    if (GetLocalPlayer()==Player (0)) then
        call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "Hi")
    else
        set path = ""
    endif
    call DestroyEffect(AddSpecialEffectTarget(path, GetTriggerUnit(), "origin"))
endfunction
Refer to this.
 
Level 16
Joined
Aug 7, 2009
Messages
1,403
JASS:
function Leave_Area takes nothing returns nothing
    if (GetLocalPlayer()==Player (0)) then
        call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "Hi") // GetLocalPlayer() should be faster than Player(0) since it takes no parameters (i think)
        //Don't use those red functions, as someone's already mentioned it
        //They're there for GUI users, but they simply call these purple lines
        //so you can call them directly; it's faster.
        call DestroyEffect(AddSpecialEffectTarget("Abilities\\Spells\\Orc\\WarStomp\\WarStompCaster.mdl", GetTriggerUnit(), "origin"))
        //Yes, you can do this; you can destroy it immediately
        //Also, don't forget about the double backslash
    endif
endfunction
->
JASS:
function Leave_Area takes nothing returns nothing
    local string path = "Abilities\\Spells\\Orc\\WarStomp\\WarStompCaster.mdl"
    if (GetLocalPlayer()==Player (0)) then
        call DisplayTextToPlayer(GetLocalPlayer(), 0, 0, "Hi")
    else
        set path = ""
    endif
    call DestroyEffect(AddSpecialEffectTarget(path, GetTriggerUnit(), "origin"))
endfunction
Refer to this.

Huhh, sorry, did not pay attention, there's a GetLocalPlayer() call. I know that it causes desync, I simply... ignored that part of the script :)

@Magtheridon96 Read the comments. I told him that he doesn't need those locals, but I wanted to show him how to use it properly if he'd need to use a location.
 
Level 17
Joined
Feb 11, 2011
Messages
1,860
Hmm, i'm not sure create/destroy an effect in a local block would desync, at least texttags don't and i would bet effects don't also, but i can't test it.

I want to create the effect so it is visible for all players.

===================================

Another quick question:
How do I create a special effect attached to the origin of Temp_Unit (variable)? This is all that I want a function to do. I have to declare variables at the beginning of each function, but how would I do this without resetting Temp_Unit?

Thanks!

- Mr_Bean
 
If you use the unit as a parameter, you do not need the Temp_Unit anymore.
It would go like this:
JASS:
function Leave_Area takes unit u returns nothing
    call KillUnit(u)
endfunction
(This was an example of how to use the parameter in the function)

How to call it (example #2):
  • Trigger
  • Events
    • Unit - A unit gains a level
  • Conditions
  • Actions
    • Custom script: call Leave_Area (GetTriggerUnit())
 
Level 17
Joined
Feb 11, 2011
Messages
1,860
Awesome! Thanks so much for your help. Looks like I've got a lot to learn!

EDIT: If I had this script, how would I use custom scripts to call it?

JASS:
function Movement_Speed takes unit Wisp returns nothing
    local real Temp_Real = GetUnitMoveSpeed(Wisp)
    call DisplayTextToPlayer(GetTriggerPlayer(), 0, 0, "Wisp movement speed: |cffffcc00" + (I2S(R2I(Temp_Real))) +"|r")
endfunction

Thanks,

- Mr_Bean
 
Level 17
Joined
Feb 11, 2011
Messages
1,860
Okay, thanks. I am trying now to create an effect on the unit Temp_Unit (variable) every 3 seconds. How would I write the custom script for this:

JASS:
function Show_Unit takes unit Temp_Unit returns nothing
    call DestroyEffect(AddSpecialEffectTarget("Abilities\\Spells\\Human\\Thunderclap\\ThunderClapCaster.mdl", Temp_Unit, "origin"))
endfunction
Thanks!

- Mr_Bean
 
Level 17
Joined
Feb 11, 2011
Messages
1,860
I am obviously doing something wrong. Here are my scripts:

JASS:
function Setup takes nothing returns nothing
    local location Temp_Point = GetPlayerStartLocationLoc(Player(0))
    local unit Wisp = CreateUnitAtLoc(Player(0), 'ewsp', Temp_Point, bj_UNIT_FACING)
    call SelectUnitForPlayerSingle(Wisp, Player(0))
    call PanCameraToTimedLocForPlayer(Player(0), Temp_Point, 0)
    call RemoveLocation(Temp_Point)
endfunction
JASS:
function Effect takes nothing returns nothing
    call DestroyEffect(AddSpecialEffectTarget("Abilities\\Spells\\Orc\\WarStomp\\WarStompCaster.mdl", Wisp, "origin"))
endfunction
Here are my triggers for calling them:

  • Map Setup
    • Events
      • Map initialization
    • Conditions
    • Actions
      • Visibility - Disable fog of war
      • Visibility - Disable black mask
      • Custom script: call Setup()
  • Effect
    • Events
      • Player - Player 1 (Red) types a chat message containing -effect as An exact match
    • Conditions
    • Actions
      • Custom script: call Effect(Wisp)
What am I doing wrong? It calls Map Setup fine, but I am having trouble with Effect. How do I set global variables (for example, a player chooses a hero and I set the variable: Hero[0] = their hero for the whole game)? Also, I know the parts in red writing are bad - what are their alternatives that I should use? Thanks!

- Mr_Bean
 
Your 'Effect' function doesn't take parameters, but you force it to, when calling it. Change the function to:
JASS:
function Effect takes unit Wisp returns nothing

Also, what's Wisp? You should define the unit you want; if you used (Triggering unit), it should be:
  • Tr
  • Events
    • Unit - A unit dies
  • Conditions
  • Actions
    • Custom script: call Effect (GetTriggerUnit())
However, if you use that event, you don't need parameters, as you have probably have the unit stored in some global variable, called Temp_Unit for example.
  • Effect
  • Events
    • Player - Player 1 (Red) types a chat message containing -effect as An exact match
  • Conditions
  • Actions
    • Custom script: call Effect()
and the Jass:
JASS:
function Effect takes nothing returns nothing
    call DestroyEffect(AddSpecialEffectTarget("Abilities\\Spells\\Orc\\WarStomp\\WarStompCaster.mdl", udg_Temp_Unit, "origin"))
endfunction
 
Using your previous function (a bit improved one) it looks somewhat like:
JASS:
function Setup takes nothing returns nothing
    local player p = Player(0)
    local real x = GetStartLocationX(GetPlayerStartLocation(p))
    local real y = GetStartLocationY(GetPlayerStartLocation(p))
    set udg_Hero[1] = CreateUnit(p, 'ewsp', x, y, bj_UNIT_FACING)
    //Select the unit plus Pan Camera
    if (GetLocalPlayer() == p) then
        call ClearSelection()
        call SelectUnit(udg_Hero[1], true)
        call PanCameraToTimed(x, y, 0)
    endif
    set p = null
endfunction
 
Level 17
Joined
Feb 11, 2011
Messages
1,860
Using your previous function (a bit improved one) it looks somewhat like:
JASS:
function Setup takes nothing returns nothing
    local player p = Player(0)
    local real x = GetStartLocationX(GetPlayerStartLocation(p))
    local real y = GetStartLocationY(GetPlayerStartLocation(p))
    set udg_Hero[1] = CreateUnit(p, 'ewsp', x, y, bj_UNIT_FACING)
    //Select the unit plus Pan Camera
    if (GetLocalPlayer() == p) then
        call ClearSelection()
        call SelectUnit(udg_Hero[1], true)
        call PanCameraToTimed(x, y, 0)
    endif
    set p = null
endfunction

THANK YOU SO MUCH!!! If I put the quoted script in JASSCraft, it says that there are undeclared variables on lines 6 and 15. Must I just ignore this?

Thanks again, :goblin_yeah:

- Mr_Bean
 
Level 17
Joined
Feb 11, 2011
Messages
1,860
the undeclared *thing* is the udg_Hero[1] so you should make the variable for it...
be warned also that you cant call just call it from custom script unless you put that function in a library OR the map header...

How would I make variables for them? With the trigger editor? All my JASS is in the map header.

- Mr_Bean

EDIT: I put the variables in the trigger editor and it works! Thanks again!
 
Status
Not open for further replies.
Top