Can I somehow get the world location of a letter in a text object?

I was wondering if I can get a specific world location of a letter in a text object via Python.

For example, I have this dialogue box in my game where you can see the symbol “@”.
I’d love to somehow find its world location so I can spawn an icon over the symbol.

def letterloc(cont):

    own = cont.owner
    letter_loc = own.scene.objects['letter_name_here'].worldPosition

    if letter_loc:
        return str(letter_loc)
    else:
        print('something is wrong with letter_loc!!!')

then use letterloc(cont) at the @ position

Thank you for your answer.
As I understand from your reply, this would find the location of the text object.
But do I have to know when the @ symbol appears?
Is there something that would automatically find the @ symbol or its number in the text?

text objects have limited functionality, and one way to get around it is to split op you text and have “option 1” as one text object and “@ - 10” as a second text object, this way the @ is the first letter and the position would be the same as the text object.

1 Like

True, that would work.
The thing is that I have it coded that Option 1 and the second line of Option 2 are the same text object.
I didn’t want to add too many of those if I didn’t have to.
I guess I’ll have to find another way.

if “option 1” and “option 2” is something persistent then have them as a texture on a plane, way faster to render to.

No no in this case it’s just a placeholder text for something different.
All I wanted to say is that these two options which can be anything are a part of one text object.

If you just have one @ in the text object then simply replace the @?

location =  letter_loc(cont)
text_obj.replace('@', location)

Thank you for your suggestion and it looks really promising but I’d like to ask.

letter_loc = own.scene.objects['letter_name_here'].worldPosition

This is for the location of the Text Object?

text_obj.replace('@', location)

Also, the text_obj what exactly is this definition for? The only thing that worked was with the Text object’s string property.
And what does it do? I’ve tried it and it wouldn’t do anything.
I’m sure I’ve messed up somewhere.

“letter_name_here” - put in the name of the object you want the position from

text_obj.replace('@', location)

text_obj = the text that need to replace the @ with the letter_loc

so it would be something like this:

def letterloc(cont):

    own = cont.owner
    letter_loc = own.scene.objects['letter_name_here'].worldPosition

    if letter_loc:
        return str(letter_loc)
    else:
        print('something is wrong with letter_loc!!!')


def change_text(cont):

    own = cont.owner
    location =  letter_loc(cont)
    text_obj = own.scene.objects['text_object_name']['Text'] OR own['Text'] #delete one and the OR, Text is for the text property if script is on an other object use the first part
    text_obj.replace('@', location)

run the script on the object with python brick set to module mode and enter the “scriptname.change_text” (without quotes).

I see, there might have been a misunderstanding.
The ‘@’ symbol is part of the text string of ‘OPTION 1 @ - 10’.
It’s not alone as one symbol in the string. So this doesn’t work.
Maybe what I’ve wanted cannot be done.
I wanted to know if you can locate the ‘@’ symbol in a string of text and find out its world location.
The world location of the letter in a text and not the text object.
It might be impossible.

uh why would you want the location of a letter/symbol?

  • it’s a pain in the ass to calculate that, if it’s even possible at all. i’m not gonna tackle that one.

It’s alright, thanks anyway.
I’ve made a different method. I have used the len() to calculate how many symbols are there and moved the icon based on that.

you need a mono space font for that to work.

1 Like

Yes, I know but at least it works.