Handmade Hero » Forums » Code » [062] Visual Studio function glitch
Huntress77
3 posts
#12005 [062] Visual Studio function glitch
4 days, 18 hours ago

I was going through the beginning of Day 62 and was baffled as to why my Sword.High entity was returning null pointer. Turns out I was baffled because there was nothing wrong with the code. Visual Studio is doing a weird thing where it's treating an if statement inside a function as scoped, so when the sword leaves that if block, it goes out of scope and the function can't return it.

When I moved
1
return(EntityHigh)
inside the glitching if statement, it worked just as it should. Fortunately, VS isn't doing this anywhere else in the code, just in that one spot. I still want to fix it, though, and reinstalling VS didn't help.

Does anyone have any ideas?
mmozeiko
Mārtiņš Možeiko
1313 posts
1 project
#12006 [062] Visual Studio function glitch
4 days, 18 hours ago Edited by Mārtiņš Možeiko on May 20, 2017, 1:25 a.m.

Are you saying this doesn't work:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
    high_entity *EntityHigh = 0;
                                    
    if(...)
    {
        EntityHigh = ...;
    }
    else
    {    
        EntityHigh = ...;
    }

    return(EntityHigh);


But this works:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
    high_entity *EntityHigh = 0;
                                    
    if(...)
    {
        EntityHigh = ...;
        return(EntityHigh);
    }
    else
    {    
        EntityHigh = ...;
        return(EntityHigh);
    }
??

I'm pretty this is not the issue and reinstalling VS will not help you here at all. There is something else wrong either with your changes or how are you using compiler or debugger.