[SOLVED] Script portativiness - lost ? or i miss something ?

jc-GUGI

Member
Everything is in the panel. Feel free to give an astuce about.

GML:
/*
/// the string replacer project _ shared code + structure reconstitution
// Hi there, i'm blocked into a bug ? i share my code for looking at it
// and talk eventualy about code portativiness. :/
// To the follow, the shared code of my thread.

//////_Font
// police name : Arial
// police size : 22


//////_Create_event
x=100;y=160;image_index=0;image_speed=0;
cc=$120630;    pp=Arial_22; pc=$ABFF09;
mx=x;        my=y; //panel_position
rx=160;        ry=460; //panel_dimension
tx=10;        ty=10; //text_offset
zz=rx-ceil(tx/2)-1; //text_autobck

txt_jiki = "ugbggrggeqeffg"
// this variable is what it is searhed to be return by the functions
// BUT, as a programmer i try to work with script wich variables
// recorvery and manipulation are by arguments only.
// You will see later on what i mean (cf. r01m_mbleft_txtcleaner)


//////_Step_event
r01m_mbleft_txtcleaner(mx,my,rx,ry,mb_left,txt_jiki)

//////_Draw_event
r01m_sauto_txtupanel(pc,cc,pp,mx,my,rx,ry,tx,ty,txt_jiki)
*/


/*
//////_Script gms_2 2.3.x ----- the string replacer script _ labotest
// To the follow : 2 functions
// the interesting one is the mbleft_txtcleaner for the room 01
// what i try to do when i code script is to conceive them
// with no functions external variable at all in it, so i'm block
// to the returning of f (cf. f is txt_jiki ***into the script***)
// I don't know if it's a bug, or what to do.
// it's play between txt_jiki and f
// at the end of the function i want to recorver f and not txt_jiki
// so txt_jiki=f for returning txt_jiki is a program-scripter failure.
// if someone knows how to return f without by passing by txt_jiki=f
// you'r welcome.
*/
function r01m_mbleft_txtcleaner(a,b,c,d,e,f){
if mouse_x>a && mouse_x<a+c
&& mouse_y>b && mouse_y<b+d {
if mouse_check_button_pressed(e) {
if (string_count("g", f) >0) {while (string_count("g", f) !=0) {f=string_replace(f,"g","zz")}}

//function_external variable ....... as doomer
txt_jiki=f;

//function_external variable ...... as tester
cc=choose(c_dkgray,c_teal,c_maroon,c_purple,$04B4C8);
}}}


function r01m_sauto_txtupanel(a,b,c,d,e,f,g,h,i,j){
draw_set_alpha(1.0);
up=d+f; uq=e+g; draw_set_color(b); draw_rectangle(d,e,up,uq,0);
tp=d+h; tq=e+i; draw_set_color(a); draw_set_font(c); draw_text(tp, tq,j);
}
*on saiL* ⛵
 

Nidoking

Member
I don't think I understand your auto-translation very well, but why can't you return f like this:

GML:
return f;
 

Nidoking

Member
I've tried return plenty of times. It works just fine for me. If you want solutions, you will need to explain the problem.
 

jc-GUGI

Member
As i've said, the *return* balisa doens't work ... I read somewhere than it's because *return* get back a real, and in my case, i need to return a string.
but return string(f) doens't work either. :/
 

Nidoking

Member
return returns what you tell it to return. When you return f (not string(f), but just f), what value does it give you, and how is that different from what you want?
 

jc-GUGI

Member
(i) have to pinpoint the doomer. :/

txt_jiki = f ... simulate what it would do if it works fine

but i want to jerk off the txt_jiki

no external variable _ script is what i search for when i code

and yes, you'r right about returns f
but like i said : *return* balisa doesn't work in my present code,
and i don't know why :/ (i tried without success)

The difference between returning the result of an inner scricpt-variable and not the result on a no-inner script-variable
is the gain of universalim aplication, hight-quality clean-codes, and dynamic repeatitiness throught any object met :
- portativiness 🧙‍♂️

the function i try to make if i use the *return* balisa. And it doesn't work.
GML:
function r01m_mbleft_txtcleaner(a,b,c,d,e,f){
if mouse_x>a && mouse_x<a+c
&& mouse_y>b && mouse_y<b+d {
if mouse_check_button_pressed(e) {
if (string_count("g", f) >0) {while (string_count("g", f) !=0) {f=string_replace(f,"g","zz")}}

//(cf. doomer)
return f;

//

}}}
 

Nidoking

