Custom Function Undeclared Identifier HELP

I’m following a written explanation by someone who us creating a custom function. I’m working in 2020.3.8f1 in URP.

There was an error I ran into regarding an ‘undeclared identifier’ that got solved when changing #ifdef SHADERGRAPH_PREVIEW to #if defined(SHADERGRAPH_PREVIEW) .

In another custom function I’m just adding onto that but having the same issue with a #ifndef SHADERGRAPH_PREVIEW . Did #ifndef also get changed? I’ll post below this function and let me know if something else is getting buggy.

void AddAdditionalLights_float(float3 WorldPosition, float3 WorldNormal, float MainDiffuse, out float Diffuse) { //takes the world pos, world normal and main diffuse
    Diffuse = MainDiffuse; //diffuse is mainlight diffuse
#ifndef SHADERGRAPH_PREVIEW //if shadergraph preview not defined
    int pixelLightCount = GetAdditionalLightsCount(); //get the number of lights in scene
    for (int i = 0; i < pixelLightCount; ++i) { //for each loop, starting at 0 through to total light count:
        Light light = GetAdditionalLight(i, WorldPosition); //get additional light i (from the loop) at world pos
        half NdotL = saturate(dot(WorldNormal, light.direction)); //clamps the dot product 0-1 between the world normal and light direction
        half atten = light.distanceAttenuation * light.shadowAttenuation; //multiplies distance and shadow atten
        half thisDiffuse = atten * NdotL; //diffuse is atten * NdotL
        Diffuse += thisDiffuse; //adds this to the original diffuse
    }
#endif
    half total = Diffuse;
}
#endif

Hi, you can use #if !defined() instead of #ifndef, just as #if defined() can be used instead of #ifdef. Note the exclamation mark within the former directive.

Though I should probably mention that the bug where using #ifdef and #ifndef would cause compile errors seems to be have been fixed in Shader Graph 10.5.0 (the version currently associated with Unity 2020.3 LTS). At least, I no longer get them ever since I switched to LTS. Not that it’s super important, since both directives have alternatives, but you know.

That being said, you seem to be having one #endif directive too many in your code, which is going to cause a “unexpected #endif directive” error.