Jump to content

Controlling a Material state


photo

Recommended Posts

Posted

Hello,

I need to check at runtime if the Auxiliary buffer is enabled for a specific material. This is the code I use:

int i = m->getState("auxiliary");
if (i >= 0 && m->getState(i) == 0) { ...do something when Aux is Enabled... }

Surprisingly, if the Aux buffer is Enabled in the base mat, then getState()==0, which is rather counter intuitive.

  • Is my code correct? Did I miss something?
  • If this is correct, then could you please document this more clearly?

Thanks!

Posted

Hi, @Amerio.Stephane!

Seems like there is an error in your fragment of  code...

14 hours ago, Amerio.Stephane said:

int i = m->getState("auxiliary");
if (i >= 0 && m->getState(i) == 0) { ...do something when Aux is Enabled... }

You're getting the current value of the "auxiliary" state (1 for enabled / 0 for disabled) via getState() by the name of the state and then pass this value to anothe overload of getState(i) again in the IF-condition treating 'i' as the index of the state named "auxiliary", but actually it is a value!

The code should look something like this:

int i = m->getState("auxiliary");
if (i) { ...do something when Aux is Enabled... }

Hope this helps.

Posted

Ooooh Stupid me. I thought getState(const char*) would return the index and -1 if this was inexistent... (so my code worked by pure coincidence, BAD!)

So... what happens if I call getState("not_a_valid_name") ?

Posted

Oh, getState("not_a_valid_name") will give you 0 as well, which might be a bit confusing.

If you want to check whether the state with the given name exists, you can call findState("state_name") - it will return state's ID, or -1 in case the name is invalid.

You should also be aware that states, parameters, and textures can be ignored in case conditions they depend on are not met. To check conditions you can call checkStateConditions ( state_id ). So in order to perform all checks for a toggle state (auxiliary) You can do something like this:

// trying to get an ID of the state by name
int id = m->findState("auxiliary");

// chacking if the state with the given name exists
if (id == -1)
  return 0;

// checking if the state is enabled and not ignored (its conditions are met)
if (m->getState(id) && m->checkStateConditions(id)) 
{ ...do something when Aux is Enabled... }

We'll update the docs! Thanks!

Hope this helps!

  • Thanks 1
×
×
  • Create New...