• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[JASS] Improving/Fixing Code

Status
Not open for further replies.

Rui

Rui

Level 41
Joined
Jan 7, 2005
Messages
7,550
[JASS=Hi. I have been playing around with JASSNewGen and I would like to get some feedback on this code]
function Trig_BoardIn_Actions takes nothing returns nothing
local unit tempunt = GetEnteringUnit()
local rect temprect = gg_rct_TempArea
local location temploc = GetRandomLocInRect(temprect)
call SetUnitPositionLoc(tempunt, temploc)
call RemoveLocation(temploc)
call SetUnitOwner(tempunt, PLAYER_NEUTRAL_PASSIVE, false)
if tempunt == gg_unit_Othr_0213 then
call ThrallIsBoarding()
call DisableTrigger(GetTriggeringTrigger())
endif
set tempunt = null
set temprect = null
set temploc = null
endfunction

function ThrallIsBoarding takes nothing returns nothing
local group TempingRedGroup
call ForGroup(GetUnitsOfPlayerAll(Player(0)), RemoveUnit(GetEnumUnit()))
set TempingRedGroup = GetLastCreatedGroup()
call DestroyGroup(TempingRedGroup)
call BJDebugMSG("This function is working.")
set TempingRedGroup = null
endfunction
//===========================================================================
function InitTrig_BoardIn takes nothing returns nothing
set gg_trg_BoardIn = CreateTrigger()
call TriggerRegisterEnterRectSimple(gg_trg_BoardIn, gg_rct_TutorialSetSail)
call TriggerAddAction(gg_trg_BoardIn, function Trig_BoardIn_Actions)
endfunction[/code]

The syntax Checker yelled at me because of undeclared variables (such as gg_rct_TempArea, which DOES exist, gg_unit_Othr_0213, etc).
It also says "Can't convert Nothing to Code" on the ForGroup part. Perhaps I misunderstood the function.

What I'm basically trying to do is removing all the units owned by player once gg_unit_Othr_0213 enters the rect.
 
Level 11
Joined
Feb 22, 2006
Messages
752
JASS:
native ForGroup takes group whichGroup, code callback returns nothing

The callback is a function where you put all of your actions concerning the ForGroup loop.

About the undeclared variables, the syntax checker will do that to you sometimes. Just make another GUI trigger that does nothing but uses the variable gg_rct_TempArea in some way. Then the syntax checker should recognize the variable.

JASS:
function Trig_BoardIn_Actions takes nothing returns nothing
    local unit tempunt = GetTriggerUnit()
    local rect temprect = gg_rct_TempArea
    local location temploc = GetRandomLocInRect( temprect )

    call SetUnitPositionLoc( tempunt, temploc )
    call RemoveLocation( temploc )
    call SetUnitOwner( tempunt, PLAYER_NEUTRAL_PASSIVE, false )
    if ( tempunt == gg_unit_Othr_0213 ) then
        call ThrallIsBoarding()
        call DisableTrigger( GetTriggeringTrigger() )
    endif

    set tempunt = null
    set temprect = null
    set temploc = null
endfunction

function Remove_Units takes nothing returns nothing
    call RemoveUnit( GetEnumUnit() )
endfunction

function ThrallIsBoarding takes nothing returns nothing
    local group g = CreateGroup()

    call GroupEnumUnitsOfPlayer( g, Player(0), null )
    call ForGroup( g, function Remove_Units )
    call BJDebugMSG( "This function is working." )

    call DestroyGroup( g )
    set g = null
endfunction
//===========================================================================
function InitTrig_BoardIn takes nothing returns nothing
    set gg_trg_BoardIn = CreateTrigger()
    call TriggerRegisterEnterRectSimple(gg_trg_BoardIn, gg_rct_TutorialSetSail)
    call TriggerAddAction(gg_trg_BoardIn, function Trig_BoardIn_Actions)
endfunction
 
Level 6
Joined
Aug 15, 2007
Messages
209
Rui, your ForGroup should look something like...

JASS:
function RemoveFunc takes nothing returns nothing
call RemoveUnit(GetEnumUnit())
endfunction

function blah takes nothing returns nothing
call ForGroup(some group, function RemoveFunc)
endfunction
 
Level 11
Joined
Aug 25, 2006
Messages
971
Are you sure you have NewGen's syntax checker on and not WE's ? Make sure disable WE syntax check is checked and Enable Jass helper is checked?
As far as I know Player(15) is neutral passive.
 
