• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Debugging shows it should work as expected, but it doesn't

Coeniq

Member
Hi there,

I am trying to follow a tutorial, works fine, I am a total beginner.

The tutorial:

Here is code, that should create a deck of five cards, shuffle them and draw them on screen:

GML:
function scr_create_deck(){
    for (i=1;i<=5;i+=1)
    {
        show_debug_message("deckcreation")
        var ran_card=irandom_range(1,12);
        if (ran_card==1) {ds_list_add(deck_p,spr_dorver);}
        else if (ran_card==2) {ds_list_add(deck_p,spr_horned_imp);}
        else if (ran_card==3) {ds_list_add(deck_p,spr_howler);}
        else if (ran_card==4) {ds_list_add(deck_p,spr_minotaur);}
        else if (ran_card==5) {ds_list_add(deck_p,spr_raged_golem);}
        else if (ran_card==6) {ds_list_add(deck_p,spr_rat_king);}
        else if (ran_card==7) {ds_list_add(deck_p,spr_rupert);}
        else if (ran_card==8) {ds_list_add(deck_p,spr_sir_orcy);}
        else if (ran_card==9) {ds_list_add(deck_p,spr_spaceman);}
        else if (ran_card==10) {ds_list_add(deck_p,spr_terror);}
        else if (ran_card==11) {ds_list_add(deck_p,spr_wingy);}
        else if (ran_card==12) {ds_list_add(deck_p,spr_worker);}
    
    }
    show_debug_message(ds_list_find_value(deck_p,0));
    show_debug_message(ds_list_find_value(deck_p,1));
    show_debug_message(ds_list_find_value(deck_p,2));
    show_debug_message(ds_list_find_value(deck_p,3));
    show_debug_message(ds_list_find_value(deck_p,4));
ds_list_shuffle(deck_p); //shuffles the list
}

function scr_draw(num){
    for (i=0;i<num;i+=1) //num is the amount of cards you want to draw
    {
        if (ds_list_size(deck_p)>0) //this means there is actually cards left in the deck
        {
            var card_draw=ds_list_find_value(deck_p,0); //draw the card at the top of the deck
            if (hand_p[1]==noone)
            {
                show_debug_message("first card")
                hand_p[1]=instance_create_layer(50,100,"Instances",obj_card_p);//Create the card
                hand_p[1].sprite_index=card_draw; //Assigns a sprite to the card.
                with (hand_p[1]){scr_assign_stats();} //Assigns the stats depending on the sprite
            }
            else if (hand_p[2]==noone)
            {
                show_debug_message("second card")
                hand_p[2]=instance_create_layer(50,140,"Instances",obj_card_p);
                hand_p[2]=sprite_index=card_draw;
                with (hand_p[2]){scr_assign_stats();}
            }
            else if (hand_p[3]==noone)
            {
                show_debug_message("third card")
                hand_p[3]=instance_create_layer(50,180,"Instances",obj_card_p);
                hand_p[3].sprite_index=card_draw;
                with (hand_p[3]){scr_assign_stats();}
            }       
            else if (hand_p[4]==noone)
            {
                show_debug_message("fourth card")
                hand_p[4]=instance_create_layer(50,220,"Instances",obj_card_p);
                hand_p[4]=sprite_index=card_draw;
                with (hand_p[4]){scr_assign_stats();}
            }
            else if (hand_p[5]==noone)
            {
                show_debug_message("fifth card")
                hand_p[3]=instance_create_layer(50,260,"Instances",obj_card_p);
                hand_p[5]=sprite_index=card_draw;
                with (hand_p[5]){scr_assign_stats();}
            }
            show_debug_message("and delete from deck")
            ds_list_delete(deck_p,0); //deletes the top card since we have drawn it.
        }
        else {break; } //if there are no cards left in the deack, break (end the loop early)
    }
}
Console says:

CreateColPairs took 0.000000s 1 usecs for 5 object types obj_col_numb=0 physobjcount=0 resizes 0 final size 0
deckcreation
deckcreation
deckcreation
deckcreation
deckcreation
4
7
4
11
7
first card
and delete from deck
third card
and delete from deck
second card
and delete from deck
fourth card
and delete from deck
fifth card
and delete from deck
However, it only draws the first two of the deck.

What is going on here?

the create event of my controller object:
GML:
/// @description Initialize

window_set_size(1280,720); //optional
window_set_position(1920/6,1080/6); //sets to center of monitor. (if 1920x1080)
randomize(); //makes the game use a random seed.
room_speed=60; //60fps! no console peasentry!

//create the deck (not required for this game, but wanted to show off lists!
deck_p = ds_list_create(); //Creates a list by the name deck_p. (i.e. deck_player)
scr_create_deck(); //create a deck of random cards

//sets the score for both players
p_score=5;
s_score=5;

//create array to store player cards.
hand_p[1]=noone;
hand_p[2]=noone;
hand_p[3]=noone;
hand_p[4]=noone;
hand_p[5]=noone;

//then draw five cards
scr_draw(5);
 

Alexx

Member
You're not using the variable i, so maybe this would be better:
repeat(5)

This also may not run as intended either, as you run the risk of drawing the same card more than once.

If you're looking 5 different cards, then a different approach for the card selection is required.

The next section also has a glaring error:

ds_list_shuffle(deck_p); //shuffles the list


You're already choosing random cards, this really isn't needed.

I didn't analyse at the rest of your code.
 
Last edited:

Coeniq

Member
You're not using the variable i, so maybe this would be better:
repeat(5)

This also may not run as intended either, as you run the risk of drawing the same card more than once.

If you're looking 5 different cards, then a different approach for the card selection is required.

The next section also has a glaring error:

ds_list_shuffle(deck_p); //shuffles the list


You're already choosing random cards, this really isn't needed.

I didn't analyse at the rest of your code.
Thanks for taking the time to answer me.
Yeah, I know that this will possibly produce multiple instances of the same card, thank you.
 
Top