how to subtract specific string from a word just by pressing on a sprite?

I have a sprite with the letter T, and when I press it, I configured it so that the letter “T” appears within a text input. I want to learn how to do it so that when I click on the letter T again, it is deleted, only the letter T, and no others, pressing the sprite AGAIN. It would be like an ON/OFF, pressed, it appeared, pressed, deleted only the letter T. The occurrence would only be for the letter T, if there were other letters, it would still only delete T, and if I typed it again, it would appear last in the word.

Would you have multiple T in your text?
Would T ALWAYS be last character?

Response: T ALWAYS be last character

So if there were 2 sprites, one for “T” and another for “O”, then pressing the T, O and then T sprites it would result in “TOT”?

But if I pressed the O, T, and then T sprites it would result in just “O”?

Take a look here cause there are A LOT of useful expressions you can use

https://wiki.gdevelop.io/gdevelop5/all-features/expressions-reference

Anyway there is expression called
StrFind(string, string)
Which will check if whatever text you search is in text in which you search

And will return a NUMBER -1 if not found or a position like 0 1 2 3 4 5 … and so go on where such text was found

There is also condition called Compare Two Numbers
So you can use it to do

StrFind(MyTextObjectHere, T) is equal to -1
Which will mean there is no T in MyTextObject

And in action you can add T however you add it

Next
StrFind(MyTextObject, T) is NOT equal to -1
Will mean there is T in your text
So to it add action to

Change text of MyTextObject set to SubStr(MyTextObject, 0, StrLength(MyhTextObject)-1)

SubStr() works like SubStr(What text to use? , From what position start your text , What portion of text to take?)

So imagine we have word ANCLEIWER
And we want to cut out CLEI from it
SO we go SubStr(ANCLEIWER, 2, 4)
2 instead of 3 cause starting position is 0
And we want 4 characters so we type 4 in last value and we would get CLEI
And what i suggest you to do above will simply do
StrLength() count how many characters word have
So if we do that -1 we will always get ONE less character than word have
So if we again got ANCLEIWER and we go StrLength(ANCLEIWER)-1
We gonna get ANCLEIWE
So that is how you get rid of your T

2 Likes

Exactly that, that’s the logic! @MrMen

If you want to check if its last character or not
Then on top what i wrote you need to go with compare two numbers and
StrFindLast(MyTextObject,T) is equal to StrLength(MyTextObject)
Or it will be

StrFindLast(MyTextObject,T) is equal to StrLength(MyTextObject)-1

I always get confused which is correct
Anyway it will check if last is T or not

2 Likes

WHAT IF MY WORD IS “TREM”, OR “ETRM”, OR “MRTE” AND I WANT TO REMOVE ONLY THE LETTER “T” AND LEAVE THE OTHERS? HOW WOULD I MAKE THE SYSTEM RECOGNIZE THAT I ONLY WANT TO REMOVE ONLY T AND LEAVE THE OTHER LETTERS? @ZeroX4

WHAT IF MY WORD IS “TREM”, OR “ETRM”, OR “MRTE” AND I WANT TO REMOVE ONLY THE LETTER “T” AND LEAVE THE OTHERS? HOW WOULD I MAKE THE SYSTEM RECOGNIZE THAT I ONLY WANT TO REMOVE ONLY T AND LEAVE THE OTHER LETTERS?

ETRM

SubStr(ETRM,0,StrFind(ETRM,T)) + SubStr(ETRM,StrFind(ETRM,T),StrLength(ETRM)-StrFind(ETRM,T)-1)

And we get ERM

BLASTER

SubStr(BLASTER,0,StrFind(BLASTER,T)) + SubStr(BLASTER,StrFind(BLASTER,T),StrLength(BLASTER)-StrFind(BLASTER,T)-1)

And we get BLASER


ERROR

In this case, would this formula only be for 1 specific word? Wouldn’t there be a method that applied the same formula to any combination of the letters TREM and removed the string “T” regardless of the position?

In gdevelop expressions returns either text or number
If you make number variable and set it to 2 and another number var and set it to 3
And now you will want to add them and print them to text you would change text SET TO Var1+Var2 and you would get text object displaying 5
Because Var1 was set to 2 and Var2 was set to 3 and 2+3 = 5

