8 Replies - 1093 Views - Last Post: 22 December 2016 - 08:10 PM Rate Topic: -----

#1 zerophase  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 07-May 13

Is there a means of storing code in a sqlite database?

Posted 22 December 2016 - 12:05 AM

I'm developing a dialogue system with branching paths based on conditions. Currently, I have everything working correctly with lamdas being run when needed, and printing text when required. But, the only problem is it's going to lead to a huge explosion of tons of tiny subclasses, per dialogue.

Example:
typedef TFunction<FName(void)> Lambda;
Lambda c_0 = [this](void)->FName{ return (gameTime->Hour() < 19) ? TEXT("0x01000001000000D4") : TEXT("0x01000001000000E2");};

dialogues.Emplace(TEXT("0x01000001000000DC"), FDialogueNode(c_0)); // dictionary of dialogues



What I'd like to do is use a sqlite database to store all of the dialogues, and retrieve the current dialogue tree by passing an enum or string to the Dialogue class. From there I'd dump everything into a dictionary, so I don't have to hit the database each time I need to retrieve the next fragment of dialogue. The lambdas that will get run are things like checking the current time in game, seeing if a specific item is owned by the player, checking against their current health, etc.

So, my question is is there a means of storing code in the database? Is a user defined function correct for this usage?

Is This A Good Question/Topic? 0
  • +

Replies To: Is there a means of storing code in a sqlite database?

#2 jon.kiparsky  Icon User is offline

  • Chinga la migra
  • member icon


Reputation: 10721
  • View blog
  • Posts: 18,354
  • Joined: 19-March 11

Re: Is there a means of storing code in a sqlite database?

Posted 22 December 2016 - 12:23 AM

I suppose it's possible there's a good reason to store code in a database, but I can't think of one. Databases are a great place to store data - the name sort of says it all.
Was This Post Helpful? 0
  • +
  • -

#3 zerophase  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 07-May 13

Re: Is there a means of storing code in a sqlite database?

Posted 22 December 2016 - 12:32 AM

The problem is if I can't store lambdas, or there isn't a means of converting lambdas to a sql function I'd still have a ton of subclasses, defeating the point of the database in this case.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Chinga la migra
  • member icon


Reputation: 10721
  • View blog
  • Posts: 18,354
  • Joined: 19-March 11

Re: Is there a means of storing code in a sqlite database?

Posted 22 December 2016 - 01:38 AM

Why do you want to store lambdas? Or, better to ask, what is it that you're actually trying to accomplish? As one of my mentors used to say when I went down a rabbit hole, don't talk to me about code, talk to me about what you're trying to do.
Was This Post Helpful? 0
  • +
  • -

#5 zerophase  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 07-May 13

Re: Is there a means of storing code in a sqlite database?

Posted 22 December 2016 - 01:46 AM

What I'm trying to accomplish is handling branching paths in dialogues based on conditions. For instance if you have 5 apples the npc response will be "I see you have the 5 apples I asked for." while if you don't he'll say something like "I need 5 apples to open the gate". So, I need something in the database that'll return the correct dialogue fragment ID based on the current game state.

This post has been edited by zerophase: 22 December 2016 - 01:48 AM

Was This Post Helpful? 1
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Chinga la migra
  • member icon


Reputation: 10721
  • View blog
  • Posts: 18,354
  • Joined: 19-March 11

Re: Is there a means of storing code in a sqlite database?

Posted 22 December 2016 - 08:23 AM

Okay, that's not an unusual sort of problem. The typical solution is to use what's called a "template string" or a "format string" - this is a string which includes some "placeholders" which can be populated from some context. This mechanism exists in most high-level languages, although the solution varies in power and expressiveness from language to language. Depending on what you have available to you in your language of choice, you might store something simple like "%d %s a-%sing" and populate it from a simple list context like [10, "lords", "leap"] or you might have something like "{qty} {gift.pluralize} a-{gift.verb}ing" which would be populated from a dict context like {"qty":10, "gift": lord}, where lord is an object which has a pluralize function and a verb parameter.

You might look at this and thing the format string is a little like code, and you'd be right, since it does have a little bit of a language which the machine has to interpret, but in your program it's treated more as data for some combining operator to work on.

Hope this helps!
Was This Post Helpful? 0
  • +
  • -

#7 zerophase  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 07-May 13

Re: Is there a means of storing code in a sqlite database?

Posted 22 December 2016 - 08:32 AM

Thanks for the help. That would definitely work in specific situations. There might be a few edge cases where I need actual logic still.

It's not just that I have to change the amount of apples being listed in the dialogue. It could be something, like if the player has less than 50 health he gets offered a different quest.

This post has been edited by zerophase: 22 December 2016 - 08:47 AM

Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Chinga la migra
  • member icon


Reputation: 10721
  • View blog
  • Posts: 18,354
  • Joined: 19-March 11

Re: Is there a means of storing code in a sqlite database?

Posted 22 December 2016 - 12:50 PM

View Postzerophase, on 22 December 2016 - 10:32 AM, said:

It's not just that I have to change the amount of apples being listed in the dialogue. It could be something, like if the player has less than 50 health he gets offered a different quest.


Okay, so that sounds like a different sort of an action. At some point, the actions available to the player depend on some state variable - this isn't just about what gets displayed to the player, it's about what the game is doing at that point.
I think I'd have to see more about how you're structuring this, but it sounds like you may have gone off the right path at some point. (usually, wanting to put code into a db is a red flag - means you need to go back and think about what you've done to make this seem like a good idea)
Was This Post Helpful? 0
  • +
  • -

#9 zerophase  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 07-May 13

Re: Is there a means of storing code in a sqlite database?

Posted 22 December 2016 - 08:10 PM

I'm trying to store the nodes of a binary tree in a database. So, I'm generating the code from documentation on the dialogues, and want to clean up the amount of classes required by using a database to store the ids. I probably need some type of ORM soultion, and part of the db will be storing binary data.
Here's the gist, with psuedo code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1