You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
3.5 KiB
100 lines
3.5 KiB
8 years ago
|
// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus).
|
||
|
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE)
|
||
9 years ago
|
|
||
10 years ago
|
using UnityEngine;
|
||
|
|
||
|
namespace Fungus
|
||
|
{
|
||
8 years ago
|
/// <summary>
|
||
|
/// Loads a saved value and stores it in a Boolean, Integer, Float or String variable. If the key is not found then the variable is not modified.
|
||
|
/// </summary>
|
||
8 years ago
|
[CommandInfo("Variable",
|
||
|
"Load Variable",
|
||
|
"Loads a saved value and stores it in a Boolean, Integer, Float or String variable. If the key is not found then the variable is not modified.")]
|
||
|
[AddComponentMenu("")]
|
||
|
public class LoadVariable : Command
|
||
|
{
|
||
|
[Tooltip("Name of the saved value. Supports variable substition e.g. \"player_{$PlayerNumber}\"")]
|
||
8 years ago
|
[SerializeField] protected string key = "";
|
||
10 years ago
|
|
||
8 years ago
|
[Tooltip("Variable to store the value in.")]
|
||
|
[VariableProperty(typeof(BooleanVariable),
|
||
|
typeof(IntegerVariable),
|
||
|
typeof(FloatVariable),
|
||
|
typeof(StringVariable))]
|
||
|
public Variable variable;
|
||
10 years ago
|
|
||
8 years ago
|
public override void OnEnter()
|
||
|
{
|
||
|
if (key == "" ||
|
||
|
variable == null)
|
||
|
{
|
||
|
Continue();
|
||
|
return;
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
Flowchart flowchart = GetFlowchart();
|
||
10 years ago
|
|
||
8 years ago
|
// Prepend the current save profile (if any)
|
||
|
string prefsKey = SetSaveProfile.saveProfile + "_" + flowchart.SubstituteVariables(key);
|
||
10 years ago
|
|
||
8 years ago
|
System.Type variableType = variable.GetType();
|
||
10 years ago
|
|
||
8 years ago
|
if (variableType == typeof(BooleanVariable))
|
||
|
{
|
||
|
BooleanVariable booleanVariable = variable as BooleanVariable;
|
||
|
if (booleanVariable != null)
|
||
|
{
|
||
|
// PlayerPrefs does not have bool accessors, so just use int
|
||
8 years ago
|
booleanVariable.Value = (PlayerPrefs.GetInt(prefsKey) == 1);
|
||
8 years ago
|
}
|
||
|
}
|
||
|
else if (variableType == typeof(IntegerVariable))
|
||
|
{
|
||
|
IntegerVariable integerVariable = variable as IntegerVariable;
|
||
|
if (integerVariable != null)
|
||
|
{
|
||
8 years ago
|
integerVariable.Value = PlayerPrefs.GetInt(prefsKey);
|
||
8 years ago
|
}
|
||
|
}
|
||
|
else if (variableType == typeof(FloatVariable))
|
||
|
{
|
||
|
FloatVariable floatVariable = variable as FloatVariable;
|
||
|
if (floatVariable != null)
|
||
|
{
|
||
8 years ago
|
floatVariable.Value = PlayerPrefs.GetFloat(prefsKey);
|
||
8 years ago
|
}
|
||
|
}
|
||
|
else if (variableType == typeof(StringVariable))
|
||
|
{
|
||
|
StringVariable stringVariable = variable as StringVariable;
|
||
|
if (stringVariable != null)
|
||
|
{
|
||
8 years ago
|
stringVariable.Value = PlayerPrefs.GetString(prefsKey);
|
||
8 years ago
|
}
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
Continue();
|
||
|
}
|
||
|
|
||
|
public override string GetSummary()
|
||
|
{
|
||
|
if (key.Length == 0)
|
||
|
{
|
||
|
return "Error: No stored value key selected";
|
||
|
}
|
||
|
|
||
|
if (variable == null)
|
||
|
{
|
||
|
return "Error: No variable selected";
|
||
|
}
|
||
10 years ago
|
|
||
8 years ago
|
return "'" + key + "' into " + variable.Key;
|
||
8 years ago
|
}
|
||
10 years ago
|
|
||
8 years ago
|
public override Color GetButtonColor()
|
||
|
{
|
||
|
return new Color32(235, 191, 217, 255);
|
||
|
}
|
||
8 years ago
|
}
|
||
10 years ago
|
}
|