Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Dewgy
Nov 10, 2005

~🚚special delivery~📦

Tunicate posted:

IIRC Lua doesnt' have a match statement or switch statement built in, right?

Lua doesn’t even know what a constant is, so, yeah basically. But, you can very easily abuse its key/value table system to do more or less the same thing, if you’re trying to be more efficient.

Adbot
ADBOT LOVES YOU

hailthefish
Oct 24, 2010

yeah was gonna say, in lua your options are basically do the ten thousand line if-else, or do some really bonkers table magic poo poo.

Dewgy
Nov 10, 2005

~🚚special delivery~📦

hailthefish posted:

yeah was gonna say, in lua your options are basically do the ten thousand line if-else, or do some really bonkers table magic poo poo.

“Table magic poo poo” is Lua’s major strength so I mean why the hell not. :v:

megane
Jun 20, 2008



You can indeed do table magic poo poo, something like
pre:
match_table = {
  plus = function(x, y) return x + y end
  product = function(x, y) return x * y end
}

function fake_match(x, y, action)
  return match_table[action](x, y)
end

print(fake_match(2, 5, "plus"))
print(fake_match(2, 5, "product"))

j.peeba
Oct 25, 2010

Almost Human
Nap Ghost
All the data in Grimrock games was Lua table magic poo poo too and it was great. Being able to spice up data with the occasional function or defining objects in bulk with for loops for example is a nice luxury to have.

Shoehead
Sep 28, 2005

Wassup, Choom?
Ya need sumthin'?
If it works who cares how you did it? (Please never look at my code)

TooMuchAbstraction
Oct 14, 2012

I spent four years making
Waves of Steel
Hell yes I'm going to turn my avatar into an ad for it.
Fun Shoe

Jabor posted:

But it feels like a ton of boilerplate to write it that way when you're starting out with only a half-dozen cases, it's often easier to just start with something that works well at that small scale and then keep growing it.

and once you have a pattern for how you add new content, it's way easier to stick to that pattern than it is to rearchitect everything. And, again, this is fine, it's a simple process, you know where to go to look up how something works. About the worst I can say about it is that it's hard to do comparisons of cards without a lot of scrolling around. The performance penalty of doing thousands of string comparisons every time a card needs to be scored is, honestly, negligible these days.

SystemLogoff
Feb 19, 2011

End Session?

Lua caches strings anyway, so the penalty for string comparison is already reduced as a language feature.

LuaJIT is such a nice language.

Red Mike
Jul 11, 2011

TooMuchAbstraction posted:

About the worst I can say about it is that it's hard to do comparisons of cards without a lot of scrolling around.

And if you end up needing that often enough, the pattern is simple enough that you move your data over to some other tool (like an Excel/Google Sheets spreadsheet), then write a really basic CSV importer that generates a string of that chain of ifs based on the data in the sheet, and you literally run it and copy paste the code in when you're done balancing the numbers. You can even automate it into your build pipeline.

KillHour
Oct 28, 2007


megane posted:

You can indeed do table magic poo poo, something like
pre:
match_table = {
  plus = function(x, y) return x + y end
  product = function(x, y) return x * y end
}

function fake_match(x, y, action)
  return match_table[action](x, y)
end

print(fake_match(2, 5, "plus"))
print(fake_match(2, 5, "product"))

It's weird to call this "magic table poo poo," since that implies to me that it's hacky/messy. It's the same as using a dictionary/hashmap of anonymous/lambda functions, which is extremely common in many languages. I do it all the time and it's a very normal alternative to giant if/else blocks. They don't need to be replaced if they aren't a problem, but this is pretty much the standard for how you would do it.


Edit: My giant conditional blocks are implemented in shaders inside a critical loop that gets run hundreds of millions of times per frame and needs to be as optimized as possible. I use case statements :v:

Double edit: I'm not exaggerating. That case statement is evaluated up to 100 times per function per pixel and there can be up to 32 functions, so that's a worst case of ~26.5 billion evaluations per frame in 4k. Worst case can't really ever happen, but 100s of millions is common. It still runs at several hundred FPS on my 3090 (for now, until I bog it down with more poo poo).

KillHour fucked around with this message at 15:30 on Apr 24, 2024

megane
Jun 20, 2008



Yep. One of the reasons Lua appeals to me it that it gives you one simple-yet-powerful tool, the table, and then you solve pretty much every problem by applying that one tool in clever ways. You can implement Sets by using tables "backwards," with the elements as keys and true/false as the values, for example. That's definitely a downside for many applications, but I enjoy it as a mental exercise.

Your Computer
Oct 3, 2008




Grimey Drawer
but it's 1-indexed! how am i supposed to use a language that's 1-indexed!!!

Chev
Jul 19, 2010
Switchblade Switcharoo
In one of the legacy applications at work the previous dev suffixed every 0-indexed thing with _indexStartsAt0.

As it's written in C#, it makes the source quite a thing to behold.

xzzy
Mar 5, 2009

Congrats on your employer coming up with a code standard that actually self documents!

TooMuchAbstraction
Oct 14, 2012

I spent four years making
Waves of Steel
Hell yes I'm going to turn my avatar into an ad for it.
Fun Shoe
I appreciate how lean and elegant Lua is, but I haaaaate having to bring my own tools (and/or find and install others') to the party. I want a robust standard library that ships with the language. :(

Dieting Hippo
Jan 5, 2006

THIS IS NOT A PROPER DIET FOR A HIPPO
I always have handy this list of curated Love2D libraries for literally everything you can think of for Love2D/Lua gamedev. For objects, 'hump.class' or 'middleclass' are both good picks, and for asset management 'cargo' is pretty much the easiest way to pull in any file data for quick use.

Polo-Rican
Jul 4, 2004

emptyquote my posts or die

the trombone champ background animations are built kinda like this for the same boneheaded reasons: each track having unique effects that couldn't easily be defined in a shared class, plus me being ignorant about a handful of coding fundamentals due to being self-taught

edit: whoa! They deleted the tweet lol

Adbot
ADBOT LOVES YOU

Chrs
Sep 21, 2015

This game I made just won ScoreSpace Choice on ScoreSpace Jam 29.



Play on Itch: https://chrsgr.itch.io/gravoor-infinity
Play on Newgrounds, with online leaderboard: https://www.newgrounds.com/portal/view/927101

So thats obviously very exciting for me lol. Check it out if you guys have a moment free.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply