• 🏆 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!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

Question about my coding style

Status
Not open for further replies.
Level 2
Joined
Jan 6, 2008
Messages
25
Hey, I'm a long-time GUI coder, who fairly recently switched over to vJASS. I'm able to accomplish most things that I try to do, and I use natives and clean leaks and such.

However, what I'm curious about is why it seems all the code I see from other people on the forums and such is different than mine. A lot of people seem to use Structs as their initializers, and differences like that.
Also there is all the external libraries that people use, like TimerUtils and such, that I never really understood that well.

My question for you guys is what's wrong with my coding style, because it's different than what everyone else seems to do. I don't mean like "how exactly would you do this one part more efficiently?" but just what can you gleam as being incorrect about the whole code style / format?



Example of my coding style
JASS:
library Setup initializer Init
//===========================================================================

    globals
        unit array pl_base[8]
        unit array pl_hero[8]
        hashtable array pl_table[8]
    endglobals

 private function Start takes nothing returns nothing
        local integer i = 0
        local real x
        local real y
        local rect r = gg_rct_Center
        local rect r2
        
        loop
            exitwhen i == 1
            if GetPlayerSlotState(Player(i)) == PLAYER_SLOT_STATE_PLAYING then
                if Player(i) == Player(0) then
                    set r2 = gg_rct_Red_Base
                elseif Player(i) == Player(1) then
                    set r2 = gg_rct_Blue_Base
                elseif Player(i) == Player(2) then
                    set r2 = gg_rct_Teal_Base
                elseif Player(i) == Player(3) then
                    set r2 = gg_rct_Purple_Base
                endif
                set bj_lastCreatedFogModifier = CreateFogModifierRect(Player(i), FOG_OF_WAR_VISIBLE, r, true, false)
                call FogModifierStart(bj_lastCreatedFogModifier)
                set bj_lastCreatedUnit = CreateUnit(Player(i), 'hbse', GetStartLocationX(i), GetStartLocationY(i), 0)
                set pl_base[i] = bj_lastCreatedUnit
                set bj_lastCreatedUnit = CreateUnit(Player(i), 'Hmge', GetRectCenterX(r) + i * 200, GetRectCenterY(r) - 500, 90)
                set pl_hero[i] = bj_lastCreatedUnit
                call UnitAddAbility(bj_lastCreatedUnit, 'Abrd')
                call SetUnitInvulnerable(bj_lastCreatedUnit, false)
                call CreateUnit(Player(i), 'htwr', GetRectMinX(r2), GetRectMinY(r2), 0)
                call CreateUnit(Player(i), 'htwr', GetRectMaxX(r2), GetRectMinY(r2), 0)
                call CreateUnit(Player(i), 'htwr', GetRectMinX(r2), GetRectMaxY(r2), 0)
                call CreateUnit(Player(i), 'htwr', GetRectMaxX(r2), GetRectMaxY(r2), 0)
                call SetPlayerState(Player(i), PLAYER_STATE_GIVES_BOUNTY, 1)
                set pl_table[i] = InitHashtable()
            endif
            set i = i + 1
        endloop
        
        set r = null
        set r2 = null
    endfunction

    private function Init takes nothing returns nothing
        local trigger t1 = CreateTrigger()
        local trigger t2 = CreateTrigger()
        local integer i = 0
        
        call TriggerAddAction(t1, function Start)
        call TriggerAddAction(t2, function Choose)
        call TriggerRegisterTimerEvent(t1, 0.1, false)
        loop
            call TriggerRegisterPlayerUnitEvent(t2, Player(i), EVENT_PLAYER_UNIT_SPELL_EFFECT, null)
            set i = i + 1
            exitwhen i == 8
        endloop
    endfunction
    
//===========================================================================
endlibrary
 
There is nothing really wrong with your coding style. People usually use what's comfortable to use. A lot of JNGP is like a UI for JASS. Structs members are essentially global arrays, but some people don't like to deal with arrays since they tend to get confusing. Structs look a little bit more neat.

People usually use methods as opposed to functions because they are associated with the struct. Thus they can use private members, and non-static methods "this." or a mere "." to point to the current instance. Static methods are functions, but ones that can use private members of the struct. The other cool thing about them is that you can override the .create() to modify it according to your spell/code.

To be honest, a lot of people who code in vJASS have C or java or some other language's background. It just seems more easier than to learn an entirely new syntax. All those big extension libraries make methods (by this I mean technique, not function methods) for things simply more efficient. It's basically decomplicating the complicated, and yet improving your code.
 
Level 14
Joined
Nov 18, 2007
Messages
1,084
Like PurgeandFire111's post said, there is nothing really wrong with your coding style. However, you may want to make it more efficient:
Ex:
Instead of
JASS:
set bj_lastCreatedUnit = CreateUnit(Player(i), 'hbse', GetStartLocationX(i), GetStartLocationY(i), 0)
set pl_base[i] = bj_lastCreatedUnit
set bj_lastCreatedUnit = CreateUnit(Player(i), 'Hmge', GetRectCenterX(r) + i * 200, GetRectCenterY(r) - 500, 90)
set pl_hero[i] = bj_lastCreatedUnit
call UnitAddAbility(bj_lastCreatedUnit, 'Abrd')
call SetUnitInvulnerable(bj_lastCreatedUnit, false)
you could do
JASS:
set pl_base[i]= CreateUnit(Player(i), 'hbse', GetStartLocationX(i), GetStartLocationY(i), 0)
set pl_hero[i]= CreateUnit(Player(i), 'Hmge', GetRectCenterX(r) + i * 200, GetRectCenterY(r) - 500, 90)
call UnitAddAbility(pl_hero[i], 'Abrd')
call SetUnitInvulnerable(pl_hero[i], false)
You could also have reused the variable t1 to make the trigger for t2.

Edit: Additional
JASS:
call FogModifierStart(CreateFogModifierRect(Player(i), FOG_OF_WAR_VISIBLE, r, true, false))
if you only wanted to start the fog modifier.
 
Last edited:
Level 2
Joined
Jan 6, 2008
Messages
25
Thanks for the help guys.
I have a lot to learn and I appreciate what you guys have said. I know this thread probably seems pretty random, I just always wondered when I would see other people's code and it would be all structs, TimerUtils, blah blah blah, and I was like "what am I doing wrong in that I don't use this stuff?"

But yeah, thanks for the help. And I'll take into account the efficiency advice, I don't know why I didn't think about some of those things ;P
 
Status
Not open for further replies.
Top