How do i access a number within a string "plz help"

Im trying to access a specific area in a string
lets say I have a string 9934576 so I want to only access the 99 and 34 and 576 all separately

I want to access these numbers separately for an inventory system I want to save the rareity “gold,epic,rare,etc” and category “shirt,weapon,health,etc” the rest of the string would be for the item in the first place

im currentlty saving these

chest to save if its been opened “if the chest number is in an array it will be opened”

chest level to see if players level is strong enough to open chest "ill most likely add a key and no key variant

Item id is why i would like to figure this out I feel it would add a lot more freedom for me in the future this would save the category and rarity of an item along with what item it would be

Amnt is incase duplicates there are no items that I wont want u to receive multiples of so im not gonna code in a feature for that

enchantments idrk if ill implement but i feel it would give more strategy

GOLD idk money you receive from a chest :stuck_out_tongue:

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

IDK how it works now after recent changes of variables
But you would turn number variable into string variable by writing ToString(Variable)
Now for you you still see 9934576
But for engine now that is text and not a number
So if you add 2 to it you won’t end up with 9934578
But with 99345762 cause now it is as if you have collection of letters and you add another letter to whole string
Imagine like each letter is block and you simply add another block to your structure
Kinda think building something with lego blocks

Ok now you can use StrAt(string, number)
So you could go with
StrAt(ToString(Variable),0)
StrAt(ToString(Variable),1)
Get
9
and
9 which are 1st two 99 of your 9934576
Be aware you get 9 and 9 not 99 from it
Imagine ToNumber(String) inverts process by changing string (letters) into numbers
So you could go
ToNumber(StrAt(ToString(Variable),0)+StrAt(ToString(Variable),1))

And from this you would get 99

Armed with that knowledge all you change for various positions of digits you need would be

ToNumber(StrAt(ToString(Variable),HERE)+StrAt(ToString(Variable),HERE))

So for example going with 9934576 you want to get 345 out of it
So not 2 but 3 digits starting from left it counts as

0 1 2 3 4 5 6
9 9 3 4 5 7 6
So position ONE (or first letter) is not starting as 1 but as 0 so keep that in mind
Now you would go with

ToNumber(StrAt(ToString(Variable),2)+StrAt(ToString(Variable),3)+StrAt(ToString(Variable),4))

And that would spit out 345 from 9934576
So inside this ToNumber( )
For each letter/digit you need StrAt(ToString(Variable),DigitPositionHere)
And if you need more than one then
StrAt(ToString(Variable),DigitPositionHere)+StrAt(ToString(Variable),DigitPositionHere)
You need + between expressions

hey what event would this be? im kinda confused abt this could u show an event please

It would be easier to store the data into structures or even a structure of structures.

If you want things to be in a single string then you could use the array tools extension. It has an expression that will break apart a text separated by say commas into an array. It would be more dynamic because the values could be different sizes.

This would split a variable into a array and add the items to a text object.

The preview.
Screenshot_20240905_144709_Chrome

If you want to just display it then it is change text for text object action

There’s a regular expression extension that could be of help here too. Regular expressions (or regex) are a way of defining rules for pattern matching, and will do what you want.

does anybody know how I can reword this so it auto goes through every childe in the array or structure ?

Thank you

JSONify the structure into a string, run regex over it and then parse the string back into a structure.

1 Like

how would I go about doing this sorry I think I’m just missing something
Im really bad with finding the right events

Setup:


Events (these could be compressed into one action, but it’s spread out to make it easier to understand):


Result (viewed in the debugger):

thank you a lot I will update if it works :slight_smile:

hey, I’ve never seen the declare option. how/where do I access this?

Right click in a blank part of the event’s condition box, select Add, and choose local variables.