I'm new to structs

Dirac

22710180
Reaction score
147
Hello guys i just made this trigger which purpose is to move a ship through water using the arrow keys. I've never used structs nor vJass before. I just want to know if theres a way to enhance my code.
- I tried to clear all BJs
- I need a better way to initialize my global arrays
- Is there a way to change global arrays from other triggers?
- What does a static method do?
- What is a keyword?

+rep will be rewarded for those who help me improve my coding skills
JASS:
scope Movement initializer OnInit


    globals
        private unit array ship

        private boolean array left
        private boolean array right
        private boolean array up

        public real array speed
        public real array angle
        public real array maxturnangle
        public real array acceleration
    endglobals

    private struct shipdata
        unit u
        integer i
        real x
        real y

        method store takes unit u, integer i returns nothing
            set this.u = u
            set this.i = i
        endmethod

        method acc takes real a, boolean b,real c returns nothing
            if this.x<c and b== true then
                set this.x = this.x+a
            elseif this.x > 0 then
                set this.x = this.x-a
            endif
        endmethod

        method turn takes real a,boolean b,boolean c, real d returns nothing
            if b != c then
                if b==true and this.y < d then
                    set this.y = this.y + a
                elseif c==true and this.y > d*(-1) then
                    set this.y = this.y - a
                endif
            else
                if this.y > 0 then
                    set this.y = this.y - a
                elseif this.y < 0 then
                    set this.y = this.y + a
                endif
            endif
        endmethod
    endstruct

    private function setglobals takes nothing returns nothing
        local shipdata ud=shipdata.create()
        set ship[0] = CreateUnit(Player(0),'H000',-3000.,-3000.,0.)
        call SetUnitUserData(ship[0],ud)
        call ud.store(ship[0],GetPlayerId(GetOwningPlayer(ship[0])))
    endfunction

    private function pressl takes nothing returns nothing
        if left[GetPlayerId(GetTriggerPlayer())] == true then
            set left[GetPlayerId(GetTriggerPlayer())] = false
        else
            set left[GetPlayerId(GetTriggerPlayer())] = true
        endif
    endfunction

    private function pressr takes nothing returns nothing
        if right[GetPlayerId(GetTriggerPlayer())] == true then
            set right[GetPlayerId(GetTriggerPlayer())] = false
        else
            set right[GetPlayerId(GetTriggerPlayer())] = true
        endif
    endfunction

    private function pressu takes nothing returns nothing
        if up[GetPlayerId(GetTriggerPlayer())] == true then
            set up[GetPlayerId(GetTriggerPlayer())] = false
        else
            set up[GetPlayerId(GetTriggerPlayer())] = true
        endif
    endfunction

    public function periodic takes nothing returns nothing
        local real x
        local real y
        local integer i = 0
        local shipdata ud=GetUnitUserData(ship<i>)
        loop
            exitwhen i == 1
            call ud.turn (0.05,left<i>,right<i>,maxturnangle<i>)
            call ud.acc (acceleration<i>, up<i>, speed<i>)
            set x = GetUnitX(ship<i>) + ud.x*Cos(Deg2Rad(GetUnitFacing(ship<i>)+ud.y))
            set y = GetUnitY(ship<i>) + ud.x*Sin(Deg2Rad(GetUnitFacing(ship<i>)+ud.y))
            call SetUnitFacing(ship<i>,GetUnitFacing(ship<i>)+ud.y)
            call SetUnitX(ship<i>,x)
            call SetUnitY(ship<i>,y)
            if GetLocalPlayer() != Player(i) then
                set x=0.
                set y=0.
            endif
            call PanCameraToTimed(x,y,0.)
            call SetCameraRotateMode(x,y,Deg2Rad(270+GetUnitFacing(ship<i>)),0.)
            call SetCameraField(CAMERA_FIELD_TARGET_DISTANCE,3900.,0.)
            set i = i+1
        endloop
    endfunction

    // =============================================================
    private function OnInit takes nothing returns nothing
        local trigger mov = CreateTrigger() //create a local for each trigger
        local trigger left = CreateTrigger()
        local trigger right = CreateTrigger()
        local trigger up = CreateTrigger()
        local trigger init = CreateTrigger()
        local trigger t = CreateTrigger()
        local integer i = 0
        loop
            exitwhen i == 5
            call TriggerRegisterPlayerEvent (left, Player (i), EVENT_PLAYER_ARROW_LEFT_DOWN) // add the arrow key events to the triggers
            call TriggerRegisterPlayerEvent (left, Player(i), EVENT_PLAYER_ARROW_LEFT_UP)
            call TriggerRegisterPlayerEvent (right, Player(i), EVENT_PLAYER_ARROW_RIGHT_DOWN)
            call TriggerRegisterPlayerEvent (right, Player(i), EVENT_PLAYER_ARROW_RIGHT_UP)
            call TriggerRegisterPlayerEvent (up, Player(i), EVENT_PLAYER_ARROW_UP_DOWN)
            call TriggerRegisterPlayerEvent (up, Player(i), EVENT_PLAYER_ARROW_UP_UP)
            set i = i+1
        endloop
        call TriggerRegisterTimerEvent(t,0.0325,true)
        call TriggerAddAction(t, function periodic )
        call TriggerRegisterTimerEvent(init,0,false)
        call TriggerAddAction( left, function pressl )
        call TriggerAddAction( right, function pressr )
        call TriggerAddAction( up, function pressu )
        call TriggerAddAction(init, function setglobals)
    endfunction

