Condition check throws error for not declared vars

Announcements, support questions, and discussion for the Dialogue System.
User avatar
Abelius
Posts: 318
Joined: Fri Jul 21, 2017 12:45 pm

Condition check throws error for not declared vars

Post by Abelius »

I had this question in my head for some time...

Please note my old version. I don't know if this changed, but in case it didn't, here's my little issue.

I've observed that if I check the value of a non-yet-declared variable, DS throws an error and stops executing that entry, preventing the conversation from going through that conditional node and its desired dialogue branch afterward.

I find this a bit inconvenient because I often just want to declare a variable in a conversation without the need to prefill that variable to the already huge list of pre-declared variables in the Variables tab.

In other words, it would be great if DS initialized on-the-fly variables that can't be found when a conditional asking for their value is run, instead of giving that error.

It might sound weird, but I commonly find myself desiring this behavior. For example, I may want to set a "Variable["LotteryWon"] = true" in the Script field of a given entry that may or may not be run before it gets checked on another entry.

I'm aware the solution to this is just to prefill that variable on the Variables tab or run "Variable["LotteryWon"] = false" in an initialization conversation at the start of the game. That's what I'm doing.

However, I'd very much prefer if DS would just assume a non-existent variable needs to be initialized to its default value (false bool, empty string, 0 number...) and keep parsing the Conditions field as normal.

If you think this would be a too big change in behavior for everyone using the asset, would it be possible to have a script to change it on demand?

Thank you.
Unity 2019.4.9f1
Dialogue System 2.2.15
User avatar
Tony Li
Posts: 22110
Joined: Thu Jul 18, 2013 1:27 pm

Re: Condition check throws error for not declared vars

Post by Tony Li »

Hi,

You can check Variable["foo"] without predefining "foo" in your dialogue database's Variable section. Just keep in mind that its value will be nil, not false. That's why you'll often see this in examples:

Code: Select all

Variable["foo"] ~= true
instead of:

Code: Select all

Variable["foo"] == false
The former checks if foo is not true (i.e., false or nil). The latter only checks if foo is false.

You can't do this with nested tables, though. So if you don't have an actor named "Abelius", you can't do this:

Code: Select all

-- Not valid if Abelius isn't defined:
Actor["Abelius"].Money = 1000000
You'd need to do this:

Code: Select all

Actor["Abelius"] = {};
Actor["Abelius"].Money = 1000000
or:

Code: Select all

Actor["Abelius"] = { Name = "Abelius", Money = 1000000 }
User avatar
Abelius
Posts: 318
Joined: Fri Jul 21, 2017 12:45 pm

Re: Condition check throws error for not declared vars

Post by Abelius »

Ah... nice trick.

So, every undeclared variable wouldn't have a 'type', I guess? Always 'nil' (null?) instead.
Unity 2019.4.9f1
Dialogue System 2.2.15
User avatar
Tony Li
Posts: 22110
Joined: Thu Jul 18, 2013 1:27 pm

Re: Condition check throws error for not declared vars

Post by Tony Li »

Yes, that's correct.
User avatar
Abelius
Posts: 318
Joined: Fri Jul 21, 2017 12:45 pm

Re: Condition check throws error for not declared vars

Post by Abelius »

Just a follow-up question...

I've decided it's gonna be simpler to have a script that initializes all 'auxiliary' variables in my game when it starts. For that effect, I've created a table component, similar to the Variables tab, that has a method that goes through each item to Set all variables at Start(). I do this because I don't want to clutter the Variables tab with not-so-important vars.

However, I'm wondering if this will be enough when I load a game (I'm using EasySave3) using 'Apply Savegame Data' PlayMaker action. I have 'Initialize New Variables' checked, but I don't really know what it does. I'm assuming it goes through all variables in the Variables tab and Sets them to their default value (false, empty, 0) if they aren't found in the savegame data string when loading a game?

If that is the case, then I guess I'll be good as long as the variables exist at runtime by having being set at the start, right?
Unity 2019.4.9f1
Dialogue System 2.2.15
User avatar
Tony Li
Posts: 22110
Joined: Thu Jul 18, 2013 1:27 pm

Re: Condition check throws error for not declared vars

Post by Tony Li »

Abelius wrote: Mon Oct 21, 2024 6:30 amI have 'Initialize New Variables' checked, but I don't really know what it does. I'm assuming it goes through all variables in the Variables tab and Sets them to their default value (false, empty, 0) if they aren't found in the savegame data string when loading a game?
Yes, that's exactly correct.
User avatar
Abelius
Posts: 318
Joined: Fri Jul 21, 2017 12:45 pm

Re: Condition check throws error for not declared vars

Post by Abelius »

But does it do that for the variables added at runtime? Not just the ones prefilled on the Variables tab? Just to confirm.
Unity 2019.4.9f1
Dialogue System 2.2.15
User avatar
Tony Li
Posts: 22110
Joined: Thu Jul 18, 2013 1:27 pm

Re: Condition check throws error for not declared vars

Post by Tony Li »

I'm not sure I understand. After restoring a saved game state, "Initialize New Variables" will only go through the dialogue database's Variables list. If any of the variables in the list are not defined in Lua, it will define them in Lua with their Initial Values.
User avatar
Abelius
Posts: 318
Joined: Fri Jul 21, 2017 12:45 pm

Re: Condition check throws error for not declared vars

Post by Abelius »

I mean, I'm picturing a scenario where I release a new game version with new variables, and the player loads an old save file.

With the new variables already prefilled on the Variables tab, I know they will be 'added' to the Lua environment with their initial values because 'Initialize New Variables' is checked. Then those variables won't throw an error when checking their values and will be stored when the player saves the game. All good.

However, my now desired approach is to have all those new variables declared when the game runs before the player can load an old save. They won't show as prefilled in the Variables tab.

So, my theory is that even if they aren't there, as they have been Set before the ApplySaveData() method on game start (title screen), all the missing variables from the savegameData string will still exist afterwards, right?

Unless ApplySaveData (with the reset option false) deletes any variable it can't find both on the savegameData string or the Variables tab, ofc.

That's my question: if all the variables that exist during that method are preserved regardless of how they were defined.
Unity 2019.4.9f1
Dialogue System 2.2.15
User avatar
Tony Li
Posts: 22110
Joined: Thu Jul 18, 2013 1:27 pm

Re: Condition check throws error for not declared vars

Post by Tony Li »

No, unfortunately in your case they're not preserved. This prevents old, obsolete variables from sticking around after loading a saved game. The only variables that will exist after ApplySavedGameData() are the variables in the saved game data and the newly-initialized variables from "Initialize New Variables".
Post Reply