GMS 2 (SOLVED)Issue with setting local variable to (global variable value - amount)

H

HanNic3197

Guest
Hello,

I keep getting the following error:


when trying to do the following code:
Code:
new_total_coins = old_total_coins - global.costume_price
if(draw_coin_amount > new_total_coins)
{
    switch(draw_coin_amount)
   {
       case 0:
           draw_coin_amount= global.coins_totalcollected;
           alarm_set(0,(room/speed)/2);
           break;
       case 1:
           draw_coin_amount= drawn_coin_amount - 1; 
           alarm_set(0,(room_speed)/2);
           break;
   }
      
}
I'm trying to make a "countdown" animation with draw text to lower the bank amount to a new total. So I want the new total price = old total price - price of purchase

I thought it was possible to set a variable equal to (x - y) or (5 - 3), etc. What am I doing wrong?

Thank in advance!
 
I suppose you're trying to subtract a real number from a string.
Make sure new_total_coins, old_total_coins and global.costume_price all are real number variables.
 
H

HanNic3197

Guest
I suppose you're trying to subtract a real number from a string.
Make sure new_total_coins, old_total_coins and global.costume_price all are real number variables.
I have just double checked with debug messages and they are indeed real numbers old_total_coins = 82 and global.costume_price = 25
 

Relic

Member
What debug message and when? Did you check the line before this alarm code?

Are you aware that in gms 2 that show_debug_message accepts either a string or real argument?
 

TheouAegis

Member
Post ALL code that sets old_total_coins and ALL code that sets global.costume_price.

I'm guessing one of these is set from a file.
 
H

HanNic3197

Guest
That seems strange. Can you put additional code?
What debug message and when? Did you check the line before this alarm code?

Are you aware that in gms 2 that show_debug_message accepts either a string or real argument?
I am very new to Gamemaker and coding for that matter, so to be honest I'm not sure I understand the difference. I'm guessing that is the difference between a function and an integer?

Post ALL code that sets old_total_coins and ALL code that sets global.costume_price.

I'm guessing one of these is set from a file.
This is true, the global.coins_totalcollected is set from an ini
But I didn't know this would affect it.

This should be all the code that affects this:
DRAW event of obj_store: (Everything retrieved from the ini or json is done here, the json contains the array for the store items)
Code:
if (mouse_check_button_released(mb_left) and global.store_pause = false and (room_width - 100) >= mouse_x and mouse_x >= (room_width/2))
{
    var i = 0
    for (i = 0; i < global.totalStoreItems; i += 1)
    {
        if ((mouse_y >= global.sprite_pos_y[i] - 100) and (mouse_y <= global.sprite_pos_y[i] + 100))
        {
            var temp_y = global.sprite_pos_y[i]
            global.sprite_numb = i
        }
        
    }

    storeEntry = global.storeItems[global.sprite_numb]

    
    
    if ((storeEntry[? "PURCHASED"]) = "true")
    {
        global.costume = (storeEntry[? "NAME"])
        show_debug_message(global.costume)
    }
    else
    {
        global.costume_choice = asset_get_index((storeEntry[? "NAME"]))
        global.store_pause = true
        draw_roundrect((room_width/2) - 500,300, (room_width/2) + 500, room_height - 300, false)
    }
}

if global.store_pause = true
{
    var text_y = 350
    storeEntry = global.storeItems[global.sprite_numb]
    
    //purchase popup
    if (global.purchase = true)
    {
        if global.price_closed = true
        {
        draw_set_color(c_black)
        //background
        draw_roundrect((room_width/2) - 300,500, (room_width/2) + 300, room_height - 500, false)
        //text
        draw_set_color(c_white)
        draw_text((room_width/2) - ((string_width("You've purchased")/2)), (room_height/2) - 250, "You've purchased")
        draw_text((room_width/2) - (string_width((storeEntry[? "NAME"])))/2, ((room_height/2) - 175), (storeEntry[? "NAME"]))
        draw_text((room_width/2) - (string_width("Price:"))/2, room_height/2 + 150, "Price:")
        draw_text((room_width/2) - (string_width((storeEntry[? "PRICE"]))), (((room_height/2) + 200)), (storeEntry[? "PRICE"]))
        draw_sprite_stretched(spr_coin1, 0, (room_width/2) + 5, (room_height/2) + 200, 50, 50)
        //costume
        draw_roundrect((room_width/2) - 125, (room_height/2) - 100, (room_width/2) + 125, (room_height/2) + 125, false)
        draw_sprite_stretched(global.costume_choice, 0, (room_width/2) - (sprite_get_width(global.costume_choice) - 25), (room_height/2) - 50, 150, 150 )
        }
    
        if keyboard_check_released(vk_enter)
        {
            global.price_closed = false   
        }
        
        //Purchase Finalized
        if global.price_closed = false
        {
            global.costume_price = string((storeEntry[? "PRICE"]))
            show_debug_message(global.costume_price)
            instance_activate_object(obj_purchase) //activates obj with code containing the issue
        }
        else
        {
            instance_deactivate_object(obj_purchase)
        }
    }
CREATE Event of obj_purchase:
Code:
restarting = false
draw_coin_amount = -1
new_total_coins = false
old_total_coins = string(global.coins_totalcollected)
show_debug_message(old_total_coins)
STEP Event of obj_purchase:
Code:
if(restarting = false and draw_coin_amount =-1)
{
    draw_coin_amount = string(global.coins_totalcollected); 
    alarm_set(0,room_speed/2); 
    show_debug_message("STEP")
}
ALARM 0 of obj_purchase:
Code:
new_total_coins = old_total_coins - global.costume_price
if(draw_coin_amount > new_total_coins)
{
    switch(draw_coin_amount)
   {
       case 0:
           draw_coin_amount= global.coins_totalcollected;
           alarm_set(0,(room/speed)/2);
           break;
       case 1:
           draw_coin_amount= drawn_coin_amount - 1; 
           alarm_set(0,(room_speed)/2);
           break;
   }
}
DRAW Event of obj_purchase:
Code:
draw_roundrect_color((room_width/2) - 200, (room_height/2) - 75, (room_width/2) + 200, (room_height/2) + 75, c_white, c_gray, false);

draw_set_color(c_black);
switch (draw_coin_amount)
{
    case 0:
        draw_text((room_width/2) + ((string_width(global.coins_totalcollected)/2)), (room_height/2), draw_coin_amount)
        break;
    case 1:
        draw_text((room_width/2) + ((string_width(global.coins_totalcollected)/2)), (room_height/2), draw_coin_amount)
        break;
}
Hope someone can spot something I can't! Thanks for all the replies!
 
H

HanNic3197

Guest
Post ALL code that sets old_total_coins and ALL code that sets global.costume_price.

I'm guessing one of these is set from a file.
Additionally, the costume price is retrieved from the json, turned into a global variable, then turned into a local.. probably excessive. how should I go about this better?
 
H

HanNic3197

Guest
I tried to do real() instead of string() and I think that worked. I'm going to play around with it a bit and see if that did it. Then I'll mark this solved. I didn't know real() was a function or real numbers were a necessity for this. so thanks all!
 

TheouAegis

Member
I'm not sure about JSON, but usually when you read from a file you have the option of reading the data in the file as a string or as a real to begin with. So like I said, there was a good chance the data is being read as a string which makes the variable a string. The real() function will take something you accidentally retrieved as a string and attempt to turn it into a real. So you should be ok.
 
Top