The Code In question:


Code:

void gameOver()
{
      while(kb_Data[1] & kb_2nd)
         kb_Scan();
      uint8_t temp = 0;
      
      uint8_t* High = &temp;
      
      ti_var_t hs = ti_Open("XW3HS","r");
      
      ti_Read(High,4,1,hs);
      ti_Close(hs);
      if(*High < score)
      {
         hs = ti_Open("XW3HS","w");
         ti_Write((char)score,, sizeof(uint8_t),hs);
         ti_Close(hs);
         pgrm_CleanUp();
      } else
      {
         ti_Close(hs);
         pgrm_CleanUp();
      }
      char* appendValue1 = float2str(*High);
      char* appendValue2 = float2str(score);
      gfx_ScaledSprite_NoClip(background, 0, 0, 10, 10);

      
      gfx_PrintStringXY("YOUR SCORE:",(24+(16*11)),100);
      gfx_PrintStringXY(appendValue2,(24+(16*11)),110);
      
      gfx_PrintStringXY("PREV BEST:" ,24,100);
      gfx_PrintStringXY(appendValue1,24,110);
      
      gfx_PrintStringXY("PRESS 2ND TO CONTINUE",24,160);
      gfx_BlitBuffer();
      while(!(kb_Data[1] & kb_2nd))
         kb_Scan();
      while(kb_Data[1] & kb_2nd)
         kb_Scan();
}

it seems like the high score is either not reading or not writing corrfectly as the value does not seem to display correctly ie: it shows your current score instead of your high score
Why are you casting score to a char, and why are you reading 4 bytes from hs when a uint8_t is 1 byte? I’d also like to know where score is defined, as I can’t seem to see it. Also, you’re using floattostr on a uint8_t, which could be causing some issues as well. Finally, there are two commas in the write.
epsilon5 wrote:
Why are you casting score to a char, and why are you reading 4 bytes from hs when a uint8_t is 1 byte? I’d also like to know where score is defined, as I can’t seem to see it. Also, you’re using floattostr on a uint8_t, which could be causing some issues as well. Finally, there are two commas in the write.
dang i didnt notice how shoddy my code was, thanks Smile
Just wondering what the range is for your score? Can it be a really big number?
tr1p1ea wrote:
Just wondering what the range is for your score? Can it be a really big number?
its usually a small-ish number but if your good it can get pretty big

Code:
void gameOver()
{
      while(kb_Data[1] & kb_2nd)
         kb_Scan();
      float temp = 0;
      
      float* High = &temp;
      
      ti_var_t hs = ti_Open("XW3HS","r");
      
      ti_Read(High,4,1,hs);
      ti_Close(hs);
      if(*High < score)
      {
         hs = ti_Open("XW3HS","w");
         ti_Write(&score,4,1,hs);
         ti_Close(hs);
         pgrm_CleanUp();
      } else
      {
         ti_Close(hs);
         pgrm_CleanUp();
      }
      char* appendValue1 = float2str(*High);
      char* appendValue2 = float2str(score);
      gfx_ScaledSprite_NoClip(background, 0, 0, 10, 10);

      
      gfx_PrintStringXY("YOUR SCORE:",(24+(16*11)),100);
      gfx_PrintStringXY(appendValue2,(24+(16*11)),110);
      
      gfx_PrintStringXY("PREV BEST:" ,24,100);
      gfx_PrintStringXY(appendValue1,24,110);
      
      gfx_PrintStringXY("PRESS 2ND TO CONTINUE",24,160);
      gfx_BlitBuffer();
      while(!(kb_Data[1] & kb_2nd))
         kb_Scan();
      while(kb_Data[1] & kb_2nd)
         kb_Scan();
}

hmmm i applied the fixes you reccomended and it seems to still not work
HacksAndSlash wrote:
[code]
hmmm i applied the fixes you reccomended and it seems to still not work

What is your issue? I’m not sure you specified what it was.
If you're only using integers/whole numbers for scores, I would recommend not using floats but an integer type like uint24_t. If you use integers, instead of doing

Code:
char* appendValue2 = float2str(score);
gfx_PrintStringXY(appendValue2,(24+(16*11)),110);

you can just do

Code:
gfx_SetTextXY(24+(16*11), 110);
gfx_PrintUInt(score, 1);


I'm guessing the issue here is in float2str - could we see the code for that, if you decide not to switch to integers? It's likely that the buffer that it's writing the string to is being overwritten the next time it's called, before the string is actually displayed.

You should also use sizeof to make sure that you don't overflow whatever you're reading into - e.g. ti_Write(&score,sizeof score,1,hs). Otherwise, you might read too many bytes and randomly change the value of some other variable.

I also don't see the need for a separate temp variable and High pointer - why not just use &temp in place of High and temp in place of *High?

Additionally, pgrm_CleanUp() is a compatibility define - it doesn't do anything anymore, so you can remove it with no ill effects.
commandblockguy wrote:
If you're only using integers/whole numbers for scores, I would recommend not using floats but an integer type like uint24_t. If you use integers, instead of doing

Code:
char* appendValue2 = float2str(score);
gfx_PrintStringXY(appendValue2,(24+(16*11)),110);

you can just do

Code:
gfx_SetTextXY(24+(16*11), 110);
gfx_PrintUInt(score, 1);


I'm guessing the issue here is in float2str - could we see the code for that, if you decide not to switch to integers? It's likely that the buffer that it's writing the string to is being overwritten the next time it's called, before the string is actually displayed.

You should also use sizeof to make sure that you don't overflow whatever you're reading into - e.g. ti_Write(&score,sizeof score,1,hs). Otherwise, you might read too many bytes and randomly change the value of some other variable.

I also don't see the need for a separate temp variable and High pointer - why not just use &temp in place of High and temp in place of *High?

Additionally, pgrm_CleanUp() is a compatibility define - it doesn't do anything anymore, so you can remove it with no ill effects.
IT worked, Thank you Very Happy
Why are you casting score to a char, and why are you reading 4 bytes from hs when a uint8_t is 1 byte? I’d also like to know where score is defined, as I can’t seem to see it. Also, you’re using floattostr on a uint8_t, which could be causing some issues as well. Finally, there are two commas in the write.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement