A cool state representation trick with Transparency

I was writing a small function for showing/hiding some beam and its endpoint Part, and figured out something that I think deserves mentioning:

You can micro-optimize memory and make your code a bit more terse (though readability could depend on how you write it) by storing original .Transparency values as the decimal part of the transparency.

This is because .Transparency unintuitively accepts numbers less than 0 and greater than 1, for some reason.

Parts Example

You have a collection of parts with transparencies 0.1, 0.3, 0.21, 0.67, 0.69, 0.42, yada yada yada… You want to write a function that hides and unhides them using their transparencies. I’d think that someone might intuitively tackle this problem by storing the original transparencies above in an Attribute or somewhere else in the code. But, you could actually just store them in the decimal portion of your transparencies by adding 1 to each transparency.. then subtracting 1 when you want the parts unhidden! And, if you’ve come across any transparent parts, they’ll still be transparent!

So, if you wanted to loop through a group of objects with some sort of .Transparency property (or something that behaves similarly in that it shares the oddity I described above), you can try implementing something like this hide/show function I wrote below:

local function toggle_indicator(shown)
	Arrow.End.Transparency += (if shown then 0 else 1) - math.floor(Arrow.End.Transparency)
	Arrow.Beam.Enabled = shown
end

I like writing terse code.. aaaand this could definitely be made more readable :skull: :clown_face:. But I decided to share this little trick I found when trying to save my time writing extra lines (and the trivial amount of extra memory I’d use).

Please note that this is code for a project I’m working on, and is not generalized at all lmao. It’s up to you to implement this random trick I’m sharing.

For some reason, coming across this neat trick (though using a very obvious fact that .Transparency > 1 is possible) in my toggle_indicator function felt really beautiful, though trivially terse. I thought it wasn’t intuitive enough for developers for me not to make this a small gem. :grin:

2 Likes

That is a nice trick, but can I recommend you wrap it in a module function for an easy update, so if Roblox decide to clamp their set transparency values in the future you don’t have to hunt through every script you wrote to fix it. :slightly_smiling_face:

3 Likes

what do you mean fix, you can bypass property clamps?

wont this just slowly add up floating point errors until its not how you wanted it anymore?

2 Likes

This is pretty clever actually.

1 Like

Yes! I think this is potent behavior. So, I think a developer who uses this trick should keep this in mind if they repeatedly hide/unhide things using +1/-1. However, I do think that the minimal drift that may change a value would negligibly change an individual BasePart’s transparency if not repeatedly hidden/unhid. I’m not a pro at this, so I’d like it if anyone can comment further on the scale at which this error could cause problems.. especially since it’s with regard to Transparency…

Btw you can replace math.floor with

Arrow.End.Transparency//1

It’s more optimized and has own opcode


Finall result:

local End = Arrow.End
local Beam = Arrow.Beam
local function toggle_indicator(shown:boolean):()
	End.Transparency += (if shown then 0 else 1) - End.Transparency//1
	Beam.Enabled = shown
end
2 Likes

That a terrible idea
Ctrl+Shift+H and replace it right away
Module script will simply prevent function from being inlined.
Module scripts are really bad and you should avoid use of them unless you use them for sharing tables etc stuff that otherwise would not be inlined or dynamic anyway

"module scripts are really bad":clown_face:

3 Likes

Yes, they are bad, and it’s a fact.
Luau does not have any AOT optimization for modules or linkers; it gets required at runtime.

Your passionate advent to share your beliefs of optimizations, don’t seem to be in good faith to me, I feel like you should try to be more respectful and polite when trying to tell others they are wrong according to your own personal beliefs which you call as facts without providing proof.

2 Likes

I provide proofs; it’s your side that should do research before accusing someone of not providing proofs.
I heavily recommend you read my post to understand what I talk about.

1 Like