About Store Forum Documentation Contact



Post Reply 
[SOLVED]How to create a Hiscore Table in EE
Author Message
Otolone Offline
Member

Post: #1
[SOLVED]How to create a Hiscore Table in EE
Hi there!
How do I create a Highscore table in EE.
I used MEMC memory container to hold

Class PlayerData
{
str Name;
int score = 0;
}
to hold the data for each player.
How do I save this data to hold the the scores even if I exit the application?
I guess I have to use the File object to write to disk?
Any suggestions will be appreciated.
Thanks.
(This post was last modified: 02-16-2018 04:49 PM by Otolone.)
01-30-2018 10:56 AM
Find all posts by this user Quote this message in a reply
Tottel Offline
Legacy License Owner

Post: #2
RE: How to create a Hiscore Table in EE
If you're planning on having lots of players, and load this data selectively for certain players who play again, I'd recommend using a database instead.
01-30-2018 02:11 PM
Find all posts by this user Quote this message in a reply
Otolone Offline
Member

Post: #3
RE: How to create a Hiscore Table in EE
Thanks. I guess I have to learn database management.
01-31-2018 10:53 AM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #4
RE: How to create a Hiscore Table in EE
You don't need a database for that.
Yes you can use File or TextData if you want to store in text format.
I recommend looking into tutorials, many of them have some sort of saving data to files.
02-01-2018 03:48 AM
Find all posts by this user Quote this message in a reply
RedcrowProd Offline
Member

Post: #5
RE: How to create a Hiscore Table in EE
it really depends hows the idea behind the highscore.
is it online? if yes, then are you saving those to be per player ? when do you need to read it ? how big is it going to be ? then DB might suit best.

if its a highscore for local machine, with just different profile, for sure use file for that.
02-02-2018 01:35 AM
Find all posts by this user Quote this message in a reply
Otolone Offline
Member

Post: #6
RE: How to create a Hiscore Table in EE
Why does the code below work while the second version crashes?
//Version1
/*****************************************************************
Code:
player_name = "John";


int score = 100;
/*****************************************************************/
class PlayerData
{
   Str name;
   int score;
}

Memc<PlayerData>names;
/***************************************************************/
int compare(const PlayerData &a, const PlayerData &b)
{
   if(a.score<b.score)return -1;
   if(a.score>b.score)return 1;
   return 0;
  
}
/*****************************************************************/
void save()
{
  
   {
      File f; // file object

      f.append("score.dat"); //  let's append a real file on Hard Disk
      f.putInt(score);    
      
      
   }
  
   {
      File f;

      f.append("names.dat");
      
      f.putStr (Player_name );
      
      
   }
  
  
}

void load()
{
    // when the file is created we can now read objects it
   {
      File f;
      
        Str str;
        f.read   ("names.dat"); // start reading file  
        if(f.is())
        while(!f.end())
        {
          
      str=f.getStr ();
      PlayerData &l_name=names.New();
      l_name.name = str;
      }
      
   }
  
   {
      File f;
      
        
        int score;
        int cnt=0;
        f.read   ("score.dat"); // start reading file    
        if(f.is())
        while(!f.end())
        {
          
      score = f.getInt();
      names[cnt].score = score;
      cnt++;        
        
       }
      
   }
   names.sort(compare);
  
  
}
/******************************************************************************
bool InitEnd()
{
  
  

save();
load();
  
   return true;
}
/******************************************************************************
void ShutEnd()
{
}
/******************************************************************************
bool UpdateEnd()
{
   if(Kb.bp(KB_ESC))return false;
   return true;
}
/******************************************************************************

void DrawEnd()
{
   D.clear(TURQ);
   REPA(names)
   D.text (0, i*0.1, S + names[i].name+ "   "+ names[i].score); // display data obtained from file
}
State BackEnd(UpdateEnd, DrawEnd, InitEnd,ShutEnd);
/*************************************************************/
[\code]
[code]
(This post was last modified: 02-17-2018 03:54 PM by Otolone.)
02-16-2018 04:48 PM
Find all posts by this user Quote this message in a reply
Zervox Offline
Source Licensee

Post: #7
RE: [SOLVED]How to create a Hiscore Table in EE
Please use the code tags and segment the code.
02-17-2018 02:27 AM
Find all posts by this user Quote this message in a reply
Otolone Offline
Member

Post: #8
RE: [SOLVED]How to create a Hiscore Table in EE
But this code crashes on running it twice.
Version 2.
Code:
/********************************************************************************​******/
Str player_name = "John";
int score = 200;

class PlayerData
{
   Str  name;
   int score;
}
Memc<PlayerData>memc;
/********************************************************************************​******/
int compare(const PlayerData &a, const PlayerData &b )
{
   if(a.score<b.score)return -1;
   if(a.score>b.score)return 1;
   return 0;
}
/*******************************************************************************/
void Save()
{
   File f;
   PlayerData player;
   player.name = Player_name;
   player.score = score;
  
   f.append("hiscore.dat");
   f.put((cptr)&player, sizeof player);
  
}
/******************************************************************************/
void Load()
{
   PlayerData player;
   File f;
  
   f.read("hiscore.dat");
  
   while(!f.end())
   {
      PlayerData &player_Data = memc.New();
      if(f.get((ptr)&player, sizeof player))
      {
        
        player_Data.name = player.name;
        player_Data.score = player.score;
      }      
      
      
   }
  
  
}
/******************************************************************************/
bool InitBackend() // initialize after engine is ready
{
    
   Save();
   Load();
memc.sort(compare);
  
   return true;
}
/******************************************************************************/
void ShutBackend() // shut down at exit
{
   // this is called when the engine is about to exit
}
/******************************************************************************/
bool UpdateBackend() // main updating
{
  

   if(Kb.bp(KB_ESC))return false; // exit if escape on the keyboard pressed
   return true;                   // continue
}
/******************************************************************************/
void DrawBackend() // main drawing
{
   // here you have to tell engine what to draw on the screen

  
   D.clear(TURQ); // clear screen to turquoise color
   REPA(memc)
   D.text (0, i*0.1, S+memc[i].name + "  "+ memc[i].score);
}
State BackEnd(UpdateBackend, DrawBackend, InitBackend,ShutBackend);
[\code][code]

Thanks
(This post was last modified: 02-17-2018 03:57 PM by Otolone.)
02-17-2018 03:55 PM
Find all posts by this user Quote this message in a reply
Otolone Offline
Member

Post: #9
RE: [SOLVED]How to create a Hiscore Table in EE
I am sorry Zervox for udating and posting my code after your suggestions.I am not familiar with the code tags.
Thanks for the suggestion.
(This post was last modified: 02-17-2018 05:03 PM by Otolone.)
02-17-2018 05:00 PM
Find all posts by this user Quote this message in a reply
Zervox Offline
Source Licensee

Post: #10
RE: [SOLVED]How to create a Hiscore Table in EE
When storing an object directly I am pretty sure you have to use fixed length arrays, like Char name[30] to force it to a fixed size, else it will try to read something which isn't there.

I usually make a void Save(File &f) function to the classes when I want to store them to files so that I only have one place to modify and find that specific class' save/load functionality. I personally do not save pointers to file.

could be a bug or it could be intentional.
02-18-2018 02:41 AM
Find all posts by this user Quote this message in a reply
Otolone Offline
Member

Post: #11
RE: [SOLVED]How to create a Hiscore Table in EE
Thanks I will work on that
02-18-2018 05:15 PM
Find all posts by this user Quote this message in a reply
Post Reply