Sign in to follow this  
DontPanic

c++ dynamically loading variables and boolean expressions from file

Recommended Posts

For part of my game I need to load data in at real time. Like the ID assuming this is from a database I want to search "wealthyWerewolf", conditions "if(playerName == "bob" && wealth == 100)" and also load assignments such a "hairy = true; increaseHeight += 2". I'm making the example stuff up but it should make sense. If I was to be saving this in a txt file they could just go on separate lines for the sake of simplicity: wealthyWerewolf if(playerName == "bob" && wealth == 100) hairy = true; increaseHeight += 2 (use of ; as delimiter doesn't matter too much, if stored in XML I could presumably separate things by line and it'll still be in tags) The idea is that a file could be searched for wealthyWerewolf, load and parse the if statement, anything that the player doesn't have as a variable or hasn't been dynamically created earlier will return false. So if the player didn't have a name, no problems, it's clearly false. If true however then it would parse the next line that holds assignments (if it isn't there, then it can't be changed). Quite a lot of that can be changed as long as it has the same functionality but that is how I envisioned it. I've been around the block with this problem from trying to implement strings into a maths expression parser, create my own scripting language, researching for days. It felt like I was reinventing the wheel at every step. Heaven if I was a masochist but I bailed each time because this surely isn't needing that much work. Someone would have done it before me. Right now I'm looking at Lua knowing that it can handle my requirements but I'm brand new at this game and could do with a poke in the right direction. Keep feeling like I should be storing these as strings like the example, loading them in Lua and parsing there... same problem if I'm not mistaken. Coding hangover from so much looking into compiler coding etc. Ok enough mind dumping, thanks for reading. I'm taking all suggestions, links to libraries, perhaps poking me in the right direction with a scripting language as I'd be new to it, anything really.

Share this post


Link to post
Share on other sites
Yes, C++ does not offer such a capability. That's what scritping is for. A scripting language is a library that reads in text at run time, parses it, and executes it in the context of running code (which may be C++).

You are going in the right direction by thinking of using Lua as a scripting language embedded in your application.

You need to read up on how to embed the scripting engine and how to link variable between the scripts and the program.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this