Now if you would do exactly the same BUT your variables would be text instead of number but you still set Var1 to 2 and Var2 to 3
And you would go Var1+Var2 you would get 23
Because text is adding itself like lego blocks and not making mathematical calculations

I explaining it to you cause it will help you understand what is going on

Now EVEYRYTHING in text object NEEDS TO BE TEXT
Meaning there are no numbers
Think of it like you have this text in this very message of mine
You can select it and copy it and then paste it into your message and edit it
image

You cannot select it you cannot copy it and then paste it into ANYTHING that would allow you to edit it like any other text

You would need image editor like even windows paint or aseprite to edit it

Cause even so for you and me its still just a text
For our devices this is an image not a actual text

That is the difference in gdevelop between number value and text value

Armed with that knowledge
Now think Var1+Var2 can if Var1 is set to 2 and Var2 is set to 3 can give result of 5
Cause calculation is made before its converted text
When you have number and you want to print it to text object
Most of the times you need to use ToString(Number here)
And your number is converted to plain text
That is how it works

Now what if you want to print to text object your player X position?

You would go with ToString(Player.X())
And your text object would show X pos of your player
But wait you did not specify there number so how so?
Cause like i said expressions returns either text or number
And that number value would change accordingly with your player moving

SOOOOOOOOO now think hard
If you can use expression where number is expected to show some number dynamically and it would update itself meaning if number change you would see different number
SO if player was at X 517 and moved one pixel right it would show 518

And magic of it is that you DID NOT type 517 or 517 there
You could but you did not you used expression to return number there

So now think if that is possible to do with numbers
How about putting expression that return text in place of actual text
Where text is expected

And believe or not variables work same exact way
So imagine you have number variable to which you add 1 each time you click LMB
So you would print to text it by change text SET TO Var1
And you would see how many times you clicked

Now what if your variable would be text variable?
It would work EXACT SAME WAY

SOOOOOOOOO ANYWHERE you see in gdevelop field to input number you can put expression or variable name that returns number

AND anywhere you see field to input text you can use expression or variable that returns text

Imagine you made variable called Var1 a text variable and set it to Ancleiwer

So now you can go with SubStr(Var1,0,2) and it would return An
Cause your var one is set to Ancleiwer and in SubStr we wanted to start from position 0 of that text and get 2 characters from it

SubStr(Var1,1,4) would return ncle
Now you get how SubStr() work?

And so
1 - instead ow SubStr(Ancleiwer,0,2) you can SubStr(AnyTextVariable or text expression , 0 , 2)

2 - Formula i made for you is meant to find T ins your text and cut it out no matter where T is

That is why in my previous message i showed you example with word BLASTER
In it T was in different place
And instead of specifying text there you can specify text variable or expression

LASTLY
I did not touch SubStr() for long time i forgot you need wrap text around “” for it to work
Error message indicates it

And would you look at that?

IF you would replace “ETRM” with variable name for example Var1
Then it would need to be

SubStr(Var1,0,StrFind(Var1,"T")) + SubStr(Var1,StrFind(Var1,"T"),StrLength(Var1)-StrFind(Var1,"T")-1)

CAUSE for gdevelop Var1 is visible as text already so you do not to point it out to gdevelop by putting it between double quotes “”

And now just so you need to suffer more reading my wall of text

SubStr(Var1,0,StrFind(Var1,"T")) + SubStr(Var1,StrFind(Var1,"T"),StrLength(Var1)-StrFind(Var1,"T")-1)

SubStr(Var1,0,StrFind(Var1,"T")) 
SubStr(From text Var1 , Start from position 0 , and take amount of characters which is StrFind(In text of Var1 , Position where text "T" is found) 

So if its ETRM it will be 
ETRM
0 1 2 3 
SO T is at position 1 (which actually is 2nd position but as i told you 1st position starts at 0)
So this way 
SubStr(Var1,0,StrFind(Var1,"T")) 
Will Start from very first position in word ETRM and will take 1 character from it which is E
So already rest is cut out

Now 
SubStr(Var1,StrFind(Var1,"T"),StrLength(Var1)-StrFind(Var1,"T")-1)
SubStr(From text of Var1 , Start at position StrFind(In text of Var , Position where text "T" is found) , And display amount of characters of StrLength(Length of text of Var1) - StrFind(In text of Var , Position where text "T" is found)
So in text of Var1 if its ETRM
We start at position of where T is found
So again
ETRM
0 1 2 3
So we start at position 1
And next we subtract where T was found from how many characters your text have
So ETRM have 4 characters - 1 = 3
So it would return TRM

I just realized i made mistake but it will prove did you read my whole message or not

So actual formula should be

SubStr("ETRM",0,StrFind("ETRM","T")) + SubStr("ETRM",StrFind("ETRM","T")+1,StrLength("ETRM")-StrFind("ETRM","T")-1)

And now that 2nd part of formula 
SubStr("ETRM",StrFind("ETRM","T")+1,StrLength("ETRM")-StrFind("ETRM","T")-1)
In text ETRM at position where is T so position 1 + 1 so we jumped to position 2

And now 
StrLength("ETRM")-StrFind("ETRM","T")-1
How many characters worrd ETRM have? well 4
So 4 - Where T is found in word ETRM? Well at 2nd position which is position 1
ETRM
0 1 2 3 

So 4 - 1 = 3
BUT we also -1 so it will = 2

So now again
SubStr("ETRM",StrFind("ETRM","T")+1,StrLength("ETRM")-StrFind("ETRM","T")-1)

SubStr("ETRM", Where T is which si 1 + 1 so 2 , (How long ETRM is which is 4 ) - (where T in ETRM is which is 1) and -1 
)
So 4 - 1 - 1 = 2

So this part
SubStr("ETRM",0,StrFind("ETRM","T")) + 
Takes everything what is BEFORE T and add it to this

SubStr("ETRM",StrFind("ETRM","T")+1,StrLength("ETRM")-StrFind("ETRM","T")-1)

Where this part takes everything that is after T 

SO in other words
We did not cut out T from ETRM
We did cut out E and RM from ETRM
And then connected them
Leaving T behind

TEXT





1 Like

How to Make the text Button? @7ToGo

capt

@7ToGo

I’m just having trouble creating the second letter, because I don’t know if I have to create another additional variable, and when I simply duplicate the one that already exists, it ends up with the same, cloned information, no matter how much I change some sentences.

Just create 1 button object, add 2 variables to it - a “letter” and a “pressed”.
Then for each button assign its letter via instance variable “letter”.

screen-20250719-140526

Wouldn’t it be necessary to change the event sheet as well? It’s no longer functional. I would appreciate a more detailed explanation, if possible, I’m sure something is missing.

@7ToGo

1. Create 3 objects.

“Text” - this object will display the word that we are entering.
“Button Text” - this object will label each button by displaying the letter it contains.
“Button” is just a regular sprite.

2. Сreating a scene or a global variable “Text”.
This will be the word that we are entering.

3. Create variables for the “Button” object.
“Letter” - will contain the letter that will be added/removed by pressing a button.
“Pressed” is a Boolean that we use to determine whether a button is pressed or not.

4. Add buttons in the scene.
There can be any number of them, for example, I made 7.

5. Specify for each button which letter it contains via instance variables.




And so on.

6. For each “Button” object, we create a “Button Text” object in its center and change the text to the value of the “Letter” variable - this way our buttons will display their letters.
изображение

7. Button functions.
Pressing a button switches its “Pressed” variable.

Then, if button is not pressed - change its animation and add “Letter” var to our global/scene var “Text”

If its pressed we also change the buttons animation and subtract that letter from “Text” var via expression we added with “Regular Expressions” extension.

Then we refresh “Text” object to reflect the changes to our word.

Hope that helps.

I would like you to do a step-by-step guide teaching how to create a second button, when the first one already exists, which in your case is the letter B. Because I did something similar to what you did, when I created the first button, and even so, it kept repeating the variable of the first one.

@7ToGo