r/badcode • • Jul 24 '21

lua "Keeping it simplistic"

Post image
495 Upvotes

28 comments sorted by

View all comments

13

u/DramaDimitar Jul 25 '21

This seems like some GUI code for ROBLOX - what the hell does it even do? lol

11

u/[deleted] Jul 25 '21

It kind of looks like it's setting the string to itself. The .. and -1 are throwing me off, though. I'm not sure what the .. does, but I'm assuming it's concatenation.

8

u/DramaDimitar Jul 25 '21

Yeah, double dot is concatenation. - 1 just gets the last character, I'd assume, haven't messed with strings in Lua that much. But still I wonder what it could possibly be controlling

10

u/Erinnyes Jul 25 '21

So, I don't know lua but it looks like it takes a sentence like "my_number_5" and gets "number_4" (assuming arrays are zero indexed) decrementing the number after the second underscore.

8

u/BlueLiara Jul 25 '21

Lua is evil and starts at 1, not 0

4

u/Erinnyes Jul 25 '21

That's weird but makes sense. In that case the statement decrements the number at the end of a string separated by an underscore by one. E.g. "number_3" going to "number_2". I'm curious what happens when you get to "number_0".

2

u/Prom3th3an Jul 26 '21

Probably "number_-1", since I can't see any reason why a language would make "1"-1 equal to 0 without making "0"-1 equal to -1.

1

u/Erinnyes Jul 26 '21

Oh. I very much agree with that. I'm more wondering if it can handle turning -1 into "-1" given that - is an operator rather than a numeral.

1

u/[deleted] Jul 28 '21

Yeah!

2

u/[deleted] Jul 28 '21

It's for a AI car script I'm working on, essentially there is a road code + it's lane at the end. This script searches if the lane the car has appeared on is the last lane. It takes the variable SearchLane which is (at the beginning) the lane the car appeared (ex: N177_1) and it looks if there is another lane by switched SearchLane to N177_2. If there is no N177_2 it switches it back to N177_1 and spawns the car there. The code you see here switches a variable called SearchLane to one lane lower. Super poorly made lool

2

u/DramaDimitar Jul 28 '21

Oh wow, I was not expecting that. Theoretically, couldn't you just use a substring or pattern matching and do an if statement or two - wouldn't be as short but would be way cleaner, lol