Member
*return* balisa
What is "balisa"? I don't see that anywhere in your code, and I don't know the word. You keep saying it "doesn't work", but what does that MEAN? What is it doing or not doing? What is the behavior of it that you want changed?

The difference between returning the result of an inner scricpt-variable and not the result on a no-inner script-variable
is the gain of universalim aplication, hight-quality clean-codes, and dynamic repeatitiness throught any object met :
- portativiness
I don't think this would make sense if it were in English, wizard emoji or no. You can return anything. It's return-by-value. You return a copy of the variable, so the scope doesn't matter.

Hang on... are you trying to manipulate the variables that were passed in? Are you trying to pass a variable as the f parameter and magically give it a new value when the function returns? No, the wizard emoji is right - that's magic, not GML. You can't do that easily and I don't recommend doing it at all.
 

jc-GUGI

Member
For being sure about what i'm talking about with you.
Here is the situation about the object
before clicking : ugbggrggeqeffg
after clicking : uzzbzzzzrzzzzeqeffzz

everything pass by the script trought the functions.

Here is the r01 mbleft_txtcleaner functions
this one with txt_jiki=f
GML:
function r01m_mbleft_txtcleaner(a,b,c,d,e,f){
if mouse_x>a && mouse_x<a+c
&& mouse_y>b && mouse_y<b+d {
if mouse_check_button_pressed(e) {
if (string_count("g", f) >0) {while (string_count("g", f) !=0) {f=string_replace(f,"g","zz")}}
txt_jiki=f;
}}}
and this one with the *return* balisa modification
GML:
function r01m_mbleft_txtcleaner(a,b,c,d,e,f){
if mouse_x>a && mouse_x<a+c
&& mouse_y>b && mouse_y<b+d {
if mouse_check_button_pressed(e) {
if (string_count("g", f) >0) {while (string_count("g", f) !=0) {f=string_replace(f,"g","zz")}}
return f;
}}}
the first version of the function gives me uzzbzzzzrzzzzeqeffzz
it's normal because it passes by txt_jiki, but it's not what i want

the second version of the function gives me ugbggrggeqeffg
it does not work ... txt_jiki stays with its old value.

Do i bug ?
 
I just realized why your "return" isn't working, and it was hidden away in the commented text. "return" does NOT change the inserted variable, it just gives back a value. If you don't actually store the result, then nothing will change. The reason why using "txt_jiki = f" works is because you're changing the actual variable itself, but "return f" isn't working because you aren't actually using the value of f outside of the script to change that variable. It's not enough to call the function. Think of it like writing down a number on a piece of paper and asking your friend to change it. Your friend could verbally say a different number, but the one on the paper will obviously not miraculously change - he has to actually write down the new number for your paper to be different. The Step event has to be as such:

txt_jiki = r01m_mbleft_txtcleaner();

Putting a variable into a function doesn't insert the actual variable, just the value it holds. "f" may hold the string that "txt_jiki" does, but when you edit "f", you are only editing that variable; it's not a pointer to "txt_jiki". That's why you have to return it into the original variable.
 

jc-GUGI

Member
//step_event
txt_jiki = r01m_mbleft_txtcleaner(mx,my,rx,ry,mb_left,txt_jiki)

it gives me *undefined*
 
Last edited:

jc-GUGI

Member
erf ... it seems to me than In the gms 1.4 version, it was enable to recorver any script without any external variables called
just the work of arguments :/
scripts worked like dynamic variables combination ...
have to work on it again.

u_u the f=string_replace should be enough but ... tsssss
 
Last edited:
"f = string_replace()" is enough, but you're doing more with it :p I personally would change the function to only handle the string change and perform the mouse check elsewhere, but if you move the "return f" to the very bottom of the function, it'll return the original string if it hasn't been edited yet, making for a simple solution.
 

jc-GUGI

Member
i believe yoyogames have broken their own IDE

if i can't make a script without external variables ... then game maker studio 2 with the updates,
the scripts are as good as they were just a common other_event : that's not what i call *script*, scripts are not purposed to "copy-style" java class logic.

As it is now, i believe i don't make any mistake, just a lose in IDE quality for some java afficionados. :/ (that's what i believe)
A great lost if i can't overcome it.
 
Last edited:

chamaeleon

Member
There is no such thing as an "external variable". You have local, instance, and global variables, just like before. Content that used to be in a script should now be placed within a function body, and given that the definition of local variables applies exactly the same way as inside the script in the older version. Instance and global variables are is course the same.
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
The problem is not with GMS it is with your understanding of how GMS works. Your understanding and implementation is flawed, and @BattleRifle BR55 has already given you the correct solution. Modify the function to this:

