How to implement variables into a file path in an interface scene node?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ShinyRedExp

Incredibly rookie coder here. I’m making a game and currently trying to make it more optimized because while I’ve heard people tell me that I should just code and wait to optimize later on, this code I feel like is too brute force-y and there is a much smarter solution to do what I’m trying to do. And by that I mean…

Basically, I have this code right here:

Figured this was a terrible way to code, so I decided to try out a for loop! Like so:
enter image description here

But then there was one big problem! The file path, “$CommandBox/CommandList/Command1”.

Command1 worked when Character1CommandActions[0] was being matched, but I need it to be Command2 for Character1CommandActions[1], Command3 for Character1CommandActions[2], and so on and so forth.

And that’s where I get stuck. I tried to perhaps use variables to construct it, like this.
enter image description here

But that gave me an error(Invalid operands ‘Object’ and ‘int’ in operator ‘+’.), so that couldn’t have been it. I learned about NodePath and tried putting it in. Also an error. I tried converting the things inside the NodePath into a string. Still an error.

Then there was also that file paths inside a variable did not like “.texture.region”? I ended up getting this error too(Invalid get index ‘texture’ (on base: ‘null instance’).).

Trying not to pull my hair out, this is so stressful and I am stuck on what to do. Do you guys know how to implement file paths into a variable? And if you don’t, is there maybe a better way to go about what I want to achieve rather than do a for loop? I am very new and almost all of the concepts Godot has to offer will be alien to me, I’d like some assistance if possible. Thank you!

:bust_in_silhouette: Reply From: Wakatta

If it ain’t broke don’t try to fix it or this happens

Trying not to pull my hair out, this is so stressful

for n in 3:
    var command = get_node("CommandBox/CommandList/Command%s" % n + 1)
    match Charater1CommandActions[n]:
            command.texture.region = Rect2(18, 0, 18, 18)
            command.texture.region = Rect2(90, 0, 18, 18)
            command.texture.region = Rect2(126, 0, 18, 18)

To be honest this is kind of a lazy answer as your code has a pattern
And whenever that happens its best to use an Algorithm

Something like

# not actual code
func command_decision(action):
    if not action in Charater1CommandActions:
    get_node("CommandBox/CommandList/Command%s" % action + 1).texture.region = Rect2((action + 4) * 18, 0, 18, 18)

I guess that’s true, why fix it if it isn’t broken. But yet still, there’s this itch in me that needs to be scratched when I see my very clunky code.

Anyways, algorithms are actually very new to me, so I think I know what I’ll be researching about next. Thank you so much! I’ll try and see how I can input this in my code.

ShinyRedExp | 2023-01-08 01:24

Understand that itch all too well.
And It’s what keeps you up past midnight for countless nights.

So this what you did here, asking this question is very good compared to being stuck inside your own head trying to figure things out.

Another great thing Todo is step away from your code, maybe try some people watching and you’ll notice with your programmatic mind several behavioural patterns of your neighbors, friends and even your family

Wakatta | 2023-01-08 03:24

Understood! Yeah, I suppose sometimes if you try and look at it from another angle like that it could possibly help you. Thank you once again!

ShinyRedExp | 2023-01-08 05:39