endscope</i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i>
 

Sevion

The DIY Ninja
Reaction score
413
Your coding skills would benefit greatly by code indention :3

Yes, there is a way to change global arrays from other triggers. Simply make sure the global is public (no modifier or [ljass]public[/ljass] modifier, which you already have) and just do something like [ljass]myPublicGlobal[1] = 14[/ljass] in whatever trigger you want. Though some things come into play like trigger placement, but as long as the trigger you're declaring the global is in is above the trigger/function that you're using it in then you're fine.

Static means the method/member has only one instance per struct. Non-static members/methods have one instance per struct instance.

A keyword simply allows for use of something above its declaration. For example:

JASS:
keyword myStruct

globals
    myStruct myGlobalStruct
endglobals

struct myStruct
    int data
endstruct
 

Dirac

22710180
Reaction score
147
thanks for the info, a couple of questions regarding your post
- What's the easiest way to apply indention to my code?
- What good does it make having a static method (isn't a normal method good enough?)

+rep
 

Sevion

The DIY Ninja
Reaction score
413
The easiest way is to use the built in indenter :p TESH auto indents for you.

Otherwise you have to just simply go through and indent with tab.

There are uses for static methods like Timer Callbacks, etc. You can't pass non-static methods through those.
 

Solmyr

Ultra Cool Member
Reaction score
30
Static methods are basically "functions in disguise". They do not use an instance ([ljass]this[/ljass]), but still have an access to all of the members of the struct.

There's another use for [ljass]keyword[/ljass] (besides what Sevion said) - making library-private struct members or methods.
JASS:
library Foo
    private keyword libraryPrivateString
    
    struct Bar
        string libraryPrivateString
        private string structPrivateString
        real publicReal
        /* This struct has access to &quot;libraryPrivateString&quot;, &quot;structPrivateString&quot; and &quot;publicReal&quot; */
    endstruct

    private function doThings takes nothing returns nothing
        /* This function has access to &quot;libraryPrivateString&quot; and &quot;publicReal&quot; */
    endfunction
endlibrary

scope Foobar
    private function doMoreThings takes nothing returns nothing
        /* This function has access only to &quot;publicReal&quot; */
    endfunction
endscope
 

Dirac

22710180
Reaction score
147
I've been coding with tesh so far and no intendion has been done so far, is there some kind of option i have to turn on?
 

Sevion

The DIY Ninja
Reaction score
413
That's strange. I thought TESH auto indents. Just remember to use Tab I suppose. Tab in every "block".

I.E. the contents of a function block are indented and the contents of an if block inside of that function are indented two tabs.
 

WolfieeifloW

WEHZ Helper
Reaction score
372
Example:
JASS:
scope...                 //Aligned all the way left

    globals              //Anything past scope indented once, new block, globals block
        integer...       //All things inside the block indented once again
    endglobals           //Block ending, lineup with the opening of the block

    function...          //New block, a function, indent once
        if...            //All code inside the function block indented once past the opening block; Also, an opening if block
            call...      //All code inside the if is indented once again
            if...        //Nested if opening
                call...  //Again, indent once after an if block
            endif        //Lineup with the opening block of your if
        endif            //Again, line it up with the opening of the if
    endfunction          //Always end the closing of blocks with the opening

endscope                 //Again, lineup openings and closings

Just a sort of basic code example.
 

WolfieeifloW

WEHZ Helper
Reaction score
372
Those look complex :p
Why not just write your code in the actual WE and use 'Tab'?
Or am I missing the point of the programs?
 

Dirac

22710180
Reaction score
147
Thanks wolfiee, i was just wondering if the editor did it automatically, guess it doesn't
Yes, there is a way to change global arrays from other triggers. Simply make sure the global is public (no modifier or public modifier, which you already have) and just do something like myPublicGlobal[1] = 14 in whatever trigger you want. Though some things come into play like trigger placement, but as long as the trigger you're declaring the global is in is above the trigger/function that you're using it in then you're fine.
I created a new trigger with this code and gives me syntax errors
JASS:
scope GlobalsSet initializer start
    private function start takes nothing returns nothing
        set speed[0] = 10.
        set angle [0] = 6.
        set acceleration[0] = 0.1
        set maxturnangle[0] = 6.
    endfunction
endscope

Also, update the code with some intending applied
 

WolfieeifloW

WEHZ Helper
Reaction score
372
Here's your code, with proper indentation:
JASS:
scope Movement initializer OnInit

    globals
        unit array ship
        
        boolean array left
        boolean array right
        boolean array up

        real array speed
        real array angle
        real array maxturnangle
        real array acceleration
    endglobals

    private struct data
        unit u
        integer i
        real x
        real y
        trigger t
        boolean a
        boolean b
        boolean c

        method acc takes real a, boolean b,real c returns nothing
            if this.x&lt;c and b== true then
                set this.x = this.x+a
            elseif this.x &gt; 0 then
                set this.x = this.x-a
            endif
        endmethod

        method turn takes real a,boolean b,boolean c, real d returns nothing
            if b != c then
                if b==true and this.y &lt; d then
                    set this.y = this.y + a
                elseif c==true and this.y &gt; d*(-1) then
                    set this.y = this.y - a
                endif
            else
                if this.y &gt; 0 then
                    set this.y = this.y - a
                elseif this.y &lt; 0 then
                    set this.y = this.y + a
                endif
            endif
        endmethod
    endstruct

    private function setglobals takes nothing returns nothing
        local data ud=data.create()
        set ship[0] = CreateUnit(Player(0),&#039;H000&#039;,-3000.,-3000.,0.)
        call SetUnitUserData(ship[0],ud)
        set speed[0] = 10.
        set angle [0] = 6.
        set acceleration[0] = 0.1
        set maxturnangle[0] = 6.
    endfunction

    private function pressl takes nothing returns nothing
        if left[GetPlayerId(GetTriggerPlayer())] == true then
            set left[GetPlayerId(GetTriggerPlayer())] = false
        else
            set left[GetPlayerId(GetTriggerPlayer())] = true
        endif
    endfunction

    private function pressr takes nothing returns nothing
        if right[GetPlayerId(GetTriggerPlayer())] == true then
            set right[GetPlayerId(GetTriggerPlayer())] = false
        else
            set right[GetPlayerId(GetTriggerPlayer())] = true
        endif
    endfunction

    private function pressu takes nothing returns nothing
        if up[GetPlayerId(GetTriggerPlayer())] == true then
            set up[GetPlayerId(GetTriggerPlayer())] = false
        else
            set up[GetPlayerId(GetTriggerPlayer())] = true
        endif
    endfunction

    private function periodic takes nothing returns nothing
        local real x
        local real y
        local integer i = 0
        local data ud=GetUnitUserData(ship<i>)
        loop
        exitwhen i == 1
            call ud.turn (0.05,left<i>,right<i>,maxturnangle<i>)
            call ud.acc (acceleration<i>, up<i>, speed<i>)
            set x = GetUnitX(ship<i>) + ud.x*Cos(Deg2Rad(GetUnitFacing(ship<i>)+ud.y))
            set y = GetUnitY(ship<i>) + ud.x*Sin(Deg2Rad(GetUnitFacing(ship<i>)+ud.y))
            call SetUnitFacing(ship<i>,GetUnitFacing(ship<i>)+ud.y)
            call SetUnitX(ship<i>,x)
            call SetUnitY(ship<i>,y)
            if GetLocalPlayer() != Player(i) then
                set x=0.
                set y=0.
            endif
            call PanCameraToTimed(x,y,0.)
            call SetCameraRotateMode(x,y,Deg2Rad(270+GetUnitFacing(ship<i>)),0.)
            call SetCameraField(CAMERA_FIELD_TARGET_DISTANCE,3900.,0.)
            set i = i+1
        endloop
    endfunction
// =============================================================
    private function OnInit takes nothing returns nothing
        local trigger mov = CreateTrigger() //create a local for each trigger
        local trigger left = CreateTrigger()
        local trigger right = CreateTrigger()
        local trigger up = CreateTrigger()
        local trigger init = CreateTrigger()
        local integer i = 0
        loop
        exitwhen i == 5
            call TriggerRegisterPlayerEvent (left, Player (i), EVENT_PLAYER_ARROW_LEFT_DOWN) // add the arrow key events to the triggers
            call TriggerRegisterPlayerEvent (left, Player(i), EVENT_PLAYER_ARROW_LEFT_UP)
            call TriggerRegisterPlayerEvent (right, Player(i), EVENT_PLAYER_ARROW_RIGHT_DOWN)
            call TriggerRegisterPlayerEvent (right, Player(i), EVENT_PLAYER_ARROW_RIGHT_UP)
            call TriggerRegisterPlayerEvent (up, Player(i), EVENT_PLAYER_ARROW_UP_DOWN)
            call TriggerRegisterPlayerEvent (up, Player(i), EVENT_PLAYER_ARROW_UP_UP)
            set i = i+1
        endloop
        call TriggerRegisterTimerEvent(init,0,false)
        call TriggerRegisterTimerEvent(mov,0.0325,true)
        call TriggerAddAction( mov, function periodic )
        call TriggerAddAction( left, function pressl )
        call TriggerAddAction( right, function pressr )
        call TriggerAddAction( up, function pressu )
        call TriggerAddAction(init, function setglobals)
    endfunction

endscope</i></i></i></i></i></i></i></i></i></i></i></i></i></i></i></i>

:)


/EDIT: Owned, you did it already :p
 

Dirac

22710180
Reaction score
147
i guess not lol they don't work. When you mean they can used in another trigger you mean:
A.- Another scope in the same code
B.- Another scope in another code
 

Laiev

Hey Listen!!
Reaction score
188
A and B is the same |: if you use other 'block of trigger' to trigger another scope or use the same 'block of trigger' to trigger another scope, they will be another scope no matter what
 

Dirac

22710180
Reaction score
147
i can't get the public globals (now with the public prefix) to work in another trigger, how do i initialize the scopes in the correct order?

Edit: nvm i just realized i have to type scopename_globalname in other scopes, however i wonder is there a way to change the order scopes initialize? or it's done by default depending on the code
 

Rllulium

New Member
Reaction score
10
Make a library:
JASS:
library Movement initializer OnInit

Libraries are automatically put before scopes.
You can also specify a requirement, that will put the trigger after the requirement:
JASS:
library Movement initializer OnInit requires ThatOtherLibrary
 

Magthridon96

Member
Reaction score
2
I agree with Rllulium.

Using scopes is bad programming practice.
You can get away with making implicit requirements.

Also, it's much better to use module Initialization:
JASS:

private module Init
    private static method onInit takes nothing returns nothing
        // init
    endmethod
endmodule
private struct Inits extends array
    implement Init
endstruct


Library initializers are horrible-
edit
-in some cases..
 

tooltiperror

Super Moderator
Reaction score
231
>Using scopes is bad programming practice.
I have to agree.

>You can get away with making implicit requirements.
Again, I agree. Using a function from a library your library does not require should not be allowed. Blame Vexorian.

>Library initializers are horrible
Not if you're making a system like AIDS and need to use the library initializer.
 

Dirac

22710180
Reaction score
147
Using a function from a library your library does not require should not be allowed
im sorry, what?
where do i find a tutorial to learn implicit requirements and library creation
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Varine Varine:
    I want to build a filtration system for my 3d printer, and that shit is so much more complicated than I thought it would be
  • Varine Varine:
    Apparently ABS emits styrene particulates which can be like .2 micrometers, which idk if the VOC detectors I have can even catch that
  • Varine Varine:
    Anyway I need to get some of those sensors and two air pressure sensors installed before an after the filters, which I need to figure out how to calculate the necessary pressure for and I have yet to find anything that tells me how to actually do that, just the cfm ratings
  • Varine Varine:
    And then I have to set up an arduino board to read those sensors, which I also don't know very much about but I have a whole bunch of crash course things for that
  • Varine Varine:
    These sensors are also a lot more than I thought they would be. Like 5 to 10 each, idk why but I assumed they would be like 2 dollars
  • Varine Varine:
    Another issue I'm learning is that a lot of the air quality sensors don't work at very high ambient temperatures. I'm planning on heating this enclosure to like 60C or so, and that's the upper limit of their functionality
  • Varine Varine:
    Although I don't know if I need to actually actively heat it or just let the plate and hotend bring the ambient temp to whatever it will, but even then I need to figure out an exfiltration for hot air. I think I kind of know what to do but it's still fucking confusing
  • The Helper The Helper:
    Maybe you could find some of that information from AC tech - like how they detect freon and such
  • Varine Varine:
    That's mostly what I've been looking at
  • Varine Varine:
    I don't think I'm dealing with quite the same pressures though, at the very least its a significantly smaller system. For the time being I'm just going to put together a quick scrubby box though and hope it works good enough to not make my house toxic
  • Varine Varine:
    I mean I don't use this enough to pose any significant danger I don't think, but I would still rather not be throwing styrene all over the air
  • The Helper The Helper:
    New dessert added to recipes Southern Pecan Praline Cake https://www.thehelper.net/threads/recipe-southern-pecan-praline-cake.193555/
  • The Helper The Helper:
    Another bot invasion 493 members online most of them bots that do not show up on stats
  • Varine Varine:
    I'm looking at a solid 378 guests, but 3 members. Of which two are me and VSNES. The third is unlisted, which makes me think its a ghost.
    +1
  • The Helper The Helper:
    Some members choose invisibility mode
    +1
  • The Helper The Helper:
    I bitch about Xenforo sometimes but it really is full featured you just have to really know what you are doing to get the most out of it.
  • The Helper The Helper:
    It is just not easy to fix styles and customize but it definitely can be done
  • The Helper The Helper:
    I do know this - xenforo dropped the ball by not keeping the vbulletin reputation comments as a feature. The loss of the Reputation comments data when we switched to Xenforo really was the death knell for the site when it came to all the users that left. I know I missed it so much and I got way less interested in the site when that feature was gone and I run the site.
  • Blackveiled Blackveiled:
    People love rep, lol
    +1
  • The Helper The Helper:
    The recipe today is Sloppy Joe Casserole - one of my faves LOL https://www.thehelper.net/threads/sloppy-joe-casserole-with-manwich.193585/
  • The Helper The Helper:
    Decided to put up a healthier type recipe to mix it up - Honey Garlic Shrimp Stir-Fry https://www.thehelper.net/threads/recipe-honey-garlic-shrimp-stir-fry.193595/
  • The Helper The Helper:
    Here is another comfort food favorite - Million Dollar Casserole - https://www.thehelper.net/threads/recipe-million-dollar-casserole.193614/

      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