• Advertisement
Sign in to follow this  

Binary I/O for first Rpg

This topic is 2808 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys!

I´m learning c++ now for a few month and after looking into SDL a bit, I decided to try my first game - a text based rpg.
First of all I wanted to get text on the screen witch worked out quite well (wrote my own word-wrapping). Next I wanted to get some way to store my in-game dialogs to file and this is were I´m stuck at the moment...

I thougt about something like a linked list with multiple pointers to the next element, to enable multiple-choice dialogs. This dialog would be stored in a binary file (for this i would write a tool), and read out of the file during the game.

so the question is: does someone know a tutorial on binary I/O that shows more than function prototypes? How did you implement dialogs in your game? :)

tanks a lot in advance,

ps: english is not my mother tounge ;)

Share this post

Link to post
Share on other sites
By dialogues do you mean something like the characters in games like morrowind/oblivion? Where they have different possible text communications (which in those cases may be linked to voice resources too) that are called upon depending on the conditions such as quest index, your stats, your faction affiliations etc.

If you have morrowind/oblivion I suggest looking at the construction kits because you can look at how they handle the different topics of conversation and get an idea of what variables might need to be stored per character/topic.

What you want to do is quite a nice task, it is something that a lot of games do. (think about mmos and such)

For example, the object oriented model would call upon a "talk" method when you engage in conversation. That would open up the dialogue and the topics of conversation linked to that character (applied based on the region, class, race and time of day etc) would be read from the back-end resources and it would provide a response such as unlocking another topic, adding a quest (with index 0 to represent current progress, so other characters can become involved when they check that quest), giving the player an item, taking an item away from the player (maby you had to go and get a Grail or collect X resource items?) etcetera.

You can design a format based on the requirements of the gameplay, and form a text file with file i/o functions.

You want to think about fopen, fread, fwrite and fclose when handling a file format. They operate byte by byte and you can use bitwise operators to encode integer values and work with strings in the file. You can even make an encryption/decryption to handle the format so that players can't cheat: "Oh, if I talk to the king at level 20 I get a cool quest! With a 500 gold reward!".

A hint: you can store an unsigned short (0-65535) in 2 bytes as a string. For example "AA" would be 2x char datatypes of value 65. The least significant byte (the second A perhaps) would be 65 and the most significant byte would be 65 multiplied by 256, ie AA equates to 16705 (65*256 + 65) as an unsigned short. The value 0 however would be represented by null characters (which ends a string) so that is something to think about.

It is up to you how you want to arrange what is in each text file. You might have a file for each character or a big file with all the characters in it. Line endings could separate each possible topic of conversation or each character. Another possibility is you "cheat" and get an XML library to handle that for you but I feel you want to get to grips with the low level aspect more?

cplusplus.com has instructions on how to use fread. Though it is quite brutal in assuming you understand the formats and general C usage.

There will be quite a learning curve to getting this working well.
Here are some friendly tutorials:
Tutorial on cprogramming.com
suite101.com fread Tutorial
Random Access File Handling <-- random access because the data required at any point in time (the player has spoken to the shopkeeper for example) could be anywhere in the file, you aren't reading big chunks at a time like you would be if you were reading a .bmp file for example. Though loading it all into RAM in a clean data structure would be a good way to do it.

hint: you can define a struct and its direct binary equivalent can be written and read cleanly.

Character dialogue is one thing to think about. But when it comes to binary i/o you can make level formats which hold entity lists and geometry information along with environment parameters, all file formats use similar knowledge. Have a shot at making a .bmp with binary file i/o - I ran into some (silly) problems trying that, I have a thread here on gamedev.net discussing the issues which has some source code you can look at.

wrote my own word-wrapping
Well done, I have been too lazy to tackle that as of yet and I know I should get round to it soon.

Is that the kind of info you were looking for? I'd be happy to help with any further discussion.

Share this post

Link to post
Share on other sites
thank you for the links...
I will have a look at them and on the Morrowind Set!

I just couldn't understand the hint with the "AA" ... but maybe after some tutorials on strings, variables and binary ;)

any other thougts on this topic?

greets jan

Share this post

Link to post
Share on other sites
Oops sorry, that AA thing was a bit over the top. You can just read 2 bytes as a ushort anyway without having to mess about like that. Remember to organise the data with structs and it ties in nicely.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement