• 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] Can someone tell me what's wrong with my function?

Status
Not open for further replies.
Level 18
Joined
Sep 2, 2005
Messages
1,029
Code:
function Trig_Spirit_Tree_Income_Actions takes nothing returns nothing
    local integer i
    set i = 0
    loop
    set i = i+1
    exitwhen i==12
    call SetPlayerStateBJ( Player(i), PLAYER_STATE_RESOURCE_GOLD, ( GetPlayerState(Player(i), PLAYER_STATE_RESOURCE_GOLD) + ( CountUnitsInGroup(GetUnitsOfPlayerAndTypeId(Player(i), 'e00D')) * 5 ) ) )
    endloop
endfunction

//===========================================================================
function InitTrig_Spirit_Tree_Income takes nothing returns nothing
    set gg_trg_Spirit_Tree_Income = CreateTrigger(  )
    call TriggerRegisterTimerEventPeriodic( gg_trg_Spirit_Tree_Income, 10.00 )
    call TriggerAddAction( gg_trg_Spirit_Tree_Income, function Trig_Spirit_Tree_Income_Actions )
endfunction

I'm new to Jass but I've done other coding before. I've checked this with a syntax checker, it seemed ok, but when I run the map, this script never does anything.

It should count how many of a certain building is owned by each player, then give them that number x 5 in gold.
 
Level 21
Joined
Aug 21, 2005
Messages
3,699
The script does work. it increases player(1) to player(12)'s gold. However, you should know that player(x) in jass is player(x + 1) in GUI, thus red's gold will never be increased.

Also, try to avoid BJfunctions. Unless they're a combination of 5 functions making your life easier, they usually do nothing more than a normal non-bj action would do. Thus, try using "SetPlayerState" instead of SetPlayerStateBJ.

Finally, I think you're leaking a group, when you're counting the # of buildings.
 
Level 18
Joined
Sep 2, 2005
Messages
1,029
The script does work. it increases player(1) to player(12)'s gold. However, you should know that player(x) in jass is player(x + 1) in GUI, thus red's gold will never be increased.

Also, try to avoid BJfunctions. Unless they're a combination of 5 functions making your life easier, they usually do nothing more than a normal non-bj action would do. Thus, try using "SetPlayerState" instead of SetPlayerStateBJ.

Finally, I think you're leaking a group, when you're counting the # of buildings.

Whoops. The spot i have the thing counting makes it skip player 0...
Totally missed that one. I'm leaking a group when counting the# of buildings? care to explain how?

leaks aren't somethign im too knowledgeable of.
 
Level 12
Joined
Aug 20, 2007
Messages
866
Sure

Well, whenever you do anything in Warcrafts editor, in JASS it runs a function
Functions take parameters

In order to execute the function, you must somehow obtain it's parameters, usually via Blizzards provided functions (or natives)

The functions you used 'CountUnitsInGroup()' uses a group as a parameter, and to produce that parameter you used 'GetUnitsOfPlayerAndTypeId()' generates a group which is for all you need to know about leaks, a large piece of information

Now, leaks happen when large pieces of information (such as groups) are finished being used, but are not removed, thus taking up valuable memory space

In order to clear the leaks, all you need to do, is create a variable
Then set that variable to the group you want to use
Use that group as much as you'd like
Then delete/destroy the group
And nullify the variable
Variables are like little pieces of information that point to the big pieces (or handles, which groups are) and says "Here it is!!!", but some variables, such as locals that we never use again, need to be nullified because they take up space, even when they are pointing to nothing

So, this would be your new leakless code


JASS:
function Trig_Spirit_Tree_Income_Actions takes nothing returns nothing
    local integer i = -1
    local integer income = 0
    local integer gold = 0 //These are just to make the code easier to read
    local group g
    loop
      set i = i+1
      exitwhen i==12
      set g = GetUnitsOfPlayerAndTypeId(Player(i), 'e00D')
      set income = CountUnitsInGroup(g) * 5                           
      set gold = GetPlayerState(Player(i),PLAYER_STATE_RESOURCE_GOLD)
      call SetPlayerState( Player(i), PLAYER_STATE_RESOURCE_GOLD, gold + income)
      call DestroyGroup(g)
      set g = null  //This is how you clear the group,
    endloop       // and it must be in the loop for this function
endfunction

//===========================================================================
function InitTrig_Spirit_Tree_Income takes nothing returns nothing
    set gg_trg_Spirit_Tree_Income = CreateTrigger(  )
    call TriggerRegisterTimerEventPeriodic( gg_trg_Spirit_Tree_Income, 10.00 )
    call TriggerAddAction( gg_trg_Spirit_Tree_Income, function Trig_Spirit_Tree_Income_Actions )
endfunction
I know you do not need to nullify integers, booleans, reals, and basically anything numerical, and a few others (like player variables) I'm not sure why but I know you don't need to
 
Level 18
Joined
Sep 2, 2005
Messages
1,029
Well, whenever you do anything in Warcrafts editor, in JASS it runs a function
Functions take parameters

In order to execute the function, you must somehow obtain it's parameters, usually via Blizzards provided functions (or natives)

The functions you used 'CountUnitsInGroup()' uses a group as a parameter, and to produce that parameter you used 'GetUnitsOfPlayerAndTypeId()' generates a group which is for all you need to know about leaks, a large piece of information

Now, leaks happen when large pieces of information (such as groups) are finished being used, but are not removed, thus taking up valuable memory space

In order to clear the leaks, all you need to do, is create a variable
Then set that variable to the group you want to use
Use that group as much as you'd like
Then delete/destroy the group
And nullify the variable
Variables are like little pieces of information that point to the big pieces (or handles, which groups are) and says "Here it is!!!", but some variables, such as locals that we never use again, need to be nullified because they take up space, even when they are pointing to nothing

So, this would be your new leakless code


JASS:
function Trig_Spirit_Tree_Income_Actions takes nothing returns nothing
    local integer i = -1
    local integer income = 0
    local integer gold = 0 //These are just to make the code easier to read
    local group g
    loop
      set i = i+1
      exitwhen i==12
      set g = GetUnitsOfPlayerAndTypeId(Player(i), 'e00D')
      set income = CountUnitsInGroup(g) * 5                           
      set gold = GetPlayerState(Player(i),PLAYER_STATE_RESOURCE_GOLD)
      call SetPlayerState( Player(i), PLAYER_STATE_RESOURCE_GOLD, gold + income)
      call DestroyGroup(g)
      set g = null  //This is how you clear the group,
    endloop       // and it must be in the loop for this function
endfunction

//===========================================================================
function InitTrig_Spirit_Tree_Income takes nothing returns nothing
    set gg_trg_Spirit_Tree_Income = CreateTrigger(  )
    call TriggerRegisterTimerEventPeriodic( gg_trg_Spirit_Tree_Income, 10.00 )
    call TriggerAddAction( gg_trg_Spirit_Tree_Income, function Trig_Spirit_Tree_Income_Actions )
endfunction
I know you do not need to nullify integers, booleans, reals, and basically anything numerical, and a few others (like player variables) I'm not sure why but I know you don't need to

Helpful. I didn't realize that the group would still take up memory. I'm used to other languages, where anything that isnt saved in a variable is deleted as soon as the function is over.

So I'm guessing GUI leaks like a bitch then?

And warc3 doesnt use a garbage collector?
 
Level 12
Joined
Aug 20, 2007
Messages
866
Oh

Oh, by the way somebody told me a bit more about the leaks, and you must null all of the variables except a select few, not because of the little leak, WC actually takes care of that

The problem, is that handles have a reference count on them, and whenever a variable is set to them the reference count increases, but when the handle is destroyed (unit dies for instance) if the reference count is still increased, it won't be totally removed from memory due to some bug in WC

In order to remove it completely, the variables for handles must be nulled, which decreases the reference count (kind of like un-setting the variable)

So as long as the reference count is increased, the handle will remain as a destroyed handle that takes up memory





Oh, yeah, Warcraft keeps everything around, I have no idea why, but I suppose thats another bug
Btw, what languages do you know? (looking for a C tutor)


Those other 2 questions, yup GUI leaks like crazzzzy, and yes Warcraft 3 does have a garbage collector, cept it's all bugged up
 
Level 18
Joined
Sep 2, 2005
Messages
1,029
Oh, by the way somebody told me a bit more about the leaks, and you must null all of the variables except a select few, not because of the little leak, WC actually takes care of that

The problem, is that handles have a reference count on them, and whenever a variable is set to them the reference count increases, but when the handle is destroyed (unit dies for instance) if the reference count is still increased, it won't be totally removed from memory due to some bug in WC

In order to remove it completely, the variables for handles must be nulled, which decreases the reference count (kind of like un-setting the variable)

So as long as the reference count is increased, the handle will remain as a destroyed handle that takes up memory





Oh, yeah, Warcraft keeps everything around, I have no idea why, but I suppose thats another bug
Btw, what languages do you know? (looking for a C tutor)


Those other 2 questions, yup GUI leaks like crazzzzy, and yes Warcraft 3 does have a garbage collector, cept it's all bugged up

I've only worked a bit with c.

Lots of vb.net, some java, annd alot of nwscript (Cept I haven't played nwn in about a year) Java is pretty much c with different code libraries.

so do I need to set the integers to null too or do local ints not leak after the function.

It took me like 5 tries and then looking online to figure out that its integer and not int in jass. I tried int and long. lol.
and the lack of {} threw me off too.

I'm a newbie to jass. but I have a year of college level programming. (the second year isnt coming as I'm in a university now) - Art, English, and Japanese. :p I'd rather be a comic book artist or a character artist than a code monkey. However if necessary I can always get back into coding for a career too. :)
 
Level 12
Joined
Aug 20, 2007
Messages
866
No

Reals, integers, and booleans don't need to be, because WC appropriately clears them, while the same goes for player variables, you don't need to change their reference count because player handels cannot be destroyed

The only ones you really need to remember are all handles that must be destroyed (theres alot, and I don't know all of them off the top of my head) and units must be nulled, because WC clears them when they die, but it won't clear them properly if there is a reference count on them
 
Level 18
Joined
Sep 2, 2005
Messages
1,029
Reals, integers, and booleans don't need to be, because WC appropriately clears them, while the same goes for player variables, you don't need to change their reference count because player handels cannot be destroyed

The only ones you really need to remember are all handles that must be destroyed (theres alot, and I don't know all of them off the top of my head) and units must be nulled, because WC clears them when they die, but it won't clear them properly if there is a reference count on them

I might be abl to find my old java notes. They could help you learn c
 
Level 12
Joined
Aug 20, 2007
Messages
866
That'd be sweet

Yeah, C -> Java, so that should be very helpful, although based on what I know about BASIC and Visual Basic, it might not, there's only one way to find out

+rep in advance :thumbs_up:
 
Level 12
Joined
Aug 20, 2007
Messages
866
Ohhhh

Thank you so much, yeah, it's always good to see real live examples

Yeah, I'm actually just a sophmore in high school, and about all my knowledge of programming comes from BASIC, VB, and the Warcraft stuff, once I learn C I should be ok with Java (I hope)

I think you'd be ok with JASS too, because it is very similar just the brackets and a few other things, like yeah the "int" is integer and the returns are not at the front, but alot of those things are pretty easy to pick up just reviewing another persons code

If I were you I'd definitely take a look at the spells section, thats how I picked up pretty much everything
 
Level 18
Joined
Sep 2, 2005
Messages
1,029
Yeah, I'm actually just a sophmore in high school,
What grade is a sophmore? I'm from Canada, and All those terms aren't used here. we just use the numbers. One of those small differences. Yeah I could do jass, I just haven't gotten used to it yet. But because I ask amateur questions everyone assumes I'm retarded or that I have never done any coding before.

I think you'd be ok with JASS too, because it is very similar just the brackets and a few other things, like yeah the "int" is integer and the returns are not at the front, but alot of those things are pretty easy to pick up just reviewing another persons code

If I were you I'd definitely take a look at the spells section, thats how I picked up pretty much everything

Yeah, well right now it's a "work on what's needed for the current map" deal. afterwards I may look at spells, but I find I learn best by doing when it comes to code.

http://www.hiveworkshop.com/forums/showthread.php?p=574947#post574947
care to give me a hand with this? so far everyone who's looked at it has pretty much ignored the actual question or just insulted me. It's quite irritating. - It actually may be in there among the ignoring... I think I found something. just a sec.
 
Level 12
Joined
Aug 20, 2007
Messages
866
Yeah

Usually people who start Jass have used the GUI and then moved on after they felt like they reached their maximum

Therefore, they pretty much know every single function

But, since your diving right into it, your gonna have alot of questions that will probably seem retarded to the average Jasser, just to give you the heads up

I highly suggest getting JassCraft, if you've already got it, then get the v1.21b patch for WC, JassCraft gets its functions from your current version of WarCraft, and will not show unknown functikns (cuz they don't technically exist :O)
 
Level 18
Joined
Sep 2, 2005
Messages
1,029
Usually people who start Jass have used the GUI and then moved on after they felt like they reached their maximum

Therefore, they pretty much know every single function

But, since your diving right into it, your gonna have alot of questions that will probably seem retarded to the average Jasser, just to give you the heads up

I highly suggest getting JassCraft, if you've already got it, then get the v1.21b patch for WC, JassCraft gets its functions from your current version of WarCraft, and will not show unknown functikns (cuz they don't technically exist :O)

Fair enough. I'm actually kindof learning gui and jass together, so like, I make what I can in gui, then convert to custom text, modify, and then tryto optimize it. Once I get used to the functions and shit though, I should be able to just write the jass from scratch.
 
Level 12
Joined
Aug 20, 2007
Messages
866
O

Sorry I fergot to answer your grade question ><

Yeah I'm actually grade 10, half way through high school, I started programming in the WarCraft language in like 8th grade though, so I'm ok as far as knowledge goes

Thats probably the best way to do it
You already got the programming skills, so all you need is the vocab, and the GUI is very very good for that
 
Level 18
Joined
Sep 2, 2005
Messages
1,029
Actually I'm starting to be able to just do the jass now. I'm not having to look things up very often, I just write it and its working the first time. This is day 2 of Jass. So it's working out ok.

Now if only I knew how to get unit IDs without goign through a big redardedly long process every time, I'd be set.
 
Level 12
Joined
Aug 20, 2007
Messages
866
Yeah

Just go to object editor --> View ---> check the rawcodes, and it will show you the units rawcode, it must have the ' ' to show its code for an integer (you probably already knew that)

For custom units, it is the part in the front
like let's say

h001:hfoo(Footman) is your custom unit's rawcode

this would be its unitid 'h001'
But for melee units, who don't have that extra bit, it's just the 'hfoo'

And yes, GUI is just about the best thing if you want a quick overview of what WarCraft can do with its functions

Justify, sprechen sie deutsch? Ich kann spreche nicht gut, aber ich bin gelernen.
 
Level 18
Joined
Sep 2, 2005
Messages
1,029
Hm what is your 'big redardedly long process'?


Make a function in gui which serves no purpose.
Give it actions or conditions that deal with the unit type I'm looking for
Convert to custom text
Copy the id
Delete the useless function

Does CTRL+D only work on unit type or does it work to see the id of specific placed units as well?

Ctrl D toggles that view menu. Oh this will save me so much work.

WQhat about getting the id of a placed unit? Like, not id for type, but id of the unit itself?
 
Level 12
Joined
Aug 20, 2007
Messages
866
Kinda tricky

For that one, I usually do that process you spoke of before, but I know there is some kind of format for the thing, I know it's based off of the unitid

Its something strange like, gg_ then something goes here _ and then the unit id, something, I don't remember it because I generally never use it
 
Level 18
Joined
Sep 2, 2005
Messages
1,029
Status
Not open for further replies.
Top