+1 vote

Hi, I've been making a custom class replicating the perspective warp effect using the polygon 2d's uv system. The problem is that the code I wrote doesn't work when the texture has the repeat property enabled.

I need a way to check through the code if the texture has the repeat flag enabled (mirrored too if possible) so that I can disable the uv changing step.

The docs mention the get_flags() function but don't say how to use it. I've tried using it as an enum but it didn't work:

if tex.get_flags() == tex.FLAG_REPEAT:
    #do stuff...
    #do other stuff...

Does anybody know a way to do this?

Godot version 3.5.stable
in Engine by (120 points)

2 Answers

+2 votes
Best answer

You can use something like this to check if a given bit is set (or not) in a given value:

func is_bit_set(value, bit):
    return value & (1 << bit) != 0

In your case, the FLAG_REPEAT is documented to have a value of 2. So, that'd be bit position 2. So, to test for that, you can do:

var flags = tex.get_flags()
var is_set = is_bit_set(flags, 2)

Note, you need to pass the bit POSITION as the second argument, not the bit VALUE. So, for example, FLAG_FILTER is value4, so that'd be bit position 3.

And, to Wakatta's point, it doesn't really matter if complex bit combinations are set. The above will correctly check if any given bit is set in a specified value.

by (21,864 points)
selected by

Dang forgot about bitwise operations.
Fantastic answer.

Yeah, I didn't even think about using them. Anyway thanks for the answers, they really helped me!

A few things here after waking up today with a working brain... :(

First, it seems my bit positions are off by one in the above explanation. So, rather than the value of "1" being bit position "1", it's bit position "0" (and so on) - at least the way the provided method works.

Also, I realize that you're really working with a bit VALUE (in the FLAG_* enums) rather than a bit POSITION. With that in mind, this can be simplified to this:

func is_bit_val_set(value, bit_value):
    return value & bit_value != 0

With that, you can simply pass the bit VALUE (rather than manually converting it to a bit POSITION - and running the chance of getting that wrong as I did above):

var flags = tex.get_flags()
var is_set = is_bit_val_set(flags, TEXTURE.FLAG_REPEAT)
+1 vote

OMG feel your struggle and walked down that same road
Unfortunately its not as simple as you've described since the flags are calculated together.



 var flags = get_flags(): # equals 34 for the above options

Wait it gets even worse as there are flag combinations that aren't documented


now that you know how it works it should be easier to get what you want.

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