GML:
function r01m_mbleft_txtcleaner(a,b,c,d,e,f)
{
if (mouse_x > a) && (mouse_x < a + c) && (mouse_y > b) && (mouse_y < b + d)
    {
    if mouse_check_button_pressed(e)
        {
        if (string_count("g", f) > 0)
            {
            while (string_count("g", f) != 0)
                {
                f = string_replace(f,"g","zz")
                }
            }
        }
    }
return f;
}
(I have tidied up the function code to make it far clearer what is going on)

Then call the function like this:
GML:
txt_jiki = r01m_mbleft_txtcleaner(mx, my, rx, ry, mb_left, txt_jiki);
The variable "txt_jiki" will now always contain a string, and this string will only ever be modified if the mouse is pressed and the other conditions are met. Tbh, I do think that @BattleRifle BR55 is also correct in suggesting you move the mouse checks out of the function and have the function simply perform the change, but that's up to you.

Now, please don't blame GMS2 for this. It has it's flaws and bugs like any software, but this is not one of them. ;)


PS: That string_replace loop can be removed if you use the function string_replace_all(); :)
 

jc-GUGI

Member
Thanks for answering ... i have looked deeply in manual and some tuto here and there.
There wasn't any clue about [this return and varia=funct] process. So your answer is godly helping hand xD thanks👏.

Sorry for the badmouthing about the studio. I study hard, it's not easy everyday.

.... 2 more things
> i don't know very much about gabarit coding ... (like when python, when c, how to c, how to ruby, how to dll, in short term : gms processes)
surely my lexical isn't rich enough for the moment,

> i don't how you conceive an external if statement clicking. :/
i know how to mouse_zone and in it input others codes or functions ; but doing it outsidely of inner process ? nope ( ? a barbaric coding ?)
//===============
That's why i know i have flaw in my understanding things.
Oh, and that's my first serious prog on gms_2 ; i code on gms_1 100% usually
in gms_2, to not be able to overlay the windows is a real pain (even if i can columns or neighboors the windows,
it's still slower than being able to overlay the windows)
 
Last edited:

Nidoking

Member
i know how to mouse_zone and in it input others codes or functions ; but doing it outsidely of inner process ? nope ( ? a barbaric coding ?)
Like a bad TV commercial, in mere seconds, you can go from this:

GML:
function doAThingIf()
{
  if (condition)
  {
    doStuff();
  }
}

doAThingIf();
to this:

GML:
function doAThingAlways()
{
  doStuff();
}

if (condition)
{
  doAThingAlways();
}
If you can't understand what I've done there, then yes, you need to do more studying and understand how programming works. There's an entire page in the Manual about the return keyword, and I know there are pretty extensive writeups of functions because it was a new feature in GM 2.3, and return also worked in scripts in earlier versions, and there's a sticky thread in this very forum that will show you how to go from one to the other.
 

jc-GUGI

Member
i have some issue with the "/r/n" or "/r" or "/n" cases
with a text field begining by a txt_jiki in this profile type

in the create_event
txt_jiki = @"blabla

bla

blablabla



blabla"

//==============
i made it worked once, but after recleaning my code, i droped it off u_u ... (such unlucky).
I don't use your method about function-externalisation because i try to isolate mouse_click in many aspects
by the mouse clicking only. A step-by-step approach in my tastelike.
//==============
something like
if (string_count("/r/n", f) != 0) { while (string_count("/r/n", f) != 0) { f=string_replace(f,"/r/n","") };};
 

jc-GUGI

Member
a new problem appears now, i don't want to chain them, just erasing the blank lines ... "\r" or "\n" only don't work. :/
 

TsukaYuriko

☄️
Forum Staff
Moderator
Think about what a blank line really is for a moment from the perspective of the character sequences behind it. If the newline character sequence is what separates a line from another line, what would cause a line to be blank?
 

jc-GUGI

Member
in french, we call it a *carriage return* and the carriage is after the last character of the (antecedente) line.
in my case, it would be for any line of any length (the all in a single text drop).
//==============
the logic is there, i guess i have to check the [switch] balisa scenario ... for end of the txt or followed by a blank line.
i have lots of of coding lines to compare as integrity (with the jerk off of some pollutors as /* /***** ////// etc.)

my final project, would be a counting prog + a redrawing coding behind for nomenclaturing my migration to 2.3 gms
 
Last edited:

TsukaYuriko

☄️
Forum Staff
Moderator
My point was that a blank line is nothing other than a newline character sequence followed by another newline character sequence. ;)
 

jc-GUGI

Member
As i understand things right now \r (for *carriage return*) and \n (for *keyboard enter*) are interpretated the same way (wich for typewriting isn't the same issue in the journalistic world)

f=string_replace_all(f, "\r\n", "\r");

should be enough, and that's not the case. hum hum :3
 
Last edited:

TsukaYuriko

☄️
Forum Staff
Moderator
Carriage Return (CR, 0x0D, \r) and Line Feed (LF, 0x0A, \n) are not the same. What exactly qualifies as a "newline character sequence" differs between different platforms, with anything touched by Microsoft generally using CRLF (\r\n) while Unix-based systems only use LF (\n), hence my usage of the term "newline character sequence" rather than "CRLF" or "LF".

The code you posted replaces CRLF with just CR, which doesn't lead to the desired result on either group of OSes.
If you change this so that it replaces CRLF with an empty string, it will put everything on a single line.

On the other hand, if you change it so that it replaces CRLFCRLF (two newlines) with CRLF (one newline), that'll get rid of (single) empty lines while preserving line breaks.

If you want it to also remove multiple empty lines in a row, run it in a loop until the input equals the output.
 

jc-GUGI

Member
erf, i guess the code must be complexified by a [for] loop decortication ...

like i said : f=string_replace_all(f, "\r\n", "\r");

should be enough for removing all empty lines
and leting the structure of the code blanked-line less ...
i will have to do it line by line :/

i tried all CR LF 0x0D 0x0A combination (with low and upper case sensisitive), nothing is passing
it's like the "\r" is coming with an auto LF with it.
 

TsukaYuriko

☄️
Forum Staff
Moderator
like i said : f=string_replace_all(f, "\r\n", "\r");

should be enough for removing all empty lines
It shouldn't be enough and, in fact, it isn't enough. As I said before, all you're doing here is replacing CRLF with just CR, which doesn't have the effect of removing empty lines. All this does is turn all newline character sequences into the first half of the newline character sequence, but the underlying engine or OS is smart enough to fill the blanks, ˢtͦwͬoͭ lͦᶠiˡnͥᵏeͤs ͪbͦʷeʸiͦnͧg ͨmͣᶰerͬgͤeͣdͩ iͭnͪtͥˢo ͩeͤˢaᵖcͥhͭ ͤother.


You are currently replacing CRLF (newline character sequence) with CR (something that doesn't make sense by itself). What I instructed you to do is replacing CRLFCRLF (two newline character sequences) with CRLF (one newline character sequence).
 

FrostyCat

Redemption Seeker
This is what TsukaYuriko meant:
GML:
f = string_replace_all(f, "\r\n\r\n", "\r\n");
Note that it only replaces all double empty lines with single empty lines once. It will not work on triple or more empty lines. The while loop from your original code is still needed for that.
GML:
while (string_count("\r\n\r\n", f) != 0)
{
    f = string_replace_all(f, "\r\n\r\n", "\r\n");
}
 

jc-GUGI

Member
i understood the frenzy code just before going to sleep, omg that was hard ...
GML:
function r01m_mbleft_txtcleaner(a,b,c,d,e,f) {
if (mouse_x > a) && (mouse_x < a + c) && (mouse_y > b) && (mouse_y < b + d) {
    if mouse_check_button_pressed(e) {
        if (string_count("\r\n\r\n", f) != 0) {
        while (string_count("\r\n\r\n", f) != 0) {
        f=string_replace_all(f, "\r\n\r\n", "\r\n");
}}}
return f;
}
gmL is really an awesome language for coders like me.
 

chamaeleon

Member
i understood the frenzy code just before going to sleep, omg that was hard ...
GML:
function r01m_mbleft_txtcleaner(a,b,c,d,e,f) {
if (mouse_x > a) && (mouse_x < a + c) && (mouse_y > b) && (mouse_y < b + d) {
    if mouse_check_button_pressed(e) {
        if (string_count("\r\n\r\n", f) != 0) {
        while (string_count("\r\n\r\n", f) != 0) {
        f=string_replace_all(f, "\r\n\r\n", "\r\n");
}}}
return f;
}
gmL is really an awesome language for coders like me.
You don't need the if statement test before the while loop as they have the same condition. If there is no sequence found the while loop won't do anything, just like a failed if test.
 
Top