Newbie vJass Questions

Darius34

New Member
Reaction score
30
So I just began learning vJass today, about a couple of hours ago. All I had before today was Handle Vars knowledge, for the record.

For practice, I was trying to convert this Game-Cache-heavy set of functions (just the projectile one in particular, the others are included because they're required)...

JASS:
function PolarOffsetX takes real originx, real offset, real angle returns real
    return originx + offset * Cos(angle)
endfunction

function PolarOffsetY takes real originy, real offset, real angle returns real
    return originy + offset * Sin(angle)
endfunction

function AngleBetweenUnits takes unit a, unit b returns real // Values are taken in radians.
    local real dx = GetUnitX(b) - GetUnitX(a)
    local real dy = GetUnitY(b) - GetUnitY(a)
    return Atan2(dy, dx)
endfunction

function DistanceBetweenUnits takes unit a, unit b returns real
    local real dx = GetUnitX(b) - GetUnitX(a)
    local real dy = GetUnitY(b) - GetUnitY(a)
    return SquareRoot(dx * dx + dy * dy)
endfunction

function SafeX takes real x returns real
    local real maparea=GetRectMinX(bj_mapInitialPlayableArea)+50
    if(x<maparea)then
        return maparea
    endif
    set maparea=GetRectMaxX(bj_mapInitialPlayableArea)-50
    if(x>maparea)then
        return maparea
    endif
    return x
endfunction

function SafeY takes real y returns real
    local real maparea=GetRectMinY(bj_mapInitialPlayableArea)+50
    if(y<maparea)then
        return maparea
    endif
    set maparea=GetRectMaxY(bj_mapInitialPlayableArea)-50
    if(y>maparea)then
        return maparea
    endif
    return y
endfunction

function GetHandleUnitString takes string actions, string name returns unit
    return GetStoredInteger(LocalVars(), actions, name)
    return null
endfunction

function FlushHandleString takes string actions returns nothing
    call FlushStoredMission(LocalVars(), actions)
endfunction

function CreateProjectilePeriodic takes nothing returns nothing
    local trigger t = GetTriggeringTrigger()
    local real speed = GetHandleReal(t, "speed")
    local unit source = GetHandleUnit(t, "source")
    local unit target = GetHandleUnit(t, "target")
    local unit projectile = GetHandleUnit(t, "projectile")
    local string actions = GetHandleString(t, "actions")
    local real targetx = GetUnitX(target)
    local real targety = GetUnitY(target)
    local real projectilex = GetUnitX(projectile)
    local real projectiley = GetUnitY(projectile)
    local real angle = AngleBetweenUnits(projectile, target)
    local real dist = DistanceBetweenUnits(projectile, target)
    local real offset = speed * 0.02
    local real x = PolarOffsetX(projectilex, offset, angle)
    local real y = PolarOffsetY(projectiley, offset, angle)

    call SetUnitX(projectile, SafeX(x))
    call SetUnitY(projectile, SafeY(y))
    call SetUnitFacing(projectile, angle * bj_RADTODEG)
    
    if dist <= offset then
        call SetUnitX(projectile, SafeX(targetx))
        call SetUnitY(projectile, SafeY(targety))
        call KillUnit(projectile)
        if actions != null then
            call StoreInteger(LocalVars(), actions, "source", H2I(source))
            call StoreInteger(LocalVars(), actions, "target", H2I(target))
            call ExecuteFunc(actions)
        endif
        call DisposeOfTrigger(t)
    elseif GetUnitState(target, UNIT_STATE_LIFE) < 0. then
        call KillUnit(projectile)
        call DisposeOfTrigger(t)
    endif
    
    set t = null
    set target = null 
    set source = null
    set projectile = null   
endfunction

function CreateProjectile takes unit source, unit target, integer projectileid, real speed, string actions returns unit
    local trigger t = CreateTrigger()
    local unit projectile = CreateUnit(GetOwningPlayer(source), projectileid, GetUnitX(source), GetUnitY(source), bj_RADTODEG * AngleBetweenUnits(source, target))

    call SetHandleHandle(t, "projectile", projectile)
    call SetHandleHandle(t, "source",  source)
    call SetHandleHandle(t, "target", target)
    call SetHandleString(t, "actions", actions)
    call SetHandleReal(t, "speed", speed)

    call TriggerRegisterTimerEvent(t, .02, true)
    call TriggerAddAction(t, function CreateProjectilePeriodic)
    
    set t = null
    return projectile
endfunction


...into vJass ones. I came up with the following:

JASS:
scope CreateProjectile

struct projectile
    unit dummy
    real speed
    string actions
    unit source
    unit target
    real targetx
    real targety
    
    method onDestroy takes nothing returns nothing
        set .dummy = null
        set .source = null
        set .target = null
    endmethod
endstruct

function AngleBetweenUnits takes unit a, unit b returns real
    local real dx = GetUnitX(b) - GetUnitX(a)
    local real dy = GetUnitY(b) - GetUnitY(a)
    return Atan2(dy, dx)
endfunction

function SafeX takes real x returns real
    local real maparea=GetRectMinX(bj_mapInitialPlayableArea)+50
    if(x<maparea)then
        return maparea
    endif
    set maparea=GetRectMaxX(bj_mapInitialPlayableArea)-50
    if(x>maparea)then
        return maparea
    endif
    return x
endfunction

function SafeY takes real y returns real
    local real maparea=GetRectMinY(bj_mapInitialPlayableArea)+50
    if(y<maparea)then
        return maparea
    endif
    set maparea=GetRectMaxY(bj_mapInitialPlayableArea)-50
    if(y>maparea)then
        return maparea
    endif
    return y
endfunction

function DisposeOfTrigger takes trigger t returns nothing
    call DisableTrigger(t)
    call FlushHandleLocals(t)
    call DestroyTrigger(t)
endfunction

function CreateProjectilePeriodic takes nothing returns nothing
    local trigger t = GetTriggeringTrigger()
    local projectile p = projectile(GetHandleInt(t, "p"))
    local real dummyx = GetUnitX(p.dummy)
    local real dummyy = GetUnitY(p.dummy)
    local real dx = p.targetx - dummyx
    local real dy = p.targety - dummyy
    local real angle = Atan2(dy, dx)
    local real dist = SquareRoot(dx * dx + dy * dy)
    local real offset = p.speed * 0.02
    local real x = dummyx + offset * Cos(angle)
    local real y = dummyy + offset * Sin(angle)

    call SetUnitX(p.dummy, SafeX(x))
    call SetUnitY(p.dummy, SafeY(y))
    call SetUnitFacing(p.dummy, angle * bj_RADTODEG)

    if dist <= offset then
        call SetUnitX(p.dummy, SafeX(p.targetx))
        call SetUnitY(p.dummy, SafeY(p.targety))
        call KillUnit(p.dummy)
        if p.actions != null then
            call ExecuteFunc(p.actions)
        endif
        call DisposeOfTrigger(t)
        call p.destroy()
    elseif GetUnitState(p.target, UNIT_STATE_LIFE) < 0. then
        call KillUnit(p.dummy)
        call DisposeOfTrigger(t)
        call p.destroy()
    endif
    
    set t = null
endfunction

function CreateProjectile takes unit source, unit target, integer projectileid, real speed, string actions returns unit
    local trigger t = CreateTrigger()
    local projectile p = projectile.create()
    local unit dummy = CreateUnit(GetOwningPlayer(source), projectileid, GetUnitX(source), GetUnitY(source), bj_RADTODEG * AngleBetweenUnits(source, target))
    
    set p.dummy = dummy
    set p.source = source
    set p.target = target
    set p.targetx = GetUnitX(target)
    set p.targety = GetUnitY(target)
    set p.speed = speed
    set p.actions = actions

    call SetHandleInt(t, "p", p)
    call TriggerRegisterTimerEvent(t, .02, true)
    call TriggerAddAction(t, function CreateProjectilePeriodic)
    
    set t = null
    set dummy = null
    return p.dummy
endfunction

endscope

Questions:

1. Is the code as efficient as could be, and have I missed anything out? Is anything leaking? I ran the trigger in the WE and got exactly the same results, so I was wondering if that was all to using structs.

2. I'm planning to put this function and a lot of others into a library. Thing is, if a library is placed at the top of the *.j file, do the Handle Vars functions (or those of any other attachment system) have to be a part of the library as well? Would I have to create a new library for them? I'd prefer to leave them in the header, if possible, to organise the code better.

3. I've always called the projectile function this way:

JASS:
function Trig_Test_Ability_Actions takes nothing returns nothing
    local unit caster = GetTriggerUnit()
    local unit target = GetSpellTargetUnit()
    
    call CreateProjectile(caster, target, 'e000', 900., "ProjectileActions")
    
    set caster = null
    set target = null
endfunction

I dealt with the actions this way (Game Cache):

JASS:
function ProjectileActions takes nothing returns nothing
    local unit target = GetHandleUnitString("ProjectileActions", "target")
    call RemoveUnit(target)
endfunction

I'm now doing (structs):

JASS:
function ProjectileActions takes nothing returns nothing
    local projectile p = projectile(GetHandleInt(GetTriggeringTrigger(), "p"))
    call RemoveUnit(p.target)
endfunction

Is this okay, and is this the best way? Now that I'm using structs, are there alternative possibilities?

4. Finally, I know the old code (projectile actions) wasn't MUI. I know structs instance, but I'm not exactly sure to what extent. Therefore, my question is: is the new one MUI?

Thanks in advance for the help. :)
 