Level 11
Joined
Aug 25, 2006
Messages
971
I think it compiles constants by replacing them with their value. So it really comes down to which is faster to type.
JASS:
    constant integer            PLAYER_NEUTRAL_PASSIVE          = 15
 

Rui

Rui

Level 41
Joined
Jan 7, 2005
Messages
7,550
Ok, thanks.

Yet another question =P can I set a local point using coordinates? If yes, do I need to destroy the point after using it?

EDIT: I have Enabled JASS Helper and disabled WE Syntax Checker, and the editor still yells about undeclared variables.
 
Level 11
Joined
Aug 25, 2006
Messages
971
Anything that involves Location(x,y) makes a point and must be removed. Instead look for a native that allows you to input the x and y without inputing a location.
 
Level 11
Joined
Aug 25, 2006
Messages
971
(Because the post between my two posts got deleted this is what was in my previous post)
JASS:
native Location                 takes real x, real y returns location
Thats the native


Just to exemplify what Dr. Super Good said:
JASS:
function Remove_Units takes nothing returns nothing
    call RemoveUnit( GetEnumUnit() )
endfunction

function ThrallIsBoarding takes nothing returns nothing
    local group g = CreateGroup()

    call GroupEnumUnitsOfPlayer( g, Player(0), null )
    call ForGroup( g, function Remove_Units )
    call BJDebugMSG( "This function is working." )

    call DestroyGroup( g )
    set g = null
endfunction
function Trig_BoardIn_Actions takes nothing returns nothing
    local unit tempunt = GetTriggerUnit()
    local rect temprect = gg_rct_TempArea
    local location temploc = GetRandomLocInRect( temprect )

    call SetUnitPositionLoc( tempunt, temploc )
    call RemoveLocation( temploc )
    call SetUnitOwner( tempunt, PLAYER_NEUTRAL_PASSIVE, false )
    if ( tempunt == gg_unit_Othr_0213 ) then
        call ThrallIsBoarding()
        call DisableTrigger( GetTriggeringTrigger() )
    endif

    set tempunt = null
    set temprect = null
    set temploc = null
endfunction
//===========================================================================
function InitTrig_BoardIn takes nothing returns nothing
    set gg_trg_BoardIn = CreateTrigger()
    call TriggerRegisterEnterRectSimple(gg_trg_BoardIn, gg_rct_TutorialSetSail)
    call TriggerAddAction(gg_trg_BoardIn, function Trig_BoardIn_Actions)
endfunction

In JASS you can only call a function that is above your current function.
 
Last edited:
  • Like
Reactions: Rui
Level 29
Joined
Jul 29, 2007
Messages
5,174
Oh didn't know this, thought it was just some sort of tradition.

And that native is just a normal loc, it doesn't really help.
What I meant is for example distance beetwin points.
Can we somehow (I know this won't work, its just an example for the question) make it check like this

local real distance = DistanceBetweenPoints(x1,y1,x2,y2)
 
Level 11
Joined
Aug 25, 2006
Messages
971
JASS:
function DistanceBetweenPoints takes location locA, location locB returns real
    local real dx = GetLocationX(locB) - GetLocationX(locA)
    local real dy = GetLocationY(locB) - GetLocationY(locA)
    return SquareRoot(dx * dx + dy * dy)
endfunction
Thats the BJ that needs a location. So you take it and you modify it to:
JASS:
function DistanceBetweenPointsXY takes real x1, real y1, real x2, real y2 returns real
    local real dx = x2 - x1
    local real dy = y2 - y1
    return SquareRoot(dx * dx + dy * dy)
endfunction
Then you add that to your code and *poof* there ya go.
 
Level 11
Joined
Aug 25, 2006
Messages
971
GetLocationZ returns the height of ground level at the X and Y coords of the location. Its not actually part of the internal location struct.
 

Rui

Rui

Level 41
Joined
Jan 7, 2005
Messages
7,550
I actually realized it myself DSG =P PJASS was yelling at me that it didn't know the function. That's when I remembered the function being called had to be above :p

I used the CreateUnit function, and I obviously used coordinates to define where it would be located. Are you totally sure it doesn't leak?

Btw Eleandor I had a few problems with 'Z' lol :p I was defining coordinates for the unit being created and it appeared at a different place. It happened that I was reading the last two numbers of the coordinates (which are Y and Z respectively) instead of the first pair xD
 
Status
Not open for further replies.
Top