SOLVED unable to convert string to float

jbug

Member
hello i’m following friendly cosmonaut’s farming RPG tutorial (the 19th episode) and i am getting this error (see attachment)Screen Shot 2020-11-19 at 2.39.18 PM.png

heres everything i got from line 24 to the end:

GML:
//Draw Text
if(counter < str_len)
{
    counter++;
}
var substr = string_copy(text_wrapped, 1, counter);
c = text_col;
draw_text_ext_color(text_x, text_y, substr, text_height, text_max_width, c,c,c,c, 1);
thank you guys so much for the help :)
 

Roldy

Member
You might want to post the code that is causing the error.

Line 24 of the Draw event for object oTextbox: if (counter < str_len)

where does str_len or counter get assigned a value before that line.
Most likely str_len contains the value "thisisatestthisisatest" instead of a number
 

jbug

Member
You might want to post the code that is causing the error.

Line 24 of the Draw event for object oTextbox: if (counter < str_len)

where does str_len or counter get assigned a value before that line.
Most likely str_len contains the value "thisisatestthisisatest" instead of a number
GML:
function srStringwrap()
{
    var str = argument0;
    var max_width = argument1;

    var str_len = string_length(str);
    var last_space = 1;

    var count = 1;
    var substr;

    repeat(str_len)
    {
        substr = string_copy(str, 1, count);
        if (string_char_at(str, count) == " ") last_space = count;
    
        if (string_width(substr) > max_width)
        {
            str = string_insert("\n", str, last_space);
            count += 1;
        }
    
        count++;
    }

    return str;
}
here is where i established the variables
 

Roldy

Member
GML:
function srStringwrap()
{
    var str = argument0;
    var max_width = argument1;

    var str_len = string_length(str);
    var last_space = 1;

    var count = 1;
    var substr;

    repeat(str_len)
    {
        substr = string_copy(str, 1, count);
        if (string_char_at(str, count) == " ") last_space = count;
  
        if (string_width(substr) > max_width)
        {
            str = string_insert("\n", str, last_space);
            count += 1;
        }
  
        count++;
    }

    return str;
}
here is where i established the variables

This is a function srStringwrap. str_len is a local variable of this function and is out of scope once the function exits.

How does this relate to the code posted in the OP? Where is str_len defined or assigned a value in the scope of the Draw event for oTextBox?
 

Nidoking

Member
I don't see you establishing any variables. That's a function with local variables. How about the part where you put a string into either counter or str_len?
 

jbug

Member
This is a function srStringwrap. str_len is a local variable of this function and is out of scope once the function exits.

How does this relate to the code posted in the OP? Where is str_len defined or assigned a value in the scope of the Draw event for oTextBox?
that is the only place i put that variable. do i need to put in that Draw event or could i put it in the Create i have for the same object?
 

flyinian

Member
If it helps,

You can't do mathematical calculations with text. A.K.A. strings. However, you can get the length of the desired string through a variable separate from the actual string variable and this should allow it to calculate the string length.

GML:
_String = Hello;

string_length(_String); // This will have a number value of 5. 1 for each character of "Hello"

// Now you can assign the string_length to a variable of your choosing and do math with it.



that is the only place i put that variable. do i need to put in that Draw event or could i put it in the Create i have for the same object?
Ideally the Create event.

As mentioned above, you'll want to make sure that the variable exists when its used. If its only a local variable in a script it'll give your errors if you are trying to use said local variable from the script itself in an object.

You can use global variables for practice and understanding how this works. Using global variables shouldn't matter where they are at. I recommend not using global variables permanently for the practice/understanding part of this though.

Manual Links:

STRINGS
VARIABLES AND VARIABLE SCOPE
 
Last edited:

Roldy

Member
that is the only place i put that variable. do i need to put in that Draw event or could i put it in the Create i have for the same object?
Press ctrl + Shift + F and do a find in all files for "str_len" somewhere it is getting erroneously assigned the value of your string. I would recommend solving that issue.

But if you just want to see if you can do a quick hacky fix then just assign str_len right before your code in the draw event.

GML:
//Draw Text

str_len = string_length(text_wrapped);

if(counter < str_len)
{
    counter++;
}
var substr = string_copy(text_wrapped, 1, counter);
c = text_col;
draw_text_ext_color(text_x, text_y, substr, text_height, text_max_width, c,c,c,c, 1);
Additionally I would recommend reading the manual about variables and scope:
 

jbug

Member
Ideally the Create event.

As mentioned above, you'll want to make sure that the variable exists when its used. If its only a local variable in a script it'll give your errors if you are trying to use said local variable from the script itself in an object.
ok but does that mean i would need to put every variable i mentioned in that same Create event? i feel like there would be an easier way to establish stuff like that...
 

flyinian

Member
ok but does that mean i would need to put every variable i mentioned in that same Create event? i feel like there would be an easier way to establish stuff like that...
You can but, it also depends on what you are trying to do and the scope.

You could post all the code that you are working with that relates to your string drawing and we could take a look and provide better and more accurate feedback.
 

jbug

Member
Press ctrl + Shift + F and do a find in all files for "str_len" somewhere it is getting erroneously assigned the value of your string. I would recommend solving that issue.

But if you just want to see if you can do a quick hacky fix then just assign str_len right before your code in the draw event.

GML:
//Draw Text

str_len = string_length(text_wrapped);

if(counter < str_len)
{
    counter++;
}
var substr = string_copy(text_wrapped, 1, counter);
c = text_col;
draw_text_ext_color(text_x, text_y, substr, text_height, text_max_width, c,c,c,c, 1);
Additionally I would recommend reading the manual about variables and scope:
thank you, that worked perfectly :) and ill check out that link because im still not quite understanding how the variables work. do you maybe have a video that could explain because i dont really do all well with written stuff like that?
 

jbug

Member
You can but, it also depends on what you are trying to do and the scope.

You could post all the code that you are working with that relates to your string drawing and we could take a look and provide better and more accurate feedback.
thank you for the help! Roldy told me to just assign that variable before the code, instead of just in my script. It worked, but im not really understanding why i would have to assign the variable again.
 

Roldy

Member
thank you, that worked perfectly :) and ill check out that link because im still not quite understanding how the variables work. do you maybe have a video that could explain because i dont really do all well with written stuff like that?

I am going to rewrite what you just said, instead of:
i dont really do all well with written stuff like that
You really mean:

I am not yet comfortable with written documentation.
This is a great opportunity to get more comfortable and used to using the documentation. It is imperative that you become proficient in using the documentation if you want an easier time to succeed. Programming = reading and writing.

But here is a video. I have never watched it. I am sure you can find more videos via google or youtube.

 

jbug

Member
I am going to rewrite what you just said, instead of:


You really mean:



This is a great opportunity to get more comfortable and used to using the documentation. It is imperative that you become proficient in using the documentation if you want an easier time to succeed. Programming = reading and writing.

But here is a video. I have never watched it. I am sure you can find more videos via google or youtube.

thank you, i really appreciate your help.
 
Top