How to save player's choices to Mysql database?

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
cozysmommy
Posts: 7
Joined: Wed Sep 27, 2023 7:53 pm

How to save player's choices to Mysql database?

Post by cozysmommy »

Hi Tony,
I'm conducting a research on educational games, the Dialogue System offers me a great opportunity to design & develop the whole game. But I'm having some problems saving player's choices to Mysql database.
1. The documentation only describe how to save items like quest state, conversation state or object's position, but I want to record player's choices on each question, whether that is the right answer or not, so that I can analyze player's learning path and cognitive process in-depth. How can I do that?
2. Let's say if I've managed to solve the first problem, how can I save the choices and the matched question to Mysql database?
I'm not very good at programming, does there exist any integrated tool to solve those problem? Thanks. :)
User avatar
Tony Li
Posts: 22108
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to save player's choices to Mysql database?

Post by Tony Li »

Hi,

The Dialogue System doesn't have built-in MySQL integration, so you will need to do at least a little bit of programming.

I recommend this approach:

1. Add a custom Boolean field named "Correct" to your dialogue database's dialogue entry template, and tick the Main checkbox:

correctField.png
correctField.png (48.06 KiB) Viewed 1042 times

2. In your player response dialogue entries (the blue nodes), set the Correct field to true for the correct responses and false for the incorrect responses:

correctAnswer.png
correctAnswer.png (10.73 KiB) Viewed 1042 times

3. Add a script with an OnConversationLine(Subtitle) method to your Dialogue Manager GameObject. In this script, check if the line is a player line. If so, check its "Correct" field and record the value to your MySQL database. Example:

Code: Select all

using UnityEngine;
using PixelCrushers.DialogueSystem;
public class RecordCorrectAnswers : MonoBehaviour
{
    public string questionID;
    
    void OnConversationLine(Subtitle subtitle)
    {
        if (string.IsNullOrEmpty(subtitle.formattedText.text)) return;
        
        if (subtitle.speakerInfo.isNPC)
        {
            // If this is an NPC line (i.e., question, not answer), record a questionID to use when the player answers:
            questionID = $"{subtitle.dialogueEntry.conversationID}:{subtitle.dialogueEntry.id}";
            // Alternate: questionID = subtitle.dialogueEntry.Title;
        }
        else
        {
            // Otherwise this is a PC response line, so determine whether it's a correct or incorrect answer:
            bool isCorrect = Field.LookupBool(subtitle.dialogueEntry.fields, "Correct");
        
            // Then record it: (Here we just log it to the Console. In your case, log it to MySQL.)
            Debug.Log($"Player answered {questionID} correctly? {isCorrect}");
        }
    }
}
In the example above, the question is identified by its conversation ID and entry ID. I also provided alternate code that could identify it by the question's Title field if you prefer.
cozysmommy
Posts: 7
Joined: Wed Sep 27, 2023 7:53 pm

Re: How to save player's choices to Mysql database?

Post by cozysmommy »

Thanks Tony! I'll try this approach.
User avatar
Tony Li
Posts: 22108
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to save player's choices to Mysql database?

Post by Tony Li »

Glad to help!
levexis
Posts: 4
Joined: Thu Nov 30, 2023 11:10 am

Re: How to save player's choices to Mysql database?

Post by levexis »

How did you get on with this? I'm thinking of using the framework for psychological research and saving responses is vital. I'm just looking to save to a text database as I'm running on the quest 2. Might want to log using a web service but can never trust the wifi at uni! Oh and I'd only really be interested in the responses to questions where there was more than one answer option (ie not instructions).
Post Reply