Quick answer: GameMaker raises unable to convert string "..." to number when a string that is not a clean number reaches real(), arithmetic, a condition or a numeric function argument. The text between the quotes in the message is the value that failed. Find where that text came from (player input, a save file, JSON, an HTTP response), then convert it through one helper that trims it, checks it and falls back to a default.

This error rarely comes from a typo. It comes from data: a save file with a missing key, a text field left empty, a price written as 1,000, a server that sent a number in quotes. GML converts strings to numbers by itself in a few places, and when the text is not numeric it stops the game. The message contains the offending text, so the cause is usually one search away. Below are the forms the message takes in current GameMaker, the places a game runs into it, and a small set of GML functions that make conversion safe. The code assumes GameMaker 2024 or later.

The exact error

Run from the IDE, or in any build made with the VM, the crash box looks like this:

___________________________________________
############################################################################################
ERROR in action number 1
of Create Event for object obj_shop:
unable to convert string "1,000" to number
 at gml_Object_obj_shop_Create_0 (line 12) - price = real(price_text);
############################################################################################
gml_Object_obj_shop_Create_0 (line 12)

A build compiled with YYC prints the same message without source lines, so the stack entries end in (line -1):

ERROR in
action number 1
of  Step Event0
for object obj_text_field:
unable to convert string "" to float
at gml_Object_obj_text_field_Step_0
############################################################################################
gml_Object_obj_text_field_Step_0 (line -1)

The last word changes with what the runtime wanted at that point. These are all the same problem:

unable to convert string "abc" to number
unable to convert string "" to float
unable to convert string "" to int64
unable to convert string "left" to bool

Read it literally. A string value arrived somewhere that needs a number (or a true/false value), GameMaker tried to parse it, and the text was not numeric. The text in quotes is the actual value. Empty quotes mean an empty string, which is the most frequent case of all. For the layout of the box itself, see how to read the GameMaker crash box.

Where GML converts strings by itself

GML has no implicit conversion in most places, which is why the few places that do convert are surprising.

CodeWhat happens
real("42"), real("-3.5"), real("1e3"), real("0x1F")Returns the number. Digits, a minus sign, a decimal point, an exponent and 0x hexadecimal are accepted.
real(""), real("1,000"), real("12px")Throws this error.
10 + "5"15. With a number on the left, a numeric string on the right is converted without a word.
10 + "abc"Throws this error.
"abc" + 10A different error: DoAdd :: Execution Error. With a string on the left, + means concatenation and both sides must be strings.
if ("left")Throws the to bool form.
int64("")Throws the to int64 form.

The silent success of 10 + "5" is what lets the bug reach players. Code that adds a loaded value to a number works for as long as the value happens to be numeric, and fails the first time a file contains something else. The concatenation side of this is covered in the DoAdd :: Execution Error guide.

Comparing a non-numeric string with a number using == does not raise this error. The test is simply false, so the mistake stays hidden until the value is used in arithmetic somewhere else.

What causes it in real projects

  1. A value read from a save or settings file as text. real(ini_read_string("player", "gold", "")) throws for every player whose file has no gold key, because the default is an empty string. A truncated or hand-edited file does the same with whatever text is in it.
  2. Player input passed straight to real(). keyboard_string or a get_string() result that is empty, has a space in it, or contains a letter, a comma or a currency symbol.
  3. Numbers that arrive as strings from outside. The "result" of an HTTP request is always a string. JSON written by another tool often has "level": "3" where you expected 3, and json_parse keeps it a string.
  4. A string used as a condition. if (player_name), or a function that returns a name on success and you test it like a boolean.
  5. A variable that changed type. A counter reset with hp = "" to mean “nothing”, an array or map entry filled with text by one system and read as a number by another, or a value saved with string() while it was unset, which writes the text undefined.
  6. A string passed to a built-in function that wants a number. Coordinates for instance_create_layer(), a position for a draw call, a length for array_create(). Some functions attempt the conversion and throw this error. Others reject the argument with a related message, for example instance_create_depth argument 3 incorrect type (string) expecting a Number (YYGI32).

How to find the value and where it came from

  1. Read the quoted text. "" means a missing or empty value: look at defaults and unfilled fields. A word such as "coast" or "left" is usually a literal in your own code or data files, so search the project for it. A line of unreadable characters means a file was read with the wrong format or is corrupt.
  2. Go to the line. In a VM build the box gives the event and line. In a YYC build you get the event or script name only; run the same steps from the IDE to get the line.
  3. Print the value and its type just before that line. The brackets make an empty string or a trailing space visible:
    show_debug_message($"price_text = [{price_text}] type = {typeof(price_text)}");
    typeof() returns "string", "number", "bool", "int64", "undefined", "array", "struct" and a few others. If it prints string for something you believed was a number, the question becomes where it was assigned.
  4. Trace the assignment. Search the project for the variable name followed by =. Check every place it is set, including Room Creation Code and other objects that write to it with obj.variable or inside with.
  5. Use the debugger when printing is not enough. Start the game in debug mode, put a breakpoint on the failing line, and when it pauses look at the variable in the Locals or Instance window, or add it to Watches. The value is shown with quotes when it is a string.

Fix 1: convert through one checked helper

Put the checks in a single script function and use it everywhere outside data enters the game. GML has had try / catch since version 2.3, and the conversion error can be caught like any other runtime exception.

/// @func safe_real(_value, _default)
/// @desc Returns _value as a number, or _default when it cannot be converted.
function safe_real(_value, _default = 0)
{
    if (is_numeric(_value)) return _value;
    if (!is_string(_value)) return _default;

    var _text = string_trim(_value);
    _text = string_replace_all(_text, ",", "");
    if (_text == "") return _default;

    var _result = _default;
    try
    {
        _result = real(_text);
    }
    catch (_error)
    {
        _result = _default;
    }
    return _result;
}

is_numeric() is true for reals, int32, int64 and booleans, so a value that is already a number passes straight through. is_real() is narrower and returns false for int64 values, which include enum members. undefined, arrays and structs fall through to the default.

The broken line from the top of this page becomes:

// Broken
price = real(price_text);

// Fixed
price = safe_real(price_text, 0);

Decide per call what the default should be, and whether a bad value deserves a log line. A silent default hides corrupt saves, so in a loader it is worth one show_debug_message() when the fallback is used.

Fix 2: clean player input before converting

For a field that only takes a whole positive number, string_digits() removes everything that is not a digit. It can leave an empty string, so check for that:

// Step event of a quantity field
if (keyboard_check_pressed(vk_enter))
{
    var _typed = string_digits(keyboard_string);
    quantity = (_typed == "") ? 1 : real(_typed);
    keyboard_string = "";
}

Be careful using string_digits() on anything else. It also removes the minus sign and the decimal point, so real(string_digits("-3.5")) is 35. For signed or decimal input use safe_real(), which rejects the text instead of rewriting it. To test whether a string is digits only without converting it, compare lengths: string_length(string_digits(_text)) == string_length(_text).

Fix 3: read saves and network data with defaults

Read numbers with the function that returns numbers. ini_read_real() does its own parsing and takes a numeric default, so a missing key never produces an empty string:

// Broken: a missing key gives "", and real("") throws
ini_open("save.ini");
gold = real(ini_read_string("player", "gold", ""));
ini_close();

// Fixed
ini_open("save.ini");
gold = ini_read_real("player", "gold", 0);
ini_close();
gold = max(0, gold);

Write with the matching function too (ini_write_real() for numbers), and clamp what you read to a sane range, because a hand-edited file can hold a valid number that is still nonsense for your game.

For JSON, check the shape and convert each numeric member. json_parse() throws on invalid JSON, so it gets its own try:

function load_progress(_json_text)
{
    var _data = undefined;
    try
    {
        _data = json_parse(_json_text);
    }
    catch (_error)
    {
        show_debug_message("Save is not valid JSON: " + _error.message);
    }

    var _level = 1;
    if (is_struct(_data) && struct_exists(_data, "level"))
    {
        _level = safe_real(_data.level, 1);
    }
    return _level;
}

In an Async HTTP event, the body is text even when the server sent a bare number:

if (async_load[? "id"] == request_id && async_load[? "status"] == 0)
{
    high_score = safe_real(async_load[? "result"], high_score);
}

More on the JSON side is in the json_parse error guide.

Fix 4: keep one type per variable

For the to bool form, write the comparison you mean:

// Broken: a string is not a condition
if (player_name) { show_name = true; }

// Fixed
if (player_name != "") { show_name = true; }

For values that drift between types, pick a sentinel of the same type and keep to it. Use -1 or 0 for “no number yet” and "" only for variables that always hold text. When a variable can legitimately be unset, use undefined and test it with is_undefined() before doing arithmetic. An unset variable that was never assigned at all gives a different error, covered in Variable not set before reading it.

How to confirm the fix

Seeing the bad value from a player’s machine

The conversion error names the exact text that failed, which makes a player’s crash report unusually useful here: it tells you what was in their save or what they typed. Bugnet’s GameMaker SDK installs an exception_unhandled_handler in bugnet_init() that saves the error and files it on the next launch (GameMaker closes the game when that handler returns), with the stack, platform, OS and game version. If you catch the error yourself, bugnet_exception_handler(_error) inside the catch block files it immediately and the game keeps running. Reports are grouped by title and the title contains the message, so two players with different bad values appear as two entries. That split shows whether you have one broken default or a file format problem. It cannot send you the save file unless you attach it.

Frequently asked questions

What does unable to convert string to number mean in GameMaker?

A string value reached code that needs a number, such as real(), arithmetic with a number on the left, or a numeric function argument, and its text was not a valid number. The text shown in quotes in the message is the value that failed.

Why does GameMaker say unable to convert string “” to number?

Empty quotes mean the value was an empty string. The usual sources are a save or ini key that does not exist and is read with an empty default, a text field the player left blank, or a variable reset to an empty string.

How do I safely convert a string to a number in GML?

Trim the text, remove thousands separators, return a default for an empty string, and call real() inside a try block so a bad value falls back to the default. Wrap that in one function and use it for every value that comes from input, files or the network.

What is the difference between this error and DoAdd :: Execution Error?

Operand order. With a number on the left of a plus sign, GameMaker tries to convert the string on the right and raises the conversion error if it cannot. With a string on the left, plus means concatenation and a number on the right raises the DoAdd error.

Does string_digits make real() safe?

Only for whole positive numbers, and only if you also handle the empty result. string_digits removes every character that is not a digit, including the minus sign and decimal point, so a value like -3.5 would turn into 35.

The crash message already contains the value that broke the conversion. Read what is between the quotes before you read any code, because an empty pair of quotes and a word like coast send you to different places.