GML Looking for advice on how to streamline this code.

Discussion in 'Programming' started by create_event@, Jun 13, 2018.

  1. So in writing this, I got hung up and almost came crying to the forums, but I managed to fix it. Yay for me.

    However, I'm convinced this code could be made better, so If anybody wants to look it over and see what could be made smoother...be my guest, I'll learn from my mistakes (I hope).

    Code:
    
    Create Event:
    
    execute code:
    
    ///basic vars
    //****INITIAL DIRECTION IS SET BY PARENT;
    
    //CODE IS WRITTEN FOR PROOF-OF-CONCEPT, ASSUMES 3 UNITS A.T.M. BUT CAN BE CHANGED
    //WITH A SINGLE NUMBER REPLACEMENT.
    
    
    fight_group[0]=0;    //THIS IS AN ARRAY MEANT TO STORE THE IDs OF EACH UNIT IN THE SQUAD
    squadcount = 0;        //USED LATER TO CREATE FULL SQUADRON
    squadform = 0;        // USED LATER TO DETERMINE SQUADRON LAYOUT
    active = 0;        //USED TO TELL FIGHTER IF IT CAN BEGIN NORMAL MOVEMENT OPERATION
    lead = 0;        //IS THIS UNIT THE SQUAD LEADER?
    orders = 0;        //FOR FUTURE FEATURES - ONLY USED FOR TESTING
    target = instance_nearest(x,y,o_practice);    //PRESETS TARGET - TESTING ONLY
    
    
    Code:
    Alarm Event for alarm 0:    //THIS ALARM IS USED TO ACTIVATE THE UNIT AFTER LAUNCH
                    //IN PRACTICE, THE HANGARBAY WILL LAUNCH IT AT 90d ANGLE
                    //AND SET THIS ALARM
    execute code:
    
    ///finish launching
    
    active = 1;    //PRESET TO 0, USED DURING STEP EVENT
    speed = 3;    //SETS FULL SPEED
    
    
    Code:
    Alarm Event for alarm 1:    //THIS ALARM IS USED TO CREATE THE FULL SQUADRON->
                    //AND IS ONLY USED BY THE FIRST UNIT->
                    //STARTED BY HANGARBAY
    
    execute code:
    
    ///create squadron
    
    var  fighter1, fighter2;        //VARS USED IN THIS EVENT
    
    squadcount = 0;                //USED TO INCREASE ARRAY COUNT
    
    fight_group[ squadcount ] = id;        //ADD THIS UNIT TO ROSTER
    
    lead = 1;                //SETS THIS UNIT AS SQUAD LEADER
    
    do        //THIS " do until" BLOCK CREATES THE FULL SQUADRON
        {
        fighter1 = instance_create( x,y,p1_fighter ); //CREATE THE FIRST SQUADMATE
            {
            squadcount += 1;                  //INCREASE UNITS IN SQUAD
            fight_group[ squadcount ] = fighter1;          //ADD UNIT TO ROSTER
            fighter1.squadform = squadcount;          //TELL UNIT WHERE IT BELONGS IN SQUAD
            fighter1.speed = .75;                  //SET UNIT SPEED
            fighter1.direction = direction + squadcount * 10; //MAKE THE UNIT DRIFT LEFT
            fighter1.alarm[ 0 ] = 40;              //SET UNIT ACTIVATION ALARM
            }
    //************************
        fighter2 = instance_create( x,y,p1_fighter ); //CREATE THE SECOND SQUADMATE
            {
            squadcount += 1;                  //INCREASE UNITS IN SQUAD
            fight_group[ squadcount ] = fighter2;          //ADD SECOND UNIT TO ROSTER
            fighter2.squadform = squadcount;          //TELL SECOND UNIT WHERE IT BELONGS IN SQUAD
            fighter2.speed = .75;                  //SET UNIT SPEED
            fighter2.direction = direction - squadcount * 15;  //MAKE UNIT DRIFT RIGHT
            fighter2.alarm[ 0 ] = 40;               //SET SECOND UNIT ACTIVATION ALARM
            }
        }
    until
        squadcount + 1 == 3;             //CHANGE THIS TO INCREASE NUMBER OF UNITS IN SQUAD
    
    //**NEXT CODE IS USED TO PASS THE SQUAD ROSTER "fight_group[x]"
    //**TO ALL UNITS IN THE ROSTER - RIGHT NOW, ONLY THIS UNIT KNOWS THE ROSTER
    
    var i;
    i=1;
    
    do
    {
    //SET FIRST SQUADMATES ROSTER EQUAL TO THIS FIGHTERS ROSTER
        fight_group[i].fight_group = fight_group;
    //*******GO TO NEXT UNIT
        i += 1;
    }
    until i == squadcount + 1 //DOES IT MATCH NUMBER OF UNITS IN SQUAD?
    
    
    Code:
    Step Event:
    
    execute code:
    
    ///movement controls / fire controls
    var attack_len, attack_dir, squadposx, squadposy;    //VARS USED IN THIS EVENT ONLY
    attack_len = point_distance(x,y,target.x,target.y);    //SETS ATTACK TARGET DISTANCE
    attack_dir = point_direction(x,y,target.x,target.y);    //SETS ATTACK TARGET DIRECTION
    //***************IF ORDERED TO ATTACK
    
    if active == 1 && orders == 1        //IS LAUNCHING DONE? YES! DOES IT HAVE ATTACK ORDERS? YES!
        {
        speed = 3;                //THEN MAX SPEED
        mp_potential_settings(6,4,10,false);    //SETTINGS IN CASE IT NEEDS TO TURN
            if attack_len > 35            //IT CAN ONLY TURN IF IT IS THIS FAR AWAY
            mp_potential_step(target.x,target.y,0,false);    //TURNS TOWARDS TARGET, IF NEEDED
        }
    
    //***************IF IDLE
    
    if active == 1 && orders == 0        //IS LAUNCHING DONE? YES! DOES IT HAVE ATTACK ORDERS? NO.
    {
    
        if lead == 1        //IF UNIT IS LEADER
                {
            speed = 1;     //LOW SPEED
            direction += 75  //JUST MAKE CIRCLES
                }
    
    
        if lead == 0                //IF UNIT IS NOT LEADER
            {
            //CREATES THE FIRST STEP OF A PYRAMID FORMATION
            if squadform = 1{
                squadposx = lengthdir_x(7,fight_group[0].direction-135);
                squadposy = lengthdir_y(7,fight_group[0].direction-135); }  //THIS ALL ASSUMES A SQUAD SIZE
                                        //OF 5, BUT WORKS OTHERWISE
            //MIRRORS THE FIRST STEP OF A PYRAMID FORMATION
            if squadform = 2{
                squadposx = lengthdir_x(7,fight_group[0].direction-225);
                squadposy = lengthdir_y(7,fight_group[0].direction-225); }
    
            //CREATES THE SECOND STEP OF A PYRAMID FORMATION
            if squadform = 3{
                squadposx = lengthdir_x(14,fight_group[0].direction-145);
                squadposy = lengthdir_y(14,fight_group[0].direction-145); }
    
            //MIRRORS THE SECOND STEP OF A PYRAMID FORMATION
          if squadform = 4{
                squadposx = lengthdir_x(14,fight_group[0].direction-215);
                squadposy = lengthdir_y(14,fight_group[0].direction-215); }
      
    
        if point_distance(x,y,squadposx,squadposy) < 3     //IF CLOSE TO PROPER SQUAD LOCATION
            speed = 1;                     //SAME SPEED AS LEADER
        else
            speed = 1.05;                     //IF NOT, GO FASTER TO CATCH UP
    
    
        //******//THE TURN SETTINGS FOR SQUADMATES ARE DIFFERENT, MAKES FOR EASIER TURNS
            mp_potential_settings(10,4,10,false);
        //******//GIVES UNIT PYRAMID LOCATION TO MOVE TOWARDS
          mp_potential_step(fight_group[0].x+squadposx,fight_group[0].y+squadposy,0,false);
            }
    
    }
    
    
    Code:
    Draw Event://THIS STUFF JUST TURNS THE SPRITE, IGNORE THIS
    
    execute code:
    
    ///draw direction
    
    image_angle = direction;
    draw_self();
    
     
    Last edited by a moderator: Jun 13, 2018
  2. obscene

    obscene Member

    Joined:
    Jun 21, 2016
    Posts:
    2,409
    Most of us are going to look at that and have no idea what is or what you are doing, much less help you streamline it. :)
     
    EvanSki and Ralucipe like this.
  3. Well, let me clarify. It takes a group of space fighters, and causes them to move in a squadron formation while idle(no attack orders), or to split and do attack runs while under orders to attack a unit. It keeps track of how many fighters are in the squadron, and will eventually be coded to replace the leader if it is destroyed. I did put in comments to help anyone who's looking to understand it.

    And If you don't, or aren't interested in giving out advice, ignore me! It's simple!
     
  4. Ralucipe

    Ralucipe Member GMCJam Champ

    Joined:
    Sep 1, 2016
    Posts:
    104
    But, if he (and everyone else on this forum) just ignored you, then you would end up sitting here wondering why nobody's replying. We just want you to help us help you.

    Even with your clarification, the code you posted is long and it's hard to follow what is happening in it. Furthermore, the range of advice you are seeking is quite broad. All of this means you are unlikely to attract helpful responses. Consider:
    • Reducing the amount of code for us to review to only what's necessary
    • Further explaining the context of the relevant code
    • Narrowing down what exactly you are seeking from the community
     
    EvanSki likes this.
  5. No, I actually expected no responses. I realize the code is a bit longer and (somewhat) convoluted, and only made the thread on the offchance I could get a suggestion or two. Assume the worst and you'll be pleasantly surprised, is my general (not primary) viewpoint.

    Thank you for the advice, I'll try to make it more palatable to viewing/understanding.
     
  6. How's that, better?

    As for what I hope the community can do, is to tell me what I've made more difficult than it needs to be, and show me where it can be shortened / made easier.
     
  7. Gonna bump this once, and then be on my merry way. The code is working correctly, so refinement isn't needed, per se.
     
  8. CloseRange

    CloseRange Member

    Joined:
    Jul 2, 2016
    Posts:
    758
    As long as you understand common coding techniques you shouldn't need to know what a code does in order to improve it.
    I cleaned up the code and I'll give you my feedback on what I saw, but only for alarm 1 because there was already so much in just that one chunk
    by the end, you'll see how to get that alarm down to 11 lines of code (#clickbait)

    1)
    Ever heard of the boy who cried wolf? That's your comments.
    Commenting is good but you don't need to comment everything
    When we see:
    Code:
    var attack_len, attack_dir, squadposx, sqadposy;
    we already know those are variables only used in that event, that's why it has var in front of it.

    2)
    Code:
    /// alarm event 1
    var i;
    i=1;
    if you're going to declare the variable right there, just make it one line:
    Code:
    var i = 1;
    3)
    Code:
    /// alarm event 1
    until squadcount + 1 == 3
    subtract 1 from both side:
    Code:
    until squadcount == 2
    4)
    on the note of do until statements, don't use them unless you absolutely have to.
    They will confuse you and anyone reading them because they are rarely used.
    If you can, use a for statement or while statement.
    In alarm 1 you had this:
    Code:
    squadcount = 0;
    do {
         squadcount+=1;
        // some code
    } until squadcount == 2;
    ( I changed the until squadcount +1 == 3 here)
    you should rewrite this:
    Code:
    squadcount = 0;
    while(squadcount < 3) {
          squadcount++;
          // some code
    }
    5)
    on this note also
    don't ever write:
    variable += 1;
    always write
    variable++;
    that's mainstream and means increment.
    You can use ++ (increment) inside of statements as well and it will change the variable directly.

    6)
    in alarm 1 you wrote 2 do statements.
    By the time the first one finishes squadcount is GUARANTEED to always be 2.
    This is because it goes until squadcount +1 == 3 (so 2)
    the next do until statement uses squadcount + 1 for its condition, however you could just use 3, because squadcount is always 2.
    you could also use a for statement here:

    Code:
    for(var i=1; i<squadcount; i++) {
         // do stuff
    }
    7)
    I just noticed your first do statement is pointless
    at the start squadcount is 0
    in the statement sqaudcount is increased by 1, two times.
    the condition is if squadcount + 1 == 3
    squadcount is 2 (because itw as increased by 2)
    2 + 1 == 3
    so it always runs 1 time guaranteed

    8)
    Looking at the code and comments, there is one main fighter, then it creates 2 more fighters that fork off from the first? The one that moves left will be offset by 10 degrees while the one on the right by 30 (not sure why they arn't the same?).
    You used spaghetti code to do this though, what if you decided later to have have bigger squadrons? like 4 or 5? My suggestion is to make a script called add_squadron();
    Code:
    /// add_squadron(spd, dir);
    var spd = argument[0];
    var dir = argument[1];
    
    var fighter = instance_create(x, y, p1_fighter);
    fight_group[++squadcount] = fighter; // putting the ++ before the variable makes it increase by 1 before using it in the array
    fighter.squadform = squadcount;
    fighter.speed = spd;
    fighter.direction = direction + dir;
    fighter.alarm[0] = 40;
    
    now you just have to say:

    Code:
    add_squadron(.75, squadcount *10);
    add_squadron(.75, squadcount *-15);

    there is also a lot more I could go into just with this alarm event (I didn't even look at the step event)
    so i'm going to rewrite the code in my style, how I would have done it (without the script). You should compare it to what you have to see the differences:

    Code:
    /// alarm 1;
    squadcount = 0;
    fight_group[0] = id;
    lead = 1;
    
    var fighter = instance_create(x, y, p1_fighter);
    fight_group[++squadcount] = fighter; // putting the ++ before the variable makes it increase by 1 before using it in the array
    fighter.squadform = squadcount;
    fighter.speed = .75;
    fighter.direction = direction + squadcount * 10;
    fighter.alarm[ 0 ] = 40;
    
    var fighter = instance_create(x, y, p1_fighter);
    fight_group[++squadcount] = fighter;
    fighter.squadform = squadcount;
    fighter.speed = .75;
    fighter.direction = direction - squadcount * 15;
    fighter.alarm[ 0 ] = 40;
    
    for(var i=1; i<squadcount; i++) {
        fight_group[i].fight_group = fight_group;
    }
    
    from 49 lines to 23 lines
    and if you use the function:

    Code:
    /// alarm 1;
    squadcount = 0;
    fight_group[0] = id;
    lead = 1;
    
    add_squadron(.75, squadcount *10);
    add_squadron(.75, squadcount *-15);
    
    for(var i=1; i<squadcount; i++) {
        fight_group[i].fight_group = fight_group;
    }
    
    11 lines
    want to add more people to the squad?
    just use add_squadron again.
    It will automatically add 1 to the squadcount, make the unit, put it in the array, and set all its variables.
     
    Last edited: Jun 15, 2018
  9. Cool stuff! Couple things I didn't know.

    My responses -
    1)My code isn't usually excessively commented, just did that for the post.
    6) / 7) Right now, it's supposed to stop after one run-through - The code is designed to be able to change the number of units in a squadron, just by changing the "do until squadcount=" requirement. (ATM it supports 5 fighters)
    8)Didn't even realize the second fighter was moving off 30 degrees- I'll have to fix that. As for changing squad size, see above.

    I'll probably move stuff like the fighter creation to scripts, but for now I'm just getting my ideas to work.

    Thank you for your time and suggestions!
     
  10. CloseRange

    CloseRange Member

    Joined:
    Jul 2, 2016
    Posts:
    758
    For 1 i figured it was for the post i never use comments myself but for a post even never over complex it

    for 6/7 it supports 5 fighters but does it support 4 and 6? :rolleyes:
    it can support odd numbers but not even.
    I mean you probably won't ever do even numbered squads but you wanted streamline and it's good to get in the habit of flexibility.
    Along with that you should make a variable called max_squads or squadToMake or something along those lines and set it to how many you want to make.

    yes the code will work to create multiple fighters but it's also better to use scripts for things like that again to make things more organized and titty for yourself. It's easier to understand what you're doing
     
  11. Yeah, I guess I did comment-bomb it.

    No it won't work for an even-numbered squad, but honestly it wouldn't take much to fix that. I just have to add more squad position coding to the movement stuff, as I couldn't figure out a way to make the pyramid formation apart from the weird way I did it.

    Spaghetti code is right...
    Time to rewrite!

    Also, "more organized and titty for yourself"??? That's gotta be some sort of typo...
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice