Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: RPG Data Serialization Methods  (Read 3973 times)

0 Members and 2 Guests are viewing this topic.

zachprinz

  • Newbie
  • *
  • Posts: 18
    • View Profile
RPG Data Serialization Methods
« on: August 16, 2013, 12:45:12 am »
I've come to a point in my current project where I will need to start adding "content" (stats on items, dialog for NPC's, skills, etc.) and I have been debating how best to implement the serialization of these values.

I've found a tutorial that uses Lua to manage NPC dialog, but I'm curious as to why they are using Lua instead of c++. It seems like I could just create c++ scripts with the same information that they are typing out in Lua.
Tutorial link below
http://www.raywenderlich.com/30561/how-to-make-a-rpg

I've never used data serialization in a project before (unless you count reading in strings from a txt file) but the first thing I thought of was XML, I've also seen people using SQLite for some stuff. I've read some other things being mentioned like Boost Serialization and Googles protobuf, but pretty much know nothing about those (but for that matter I don't have much knowledge on any of these topics/libraries.)

While I'm fairly sure that I could get everything I need done by simply reading in strings (which I know I could do from a text file.) I've kind of assumed that there must be advantages to using the aforementioned methods which I could take advantage of.

I guess what I'm trying to find out is what benefit these methods would offer someone who is trying to add "content" (in my previously defined usage of the word) for an RPG style game.

I hope my question isn't too vague, thanks for any help.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: RPG Data Serialization Methods
« Reply #1 on: August 16, 2013, 02:18:18 am »
I haven't developed an RPG or anything myself, but my impression is that it makes sense to use a simple high-level scripting language in the context of that tutorial because it's leaning toward making an engine, not just a single RPG.

When designing an engine that other people can use to make a certain kind of game, the idea is to give them a lot of control over their own game while still making it significantly easier than coding the whole game from C++ themselves.  Thus, allowing them to write scripts in a simple scripting language is often a good compromise.

If you've already coded everything in C++ yourself, then I don't know of any reason you'd want to integrate Lua into your game, unless one of the core mechanics is letting the player write code.  Text files with strings should be fine as long as you keep them well-organized and properly encoded/formatted.

Hopefully I'm not about to be told that everything I said is wrong by someone who's actually made an RPG before.

zachprinz

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: RPG Data Serialization Methods
« Reply #2 on: August 16, 2013, 04:11:40 am »
Yeah, I don't think I'm going to use Lua. For the time being I'm looking closer at sqlite. The main reason I'm not using text files Is this entire project has been about learning and I know how to use them already, I'd rather delve into something new.

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
Re: RPG Data Serialization Methods
« Reply #3 on: August 16, 2013, 04:23:57 am »
For something like loading a string of text for the NPC to say, then yes, using Lua is definitely overkill. That article does eventually talks about some of the better use cases for why Lua was used, though. Things like NPC movements, handling quest branches, and enemy AI can all be handled in your scripting language.

But you had mentioned,

Quote
If you've already coded everything in C++ yourself, then I don't know of any reason you'd want to integrate Lua into your game, unless one of the core mechanics is letting the player write code.

Sure, that's definitely something you can have as part of your game, but the main reason people use scripting languages is because they get interpreted when they are loaded, so there is no compiling going on. That means you can make a quick change, and simply run your game again. There are also systems(Wolfire's Overgrowth engine is such an example) that will actually auto-load and make the changes when it detects a change in the script file.

Of course, this is all fine and dandy, but be aware that there is some overhead when running scripting code since it is interpreted at runtime.

As far as other data goes, like items, character stats, etc., how you load that really depends on what you want to interact with. You can use an XML file, a plain text file, a YAML file, a binary file, and the list goes on. If this is your first RPG, use what ever you are comfortable with!
DSFML - SFML for the D Programming Language.

zachprinz

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: RPG Data Serialization Methods
« Reply #4 on: August 16, 2013, 04:47:47 am »
For something like loading a string of text for the NPC to say, then yes, using Lua is definitely overkill. That article does eventually talks about some of the better use cases for why Lua was used, though. Things like NPC movements, handling quest branches, and enemy AI can all be handled in your scripting language.

But you had mentioned,

Quote
If you've already coded everything in C++ yourself, then I don't know of any reason you'd want to integrate Lua into your game, unless one of the core mechanics is letting the player write code.

Sure, that's definitely something you can have as part of your game, but the main reason people use scripting languages is because they get interpreted when they are loaded, so there is no compiling going on. That means you can make a quick change, and simply run your game again. There are also systems(Wolfire's Overgrowth engine is such an example) that will actually auto-load and make the changes when it detects a change in the script file.

Of course, this is all fine and dandy, but be aware that there is some overhead when running scripting code since it is interpreted at runtime.

As far as other data goes, like items, character stats, etc., how you load that really depends on what you want to interact with. You can use an XML file, a plain text file, a YAML file, a binary file, and the list goes on. If this is your first RPG, use what ever you are comfortable with!

I can definitely understand where things being loaded at runtime would be an advantage. I wasn't really sure if Lua had to be compiled. To be honest I didn't know about compiled vs scripting languages.

So the advantage in using Lua with c++ would be I wouldn't have to recompile the game to test and create content like npc dialog or movement. I could have the game/engine coded completely independently from its content. Sounds like a very clean almost encapsulated way of doing things. I'll start learning Lua.

About the RPG, I've been kind of making the same game over and over again, each time moving up languages, improving the classes and organisation of the engine/game and implementing superior methods (like this for example, moving from .txt files to Lua would be awesome!) So far I've made it (to some extent) in java and c#. C++ makes the 3rd language I've written it in.

Any tips for starting with Lua?
« Last Edit: August 16, 2013, 05:00:30 am by zachprinz »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: RPG Data Serialization Methods
« Reply #5 on: August 16, 2013, 09:38:02 am »
Programming in Lua book online on the site is free and is good reference, even if it's for 5.0 and you have to adapt some things. If you're very desperate, want to support Lua and have money to spare you can buy the last book for 5.2.
Also language, standard library, C API and syntax reference is 100% free and up to date.
« Last Edit: August 16, 2013, 10:13:36 am by FRex »
Back to C++ gamedev with SFML in May 2023

zachprinz

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: RPG Data Serialization Methods
« Reply #6 on: August 17, 2013, 01:36:07 am »
I'm having a bit of trouble finding many good tutorials or information on binding Lua to C++. LuaBind hasn't been updated in 3 years which worries me a tad. But it still seems like the best option.

I'm also not sure If I've gotten the whole concept of using a scripting language for logic. My understanding of it as it stands is that I will create a scripts folder in my project folder and put a few more folders in that specifying the type of scripts inside for organization. Inside those I'll make short .lua scripts for each type of enemy/item/etc. These scripts will contain a few methods like OnCreate() to set up the values of the c++ instance of "Enemy"

Quote
RPGProject
  --scripts
     --enemies
       --man.lua
       --goblin.lua
     --items
       --potion.lua
       --food.lua

When I start the game it will load the map (I'm using tiled .tmx files) and through reading the map it will find say an enemy on tile (16,12) with a name value of "man." It will create an instance of "Enemy.cpp" and in the constructor of "Enemy.cpp" it will take the name value and find "scripts/enemies/man.lua" and call the OnCreate() method of that .lua file which will set up the values that go along with the "man" enemy.

I also plan to have an Update() method that will keep track of AI and that particular enemies behavior. The Update() method will be called every time the enemy finished the last task provided by the Update() method.

I just want to make sure I'm on the right path with this.

EDIT: Figured out how to install everything.
« Last Edit: August 17, 2013, 03:31:41 am by zachprinz »