T.s.e

Wish I was old and a little sentimental
Reaction score
133
Handle Variables are old, slow and outdated. Nobody uses them anymore.
 

Darius34

New Member
Reaction score
30
Yes, I know, which is why I'm learning to use structs. I'm using Handle Vars to attach structs only until I have the time to sit down and learn how to use another more advantageous system.

Anyway, did you read the rest of my post (beyond the first line -_- )?
 

Builder Bob

Live free or don't
Reaction score
249
Most of your questions are too vague for me to answer them. What is best varies from who looks at it. The trigger looks fine to me. What's important is that it gets it's job done. You seem to have a grasp of how to prevent leaks too.

What I can answer more specifically is your second question.
2. I'm planning to put this function and a lot of others into a library. Thing is, if a library is placed at the top of the *.j file, do the Handle Vars functions (or those of any other attachment system) have to be a part of the library as well? Would I have to create a new library for them? I'd prefer to leave them in the header, if possible, to organise the code better.

The easiest thing would be to put everything you have in your header into a separate library in a separate trigger. Let's call this library HelperLibrary. To make sure HelperLibrary is put on top of your CreateProjectile library, just specify which libraries are needed like this:
JASS:
library CreateProjectile needs HelperLibrary



Edit: Why wasn't your old trigger MUI by the way. When using local handle vars and doing it the way you did, I don't see why it shouldn't be...
 

Darius34

New Member
Reaction score
30
Thank you for the comprehensive reply.

Most of your questions are too vague for me to answer them. What is best varies from who looks at it. The trigger looks fine to me. What's important is that it gets it's job done. You seem to have a grasp of how to prevent leaks too.
Okay. I guess my question was really whether I was missing anything out. I guess not then.

I get the bit about libraries, too. Much appreciated.

Edit: Why wasn't your old trigger MUI by the way. When using local handle vars and doing it the way you did, I don't see why it shouldn't be...
Mm, you're right. I had originally assumed it wouldn't be, since I thought a separate instance of the trigger would overwrite the GC key. Haha, never mind.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top