Jump to content


Photo

simple "Keyboard_string" script!


  • Please log in to reply
12 replies to this topic

#1 killerheath

killerheath

    GMC Member

  • GMC Member
  • 209 posts
  • Version:GM8

Posted 16 December 2011 - 11:15 AM

This script is basically "keyboard_string" but the string doesnt get removed when you minimize the window.
i have also included a text limiter into the system.

//---Scr_kstringget(Width_Of_String)---//
//---GML Script made by, Kyanite Entertainment---//
{
if (!variable_local_exists("kstring"))
    kstring="";
if keyboard_string!=""
    if string_width(keyboard_string)<argument0
        kstring=keyboard_string;
    else
        keyboard_string=kstring;
else
    if string_length(kstring)<2
        kstring=keyboard_string;
    else
        keyboard_string=kstring;
return kstring;
}
Now for the draw event, remember to include the first argument of this script, otherwise it will act as if the limit is 0, and nothing will be typed.
//---Draw Event---//
draw_text(x,y,Scr_kstringget(90));//draws the script with a text width limit of 90 pixels.

"treat unitialized variables as 0" does not need to be true.
and as far as i know, this script is working!

Edited by killerheath, 19 December 2011 - 03:51 AM.

  • 0

#2 paul23

paul23

    GMC Member

  • Global Moderators
  • 3388 posts
  • Version:GM8

Posted 16 December 2011 - 01:01 PM

Uhm this won't work. Unless you have treat unitialized variables as 0.

Look here:
var kstring;
if keyboard_string!=""
    if string_width(keyboard_string)<argument0
        kstring=keyboard_string;
    else
        keyboard_string=kstring;

If keyboard_string is wider than argument0, the last statement will be executed. Keyboard string will be setted to kstring. However kstring is never set itself, so to what should keyboard_string be set? The result is not clear.
  • 0

#3 killerheath

killerheath

    GMC Member

  • GMC Member
  • 209 posts
  • Version:GM8

Posted 16 December 2011 - 01:48 PM

Lol, believe what you want, im using this in my online game right now, works with everyone so far :)

Let me ask you this, did you even try it before saying it doesnt work?? :) oh and i have not ticked "treat unitialized variables as 0." so yeah -.-

i dont really get what your saying, but ill try to answer your question :S

keyboard_string=kstring;

at the start of this code, kstring is set to keyboard_string, because it will start off smaller then argument0, therefore when we set keyboard_string to kstring, it basically just keeps it the same the whole time, until something changes :S

Edited by killerheath, 16 December 2011 - 01:54 PM.

  • 0

#4 IceMetalPunk

IceMetalPunk

    InfiniteIMPerfection

  • Retired Staff
  • 9314 posts
  • Version:Unknown

Posted 16 December 2011 - 11:32 PM

keyboard_string=kstring;

at the start of this code, kstring is set to keyboard_string, because it will start off smaller then argument0, therefore when we set keyboard_string to kstring, it basically just keeps it the same the whole time, until something changes :S

Which makes sense...until you realize you put var kstring at the beginning of the script. So when the script ends, kstring is discarded (cleared) and the next time the script is called, it's empty again.

I'm assuming you're using this in your game without the var kstring line?

-IMP
  • 0

#5 killerheath

killerheath

    GMC Member

  • GMC Member
  • 209 posts
  • Version:GM8

Posted 17 December 2011 - 12:29 AM

keyboard_string=kstring;

at the start of this code, kstring is set to keyboard_string, because it will start off smaller then argument0, therefore when we set keyboard_string to kstring, it basically just keeps it the same the whole time, until something changes :S

Which makes sense...until you realize you put var kstring at the beginning of the script. So when the script ends, kstring is discarded (cleared) and the next time the script is called, it's empty again.

I'm assuming you're using this in your game without the var kstring line?

-IMP


Yeahp, your right, thanks, ill remove it :P

Oh and paul, i did get the error when i tried to check the limiter :3

Edited by killerheath, 17 December 2011 - 12:30 AM.

  • 0

#6 paul23

paul23

    GMC Member

  • Global Moderators
  • 3388 posts
  • Version:GM8

Posted 17 December 2011 - 01:07 AM

Now it can fail if keyboard_string is larger than argument0 when the game starts (this script is called for the first time).

Simply the whole idea of not initializing variables properly is something I'm looking down on very much. It always leads to bugs.


Also you now occupy one of the instance namespace-variable names, kstring. If another engine uses kstring for another purpose everything will break.
  • 0

#7 killerheath

killerheath

    GMC Member

  • GMC Member
  • 209 posts
  • Version:GM8

Posted 17 December 2011 - 01:12 AM

Now it can fail if keyboard_string is larger than argument0 when the game starts (this script is called for the first time).

Simply the whole idea of not initializing variables properly is something I'm looking down on very much. It always leads to bugs.


Also you now occupy one of the instance namespace-variable names, kstring. If another engine uses kstring for another purpose everything will break.


If someone is going to use this script, they are not going to set the limit of the ammount of text to smaller then 1 ??? :S

Well then they can edit it :S

