// Copyright (c) Pixel Crushers. All rights reserved.
namespace PixelCrushers.DialogueSystem
{
///
/// A user variable asset. Chat Mapper allows you to define your own user variables that you
/// reference in dialogue entry conditions and user scripts. This class represents those user
/// variables in the dialogue system. As with Chat Mapper projects, a dialogue database
/// contains a table of user variables (named "Variable[]") that you can access in Lua.
///
[System.Serializable]
public class Variable : Asset
{
///
/// Gets or sets the initial string value of a dialogue variable.
///
///
/// The initial value.
///
public string InitialValue
{
get { return LookupValue(DialogueSystemFields.InitialValue); }
set { Field.SetValue(fields, DialogueSystemFields.InitialValue, value); }
}
///
/// Gets or sets the initial float value of a dialogue variable. Use this when the data
/// type is FieldType.Number.
///
///
/// The initial float value.
///
public float InitialFloatValue
{
get { return LookupFloat(DialogueSystemFields.InitialValue); }
set { Field.SetValue(fields, DialogueSystemFields.InitialValue, value); }
}
///
/// Gets or sets the initial bool value of a dialogue variable. Use this when the data type
/// is FieldType.Boolean.
///
///
/// The initial bool value.
///
public bool InitialBoolValue
{
get { return LookupBool(DialogueSystemFields.InitialValue); }
set { Field.SetValue(fields, DialogueSystemFields.InitialValue, value); }
}
///
/// Gets the data type of the variable based on the data type of its initial value.
///
///
/// The type.
///
public FieldType Type
{
get { return LookupInitialValueType(); }
set { SetInitialValueType(value); }
}
///
/// Initializes a new Variable.
///
public Variable() { }
///
/// Copy constructor.
///
/// Source variable.
public Variable(Variable sourceVariable) : base(sourceVariable as Asset) { }
///
/// Initializes a new Variable copied from a Chat Mapper user variable.
///
///
/// The Chat Mapper user variable.
///
public Variable(ChatMapper.UserVariable chatMapperUserVariable)
{
Assign(chatMapperUserVariable);
}
///
/// Copies a Chat Mapper user variable asset.
///
///
/// The Chat Mapper user variable.
///
public void Assign(ChatMapper.UserVariable chatMapperUserVariable)
{
if (chatMapperUserVariable != null)
{
Assign(0, chatMapperUserVariable.Fields);
// Chat Mapper 1.6 XML lists type of bools as "Number". This fixes it:
Field initialValue = Field.Lookup(fields, DialogueSystemFields.InitialValue);
if ((initialValue != null) &&
(initialValue.type == FieldType.Number) &&
(string.Equals(initialValue.value, "True", System.StringComparison.OrdinalIgnoreCase) || (string.Equals(initialValue.value, "False", System.StringComparison.OrdinalIgnoreCase))))
{
initialValue.type = FieldType.Boolean;
}
}
}
///
/// Returns the data type of the initial value.
///
///
/// The data type of the Initial Value field.
///
private FieldType LookupInitialValueType()
{
Field initialValue = Field.Lookup(fields, DialogueSystemFields.InitialValue);
return (initialValue == null) ? FieldType.Text : initialValue.type;
}
///
/// Sets the data type of the initial value.
///
///
/// A variable type.
///
private void SetInitialValueType(FieldType type)
{
Field initialValue = Field.Lookup(fields, DialogueSystemFields.InitialValue);
if (initialValue != null)
{
initialValue.type = type;
initialValue.typeString = Field.GetTypeString(type);
}
}
}
}