Set bool variables true or false from external script using Lua?
Posted: Tue Mar 29, 2022 6:20 pm
Hi, and thanks so much for this amazing Plugin!
I've read about some similar scenarios in the forums but nothing that quite matches what I'm trying to do, so I'm hoping you can help me (I'm also pretty new to coding so I may be overlooking something very simple here):
In my game, the player can control any one of three player characters at will. Which player character is currently being controlled is set by a custom PlayerSwitcher script. My goal is to add something to that script to set variables within the dialogue system, so that NPC dialogue can branch in different directions depending on which character the player is controlling.
At first I set a boolean for each player in the Dialogue System variables tab (isShael is the default and set to 'true'):
Example: If you're controlling "Nyla," the NPC uses one branch, if you're "Shael" they use the other branch depending on the bool being true or false. (All of this so far seems straightforward).
In my PlayerSwitcher script, I added using PixelCrushers.DialogueSystem, and started by trying SetVariable as follows for each character, referring directly to the name of the variable:
That didn't work, so I watched the tutorial video on connecting C# methods to Lua, and used the script template to make another script called RegisterLua that's attached to the Dialogue Manager. I'm not sure exactly how to handle a boolean in this case- the video showed a string that returned a bool value- so perhaps I've got the syntax wrong or am missing an argument:
Since it looks like you have to register "PlayerIsNyla" in this format instead of the actual names of the variables I updated the SetVariable as follows for each character:
The variables still weren't changing during gameplay, and the NPC still gave no dialogue, so next I tried creating custom Lua functions from the asset menu instead of trying to access the ones in the Variables tab:
...and updated the conditions in the dialogue tree to use Custom.
The result was the same, the boolean isn't changing when SetVariable is triggered in the PlayerSwitcher script, and the NPC gives no dialogue. So I'm obviously missing something but I'm not sure where. It's also possible there's an easier/better way to set variables from outside scripts that I'm overlooking.
Any help would be appreciated. Thanks very much!
I've read about some similar scenarios in the forums but nothing that quite matches what I'm trying to do, so I'm hoping you can help me (I'm also pretty new to coding so I may be overlooking something very simple here):
In my game, the player can control any one of three player characters at will. Which player character is currently being controlled is set by a custom PlayerSwitcher script. My goal is to add something to that script to set variables within the dialogue system, so that NPC dialogue can branch in different directions depending on which character the player is controlling.
At first I set a boolean for each player in the Dialogue System variables tab (isShael is the default and set to 'true'):
Example: If you're controlling "Nyla," the NPC uses one branch, if you're "Shael" they use the other branch depending on the bool being true or false. (All of this so far seems straightforward).
In my PlayerSwitcher script, I added using PixelCrushers.DialogueSystem, and started by trying SetVariable as follows for each character, referring directly to the name of the variable:
Code: Select all
DialogueLua.SetVariable("isNyla", true);
Code: Select all
using UnityEngine;
using PixelCrushers.DialogueSystem;
public class RegisterLua : MonoBehaviour
{
public bool isNyla;
public bool isShael;
public bool isDwen;
public bool PlayerIsNyla()
{
return isNyla;
}
public bool PlayerIsShael()
{
return isShael;
}
public bool PlayerIsDwen()
{
return isDwen;
}
void OnEnable()
{
Lua.RegisterFunction("PlayerIsNyla", this, SymbolExtensions.GetMethodInfo(() => PlayerIsNyla()));
Lua.RegisterFunction("PlayerIsShael", this, SymbolExtensions.GetMethodInfo(() => PlayerIsShael()));
Lua.RegisterFunction("PlayerIsDwen", this, SymbolExtensions.GetMethodInfo(() => PlayerIsDwen()));
}
void OnDisable()
{
Lua.UnregisterFunction("PlayerIsNyla");
Lua.UnregisterFunction("PlayerIsShael");
Lua.UnregisterFunction("PlayerIsDwen");
}
}
Code: Select all
DialogueLua.SetVariable("PlayerIsNyla", true);
...and updated the conditions in the dialogue tree to use Custom.
The result was the same, the boolean isn't changing when SetVariable is triggered in the PlayerSwitcher script, and the NPC gives no dialogue. So I'm obviously missing something but I'm not sure where. It's also possible there's an easier/better way to set variables from outside scripts that I'm overlooking.
Any help would be appreciated. Thanks very much!