[Edit]
I tried using
draw_text(x,y,Scr_kstringget(0));
inside my draw event, with this exact script, and no errors came?? i also tried setting keyboard_string to a larger amount of text before calling the script, still, no errors :)

Edited by killerheath, 17 December 2011 - 01:23 AM.

  • 0

#8 torigara

torigara

    GMC Member

  • GMC Member
  • 6487 posts

Posted 17 December 2011 - 05:14 AM

I also wonder what this part is for.

else
    if string_length(argument0)<2

argument0 is supposed to be a number (width of string), right? When you put a number where a string is expected, it is treated as an empty string "" (as far as 8.1; this behavior isn't documented and not guaranteed to work longer.) Isn't it equivalent to the following one?
else
    if string_length("")<2
Which in turn:
else
    if 0<2

If you wanted to execute a statement always, there was no need to invent such a roundabout way nor sticking unnecessary else part. You could just write:
...
else
    kstring = ""; // since keyboard_string is "" when this part is executed.
return kstring;

Edited by torigara, 17 December 2011 - 05:37 AM.

  • 0

#9 killerheath

killerheath

    GMC Member

  • GMC Member
  • 209 posts
  • Version:GM8

Posted 17 December 2011 - 11:02 AM

I also wonder what this part is for.


else
    if string_length(argument0)<2

argument0 is supposed to be a number (width of string), right? When you put a number where a string is expected, it is treated as an empty string "" (as far as 8.1; this behavior isn't documented and not guaranteed to work longer.) Isn't it equivalent to the following one?
else
    if string_length("")<2
Which in turn:
else
    if 0<2

If you wanted to execute a statement always, there was no need to invent such a roundabout way nor sticking unnecessary else part. You could just write:
...
else
    kstring = ""; // since keyboard_string is "" when this part is executed.
return kstring;



Whoops, thats meant to be "atext" not "argument0" thanks for that XD
Well its there because, if the text is small then 2, you cant backspace it properly, so yeah :)
  • 0

#10 IceMetalPunk

IceMetalPunk

    InfiniteIMPerfection

  • Retired Staff
  • 9314 posts
  • Version:Unknown

Posted 17 December 2011 - 08:52 PM


I also wonder what this part is for.


else
    if string_length(argument0)<2

argument0 is supposed to be a number (width of string), right? When you put a number where a string is expected, it is treated as an empty string "" (as far as 8.1; this behavior isn't documented and not guaranteed to work longer.) Isn't it equivalent to the following one?
else
    if string_length("")<2
Which in turn:
else
    if 0<2

If you wanted to execute a statement always, there was no need to invent such a roundabout way nor sticking unnecessary else part. You could just write:
...
else
    kstring = ""; // since keyboard_string is "" when this part is executed.
return kstring;



Whoops, thats meant to be "atext" not "argument0" thanks for that XD
Well its there because, if the text is small then 2, you cant backspace it properly, so yeah :)

Which brings us to another issue. What's atext? It's not in the code except when being checked, never when being set.

-IMP
  • 0

#11 killerheath

killerheath

    GMC Member

  • GMC Member
  • 209 posts
  • Version:GM8

Posted 18 December 2011 - 02:16 PM



I also wonder what this part is for.


else
    if string_length(argument0)<2

argument0 is supposed to be a number (width of string), right? When you put a number where a string is expected, it is treated as an empty string "" (as far as 8.1; this behavior isn't documented and not guaranteed to work longer.) Isn't it equivalent to the following one?
else
    if string_length("")<2
Which in turn:
else
    if 0<2

If you wanted to execute a statement always, there was no need to invent such a roundabout way nor sticking unnecessary else part. You could just write:
...
else
    kstring = ""; // since keyboard_string is "" when this part is executed.
return kstring;



Whoops, thats meant to be "atext" not "argument0" thanks for that XD
Well its there because, if the text is small then 2, you cant backspace it properly, so yeah :)

Which brings us to another issue. What's atext? It's not in the code except when being checked, never when being set.

-IMP

What? it gets set at the start??? -.-
kstring is what keeps Keyboard_string inside it.
  • 0

#12 torigara

torigara

    GMC Member

  • GMC Member
  • 6487 posts

Posted 19 December 2011 - 01:14 AM

What? it gets set at the start??? -.-

This is the section for completed GML scripts. Scripts posted here are supposed to work independently, so you're advised to test your script with a new test environment to make sure it works without depending on other part of your own game.

Create a new game, copy your script (make sure it is the same code you're going to post), create a new object and copy the test code in its draw event (adding one missing closing paren.) Put the object in a room, run it and guess what you see:

In script Scr_kstringget:
Error in code at line 10:
if string_length(atext)<2
^
at position 23: Unknown variable atext

If the script requires some settings (e.g. initializing variables in the calling object) you should include instructions for it.
  • 0

#13 killerheath

killerheath

    GMC Member

  • GMC Member
  • 209 posts
  • Version:GM8

Posted 19 December 2011 - 03:44 AM

Sorry, i have remade the script, and it works perfectly now.. unless you are using the kstring variable somewhere else :P

anyways, its complete now :)

Edited by killerheath, 19 December 2011 - 03:51 AM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users