Help! Errors drawing text in RPG Combat System

Discussion in 'Programming' started by Inspiratium Entertainment, Oct 4, 2019.

  1. Inspiratium Entertainment

    Inspiratium Entertainment Member

    Joined:
    Oct 4, 2019
    Posts:
    2
    [​IMG]

    Help!


    Hey guys, so, this is going to be a lot of code to look at, but, if you have time can you tell me what I'm doing wrong here?
    I'm trying to make things update the EnemyHealthDisplay and PlayerHealthDisplay once attacks complete. I'm not sure if attacks are just not compkleting successfully or I'm doing something else wrong. I'll provide the code for the create, draw_gui, and step events.

    Create Event
    Code:
    //Menu setup
    
    //set current view mode
    CurrentPlayerMode = "platformer";
    
    //set global variables
    playerHealth = 25;
    playerMaxHealth = 25;
    playerBaseDamage = 5;
    playerAccuracy = 0;
    playerCanHit = false;
    weaponDamage = 0;
    
    EnemyMaxHealth = 25;
    EnemyHealth = 25;
    EnemyAttack = 1;
    EnemyAccuracy = 1;
    EnemyBaseDamage = 5;
    EnemyCanHit = false;
    
    //gui dimensions
    gui_width = display_get_gui_width();
    gui_height = display_get_gui_height();
    //gui_margin = 32;
    
    menu_x = gui_width +200;
    menu_y = gui_height -200;
    menu_x_target = gui_width * .75;
    menu_speed = 25; //lower is faster
    menu_font = font_Menu;
    menu_itemHeight = font_get_size(font_Menu);
    menu_committed = -1;
    menu_control = true;
    
    menu_array[2] = "Fight";
    menu_array[1] = "Items";
    menu_array[0] = "Flee";
    
    menu_items = array_length_1d(menu_array);
    menu_cursor = 2;
    
    //logic for fight stats controlled by fightscreen obj
    playerDamage = 0;
    criticalChance = 0;
    damageModifier = 0;
    enemyDamage = 0;
    
    
    //alarm for timing enemy attacks
    alarm[0] =  0;
    fightTimer = false;
    
    Draw_GUI Event
    Code:
    //Draw Menu
    
    draw_set_font(menu_font);
    draw_set_halign(fa_left);
    draw_set_valign(fa_bottom);
    
    for (var i = 0; i < menu_items; i++) {
        var offset = 2;
        var txt = menu_array[i];
        if (menu_cursor == i) {
            txt = string_insert("> ", txt, 0);
            var color = c_white;
        }
        else {
            var color = c_gray;   
        }
        var xx = menu_x;
        var yy = menu_y - (menu_itemHeight * (i * 1.5));
        draw_set_color(c_black);
        draw_text(xx-offset,yy,txt);
        draw_text(xx+offset,yy,txt);
        draw_text(xx,yy+offset,txt);
        draw_text(xx,yy-offset,txt);
        draw_set_color(color);
        draw_text(xx,yy,txt);
        
    }
    
    
    playerHealthDisplay = string(playerHealth) + "/" + string(playerMaxHealth);
    draw_set_color(color);
    draw_text(400, 400, playerHealthDisplay);
    
    enemyHealthDisplay = string(EnemyHealth) + "/" + string(EnemyMaxHealth);
    draw_set_color(color);
    draw_text(600, 400, enemyHealthDisplay);
    
    Step Event
    Code:
    //Menu control logic
    
    //menu transitions from right of screen inward
    menu_x += (menu_x_target - menu_x) / menu_speed;
    
    //Keyboard menu controls
    if (menu_control == true) {
        if (keyboard_check_pressed(ord("W"))) {
         menu_cursor++;
            if (menu_cursor >= menu_items) {
                menu_cursor = 0;
            }
        }
            if (keyboard_check_pressed(ord("S"))) {
         menu_cursor--;
            if (menu_cursor < 0) {
                menu_cursor = menu_items - 1;
            }
        }
        
        if (keyboard_check_pressed(vk_enter)) {
            menu_committed = menu_cursor;
            menu_control = false;
        }
    }
    
    //perform committed menu action
    if (menu_committed != -1) {
        switch(menu_committed) {
        case 2: default:
            randomize();
            //get random number for accuracy check
            playerAccuracy = random_range(1,10);
            if (playerAccuracy > 3) {
                playerCanHit = true;
            }
            if (playerAccuracy < 3) {
                playerCanHit = false;
            }
            //begin attack logic if CanHit is true
            if (playerCanHit = true) {
                //roll critical hit chance logic, if a 3 is rolled a second roll starts
                criticalChance = random_range(1,3);
                //perform second roll for 50/50 chance of succeeding at critical
                if (criticalChance = 3) {
                    var critical5050 = random_range(1,2);
                    if (critical5050 = 1) {
                        damageModifier = 3;
                    }
                    if (critical5050 = 2) {
                    damageModifier = 2;
                    }
                }
                //perform calculation for and apply player attack damage
                playerDamage = damageModifier * (playerBaseDamage + weaponDamage);
                EnemyHealth = EnemyHealth - playerDamage;
                draw_set_color(c_white);
                draw_text(600, 400, enemyHealthDisplay); break;
                
                //start alarm timer for enemy attack
                fightTimer = true;
                if (fightTimer = true) {
                alarm[0] = room_speed * 3;   
                }
                EnemyAccuracy = random_range(1,10);
            if (EnemyAccuracy > 3) {
                EnemyCanHit = true;
            }
            if (EnemyAccuracy < 3) {
                EnemyCanHit = false;
            }
            //begin attack logic if CanHit is true
            if (EnemyCanHit = true) {
                //roll critical hit chance logic, if a 3 is rolled a second roll starts
                criticalChance = random_range(1,3);
                //perform second roll for 50/50 chance of succeeding at critical
                if (criticalChance = 3) {
                    var critical5050 = random_range(1,2);
                    if (critical5050 = 1) {
                        damageModifier = 3;
                    }
                    if (critical5050 = 2) {
                    damageModifier = 2;
                    }
                }       
            }
        }
        //set fightTimer to false and apply enemy damage to player
        fightTimer = false;
        enemyDamage = (damageModifier * EnemyBaseDamage);
        playerHealth = playerHealth - enemyDamage;
        draw_text(400, 400, playerHealthDisplay);
        menu_control = true;
        menu_committed = -1;
            break;
        case 0: game_end(); break;
        }
    }
    
    
    
     
  2. ParodyKnaveBob

    ParodyKnaveBob The Laughing Rogue

    Joined:
    Jun 23, 2016
    Posts:
    537
    Howdy, IE, and welcome to the GMC.

    Some issues & questions, not all directly related to your problem:
    • You try to draw_text() your player/enemy display strings in the Step Event. That's just gonna get wiped over when the application_surface gets cleared every Draw cycle anyway. (But then, you also draw_text() this in your Draw GUI, too. At any rate, might as well remove that from the Step.)
    • You check for player/enemy accuracy being only < or > 3. Anytime it's 3, the CanHit check can only read from the previous value. The first time it's run, that's false of course because of the Create Event. I don't know if maybe you wanted it to check <= 3 vs. > 3, or perhaps < 3 vs. >= 3. Either way, you should probably handle what happens on 3. Also, tip, you can change 6 lines to 1 by dropping the if() statements for a single playerCanHit = playerAccuracy >= 3; and EnemyCanHit = EnemyAccuracy >= 3; (or > if that's what you need).
    • You randomize() the seed every time the Step Event runs the attacking code. You only need to randomize() once, generally at the very beginning of the game, and then GM will advance the seed to random numbers on its own. Also, while testing, and especially while debugging, it can often be nice to comment out the randomize() function in order to get consistent results.
    • You commented that a bunch of variables are global, but unless you set globalvar somewhere else, they're not global. I suppose perhaps you meant they're intended for global use and access even though they're assigned to whatever this instance is? What is this object by the way? I suppose it's the main controller.
    • What exactly is the problem by the way? You stated what you're trying to do, which is good, but you didn't say what it's doing instead. My guess is it's not updating the two health display variables.
    • Finally, probably most importantly, you said you're not sure if the attacks are even completing properly? Ah-ha, that says you haven't run this through the debugger's Step Through Code yet. I suggest you set a breakpoint on the switch() statement's line, run the game, do your menu thing, and once the debugger stops the code step through the code one line at a time, watching the variables while you're there, and see just what in the world it's doing.
    I hope this helps!
     

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