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

[vJASS] Help with a simple vJass script. Thanks.

Status
Not open for further replies.
Level 11
Joined
Oct 11, 2012
Messages
711
Hi all, I am learning vJass. Is the following struct correct? It does work in game, but how about leak and other things? It drops certain items when a unit is killed.
JASS:
struct ArenaItem extends array
        private static integer i = 0
        private static integer array itemmm
    
    private static method itemmsettings takes nothing returns nothing
        set itemmm[0] = 'ratc'
        set itemmm[1] = 'rat9'
        set itemmm[2] = 'belv' 
    endmethod

    private static method run takes nothing returns boolean
        local unit dying = GetDyingUnit()
        local real x = GetUnitX(dying)
        local real y = GetUnitY(dying)
        if 
        /* Code */
        endif

        set dying = null

        return false
    endmethod

    private static method onInit takes nothing returns nothing
        local trigger t = CreateTrigger()
        call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_DEATH)
        call TriggerAddCondition(t, Condition(function thistype.run))
        set t = null
        call thistype.itemmsettings()
    endmethod
endstruct
 
Level 19
Joined
Mar 18, 2012
Messages
1,716
struct ArenaItem extends array could be private struct ArenaItem extends array since you use private methods only, that is just safety.

private static integer i = 0 what is that for? Any deleted code?
local unit dying = GetDyingUnit() --> Keep locals as simple as possible u is a pretty common declaration for a local unit local unit u = GetTriggerUnit() also GetTriggerUnit() returns the same as GetDyingUnit() does.
set t = null In case you never destroy that specific trigger, nulling is not needed. Still it is good practise.

I've learned vJass by writing my own resources based on epicenter and subzero by magtheridon96 :).
Have you checked this out already Structs for Dummies

Keep in mind: set myUnit[this] = and set this.myUnit = and set .myUnit = is all the same. That irritated me the most in the beginning.
 
Last edited:
Level 11
Joined
Oct 11, 2012
Messages
711
struct ArenaItem extends array could be private struct ArenaItem extends array since you use private methods only, that is just safety.

private static integer i = 0 what is that for? Any deleted code?
local unit dying = GetDyingUnit() --> Keep locals as simple as possible u is a pretty common declaration for a local unit local unit u = GetTriggerUnit() also GetTriggerUnit() returns the same as GetDyingUnit() does.
set t = null In case you never destroy that specific trigger, nulling is not needed. Still it is good practise.

Yea, the "integer i" is used in the code that I omitted in the example. I read something about creating and destroying a struct, but I suppose that isn't related to my example, right? :)
 
Level 11
Joined
Oct 11, 2012
Messages
711
Right because all of your methods and members are static.

Thanks. Under what circumstances do I need to create() and destroy()? When either methods or members are not static?

Btw feel free to post the full code, if it is not FBI top secret. vJass has a long learning curve and there are lots of people here, who are eager to help and some of them truly can.

It's not secret at all, the omitted code is real simple, just like normal jass code, I don't think it matters. But thanks for pointing that out. :)
 
Geshishouhu said:
Thanks. Under what circumstances do I need to create() and destroy()? When either methods or members are not static?

You don't need to manually define them if you don't want to. If you want dynamically allocated data then don't use static and if you basically want regular functions / globals, then do.

JASS:
struct example
	private string msg
	
	method show takes nothing returns nothing
		call BJDebugMsg(this.msg)
	endmethod
	
	static method onInit takes nothing returns nothing
		local thistype first = thistype.create()
		local thistype secnd = thistype.create()
		set first.msg = "Hello"
		set secnd.msg = "World"
		call BJDebugMsg(I2S(first)) // displays 0
		call BJDebugMsg(I2S(secnd)) // displays 1
		call first.show() // displays "Hello"
		call secnd.show() // displays "World"
	endmethod
	
endstruct

Here's the same thing but with .create() defined.

JASS:
struct example
	private string msg
	
	static method create takes string s returns thistype // must return the struct-type
		local thistype this = thistype.allocate() // always need to allocate an id
		set this.msg = s
		return this
	endmethod
	
	method show takes nothing returns nothing
		call BJDebugMsg(this.msg)
	endmethod
	
	static method onInit takes nothing returns nothing
		local thistype first = thistype.create("Hello")
		local thistype secnd = thistype.create("World")
		call BJDebugMsg(I2S(first)) // displays 0
		call BJDebugMsg(I2S(secnd)) // displays 1
		call first.show() // displays "Hello"
		call secnd.show() // displays "World"
	endmethod
	
endstruct

.destroy() recycles the allocated index so it can be re-used by another instance.

Refer to the JassHelper Manual for similar questions.
 
Level 11
Joined
Oct 11, 2012
Messages
711
You don't need to manually define them if you don't want to. If you want dynamically allocated data then don't use static and if you basically want regular functions / globals, then do.

JASS:
struct example
	private string msg
	
	method show takes nothing returns nothing
		call BJDebugMsg(this.msg)
	endmethod
	
	static method onInit takes nothing returns nothing
		local thistype first = thistype.create()
		local thistype secnd = thistype.create()
		set first.msg = "Hello"
		set secnd.msg = "World"
		call BJDebugMsg(I2S(first)) // displays 0
		call BJDebugMsg(I2S(secnd)) // displays 1
		call first.show() // displays "Hello"
		call secnd.show() // displays "World"
	endmethod
	
endstruct

Here's the same thing but with .create() defined.

JASS:
struct example
	private string msg
	
	static method create takes string s returns thistype // must return the struct-type
		local thistype this = thistype.allocate() // always need to allocate an id
		set this.msg = s
		return this
	endmethod
	
	method show takes nothing returns nothing
		call BJDebugMsg(this.msg)
	endmethod
	
	static method onInit takes nothing returns nothing
		local thistype first = thistype.create("Hello")
		local thistype secnd = thistype.create("World")
		call BJDebugMsg(I2S(first)) // displays 0
		call BJDebugMsg(I2S(secnd)) // displays 1
		call first.show() // displays "Hello"
		call secnd.show() // displays "World"
	endmethod
	
endstruct

.destroy() recycles the allocated index so it can be re-used by another instance.

Refer to the JassHelper Manual for similar questions.

Thanks for the detailed example! :)
I am reading the vJass manual actually, kinda hard to understand. Your example is better. ): +Rep if I can.
 
Level 18
Joined
Sep 14, 2012
Messages
3,413
Try to put the value of locals inside the if block.
Because every time a unit dies you will run those actions so keep it into the if block like this :
JASS:
    private static method run takes nothing returns boolean
        local unit u
        local real x
        local real y
        if 
            set u = GetTriggerUnit()
            set x = GetUnitX(u)
            set y = GetUnitY(u)
             /* Code */
            set dying = null
        endif

        return false
    endmethod
 
Level 22
Joined
Sep 24, 2005
Messages
4,821
@Malhorne, that's the reason I dislike boolexpr-only trigger functions, the overhead in allocating those locals grow depending on the amount of variables going to be used.
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
You can still do this if you care about it :

JASS:
scope Trigger initializer Init

    private function Actions takes nothing returns nothing
        // local variables
        // other trigger stuff
    endfunction

    private function Conditions takes nothing returns nothing
        if ... then
            call Actions()
        endif
    endfunction

    private function Init takes nothing returns nothing
        local trigger trig = CreateTrigger()
        // register some event
        call TriggerAddCondition(trig,function Conditions)
    endfunction

endscope
 
You can still do this if you care about it :

Forgot to return bool
JASS:
scope Trigger initializer Init

    private function Actions takes nothing returns nothing
        // local variables
        // other trigger stuff
    endfunction

    private function Conditions takes nothing returns boolean
        if ... then
            call Actions()
        endif
        return false
    endfunction

    private function Init takes nothing returns nothing
        local trigger trig = CreateTrigger()
        // register some event
        call TriggerAddCondition(trig,function Conditions)
    endfunction

endscope
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
Hmm, strange, have you tested it ? I was pretty sure.
Mostly because about that old supposed desync issue with mac users, which is supposed to be fixed long time ago btw (using a such boolexpr).

EDIT : And i don't write Filter or Condition, because still IIRC it should be implicit there, thx to jasshelper.
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
Then try with the official editor and be sure you use the last version of jasshelper. (check the "about" in the menu jasshelper of the JNGP when you have started it)
I'm really surprised but maybe i'm just getting much older that i expected (my memory starts to lie)
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
Oh well maybe it's since the newest patch (no typecasting at all, even widget -> unit). So it souldn't be possible to "typecast" nothing -> false
Or ofc i was totally wrong.

EDIT :

Well, i can't leave it.
At least using it with a code variable should work, since i'm 100 % sure i've already used it like that, according to my sample of UnitLL :
http://www.hiveworkshop.com/forums/jass-resources-412/snippet-unitll-209540/
 
Level 17
Joined
Apr 27, 2008
Messages
2,455
It was like that before, for sure (allowing conditions to return nothing). It might've been remedied with the latest version of PJASS. But it was kind of abusive to return nothing anyway. :p

Meh, we are not supposed to use trigger conditions like that anyway, so less verbosity is better in my opinion.
Now, ofc vJass, just like jass is pretty much verbose.
 
Status
Not open for further replies.
Top