Adventure Creator Cutscenes and Variable changes

Announcements, support questions, and discussion for the Dialogue System.
mrcsbmr
Posts: 31
Joined: Mon Mar 21, 2016 5:26 am

Adventure Creator Cutscenes and Variable changes

Post by mrcsbmr »

Hi,

I have a problem with Dialogue System and Adventure Creator. I'm changing a variable within a dialogue. Usually, that works. When the dialogue ends, the Adventure Creator variables get updated, everything's set up properly, However, when I call a cutscene in the dialogue that changes the variable, the Adventure Creator variables are not updated when the dialogue ends. I change the variable AFTER the cutscene ran, the dialogue looks something like this:

Player: "Line A"
Player: "Line B"
Player: "" / Sequence: AC(TheCutscene)
Player: "Line C" / Script: Variable["VariableName"] = true;
NPC: "Line D"
Player: "Line E"

When I delete the fragment with the Cutscene, it works. When the cutscene is called, it doesn't, no matter what's happening in the cutscene and no matter if the cutscene exists or not.

Any ideas why?
mrcsbmr
Posts: 31
Joined: Mon Mar 21, 2016 5:26 am

Re: Adventure Creator Cutscenes and Variable changes

Post by mrcsbmr »

Weirdly, it works if we put the Script in one line later:

Player: "Line A"
Player: "Line B"
Player: "" / Sequence: AC(TheCutscene)
Player: "Line C"
NPC: "Line D" / Script: Variable["VariableName"] = true;
Player: "Line E"
User avatar
Tony Li
Posts: 21055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Adventure Creator Cutscenes and Variable changes

Post by Tony Li »

Hi,

A couple things might be going on.

When the AC() sequencer command starts, it copies Dialogue System variable values into their corresponding Adventure Creator variables. When the command ends, it copies the Adventure Creator variable values back into the Dialogue System. If you suspect this is causing the problem, I recommend changing your AC cutscene's action to change the AC variable, not the Dialogue System variable. The documentation in the next version of the Dialogue System clarifies this point.

If that's not the issue, keep in mind that the Dialogue System has to evaluate Conditions one level ahead. (See here for an explanation.) If "Line C" has a condition based on the variable set in the previous line's AC cutscene, you'll need to add an empty spacer node between them to delay evaluation.

If that doesn't help, please feel free to send an example scene to tony (at) pixelcrushers.com. I'll be happy to take a look.

Best regards,
Tony
mrcsbmr
Posts: 31
Joined: Mon Mar 21, 2016 5:26 am

Re: Adventure Creator Cutscenes and Variable changes

Post by mrcsbmr »

Hi Tony,

thanks for your response. Our problem is indeed that
a) changes to variables directly after cutscenes are not possible and need to happen one line later

Line A
Line B calls AnyCutscene
Line C changes variable
Line D

does not work, whereas
Line A
Line B calls AnyCutscene
Line C
Line D changes variable

works.

and, a similar issue:
b) changes to items in the AC inventory within cutscenes called from DialogueSystem are only possible when another line of dialogue follows.

Line A
Line B calls TheCutsceneThatAddsAnInventoryItem

does not work, whereas
Line A
Line B calls TheCutsceneThatAddsAnInventoryItem
Line C

works.

This is a little annoying and also pretty error-prone. :| I see the changes to variables and items only get processed correctly during a following line of dialogue. Do you have any idea how to approach this problem other than adding empty lines, maybe with a custom sequencer action that takes care of handling item/variable changes at a custom time?

[edit] I found out that b) can be solved by adding a Dialogue System Lua Action to the TheCutsceneThatAddsAnInventoryItem and check the Sync Data checkbox.

Thanks.
User avatar
Tony Li
Posts: 21055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Adventure Creator Cutscenes and Variable changes

Post by Tony Li »

I think I can provide a quick fix later today.

Here's what's happening under the hood:

Let's consider an Adventure Creator variable "X". It starts with the value 42.

1. When the conversation starts, the AdventureCreatorBridge copies the values of Adventure Creator's variables into the Dialogue System's variable table. So now AC's "X" and DS's Variable["X"] both equal 42.

2. When the AC() sequencer command runs, the first thing it does is copy the Dialogue System's variables back to Adventure Creator. Both still equal 42.

3. When the AC() sequencer commands finishes, it copies AC's values back to DS.

4. However, on the very same frame, the next dialogue entry starts. Let's say this dialogue entry sets Variable["X"]=99. So two operations happen in this frame:
  • A. The AC() sequencer command copies the AC values back to DS (setting Variable["X"] to 42), and
  • B. The next entry's Script sets Variable["X"] to 99.
This is a race condition. Unity generally doesn't guarantee the order that scripts run in a frame. So:
  • If (A) runs first and (B) runs second, Variable["X"] will be 99 (set by the Script).
  • If (B) runs first and (A) runs second, Variable["X"] will be 42 (copied from the AC() command).
(5. Included just for completeness since the damage has already been done in #4 above: When the conversation ends, the AdventureCreatorBridge copies the DS's values back to AC.)

It's important that the previous dialogue entry finishes on the same frame that the next one starts. This prevents flicker in some cases.

Would it work for you if the AC() sequencer command had an option to not copy the AC values back to DS at the end?

Another option would be to provide Lua functions that you could use in your Script fields. So instead of this:
  • Script:

    Code: Select all

    Variable["VariableName"] = true
They would look like this:
  • Script:

    Code: Select all

    SetBool("VariableName", true)
This command would set the AC and DS values at the same time so there would never be any syncing issues.
mrcsbmr
Posts: 31
Joined: Mon Mar 21, 2016 5:26 am

Re: Adventure Creator Cutscenes and Variable changes

Post by mrcsbmr »

Since we're working with Articy Draft and have a big project it's not so easy to always see when one would need to prevent the system from copying the values back to DS. We now solved it this way: The DialogueManager with the ACBridge has an additional script that ensures scripts from dialogue lines are executed (again) after the AC values got copied back to DS:

Code: Select all

	
	public  void OnConversationLine(Subtitle subtitle)
	{
		if (subtitle.dialogueEntry.userScript != "")
		{
			cAdventureCreatorBridge.SyncAdventureCreatorToLua(); 
			Lua.Run(subtitle.dialogueEntry.userScript);
			cAdventureCreatorBridge.SyncLuaToAdventureCreator(); 		
		}
	}


I guess the ACBridge is the script that handles all the syncing which means we couldn't ensure the order of those events using the Unity Script Execution Order settings?
User avatar
Tony Li
Posts: 21055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Adventure Creator Cutscenes and Variable changes

Post by Tony Li »

I'll dig into the script execution order for SequencerCommandAC.cs (which syncs AC to Lua in OnDestroy) and ConversationModel (which runs the Lua Script) and get back to you.

If it's not possible to adjust the script execution order to work the way you need, how about if the AC() sequencer command checks if the dialogue entry has a script and automatically runs Lua.Run after syncing? I could make this the default behavior, with an option to run not the script if specified.
User avatar
Tony Li
Posts: 21055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Adventure Creator Cutscenes and Variable changes

Post by Tony Li »

An updated Adventure Creator Support package on the Dialogue System Extras page adds a new checkbox "Rerun Script After Cutscenes" to the Adventure Creator Bridge that resolves this race condition.
mrcsbmr
Posts: 31
Joined: Mon Mar 21, 2016 5:26 am

Re: Adventure Creator Cutscenes and Variable changes

Post by mrcsbmr »

Thank you!
User avatar
Tony Li
Posts: 21055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Adventure Creator Cutscenes and Variable changes

Post by Tony Li »

Happy to help!
Post Reply