This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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:
enter image description 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!

in Engine by (14 points)

1 Answer

0 votes

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)
by (6,942 points)

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.

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

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!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.