Hello, I'm working with a script that includes a global variable. I want to use the value of this variable within a schematic, specifically within a 'Variable Fork' node. What is the best way to retrieve and use this (AttackBonusConclusion) variable in my schematic?

using UnityEngine;
using UnityEngine.UI;

public class AttackBonusTimer : MonoBehaviour
{
// Static variable to store the outcome of the attack bonus
public static string AttackBonusConclusion;

// UI elements and prefabs for the timer
public Image progressBar;
public GameObject greenCuePrefab;
public GameObject redCuePrefab;
public GameObject orangeCuePrefab;
public Text cueText;
public GameObject arrowPrefab;

// Timer settings
public float maxTime = 2f; // Duration for the timer
private float currentTime; // Current countdown time
private bool shouldReset = true; // Flag to control timer reset

// Game objects representing cues and arrow in the UI
private GameObject arrow;
private GameObject greenCue;
private GameObject redCue;
private GameObject orangeCue;

// Initialization of the timer on start
void Start()
{
InitializeTimerGameObjects();
ResetTimer();
}

// Sets up the timer UI elements
void InitializeTimerGameObjects()
{
progressBar.fillAmount = 0f;

// Create cue indicators
redCue = Instantiate(redCuePrefab, progressBar.transform);
orangeCue = Instantiate(orangeCuePrefab, progressBar.transform);
greenCue = Instantiate(greenCuePrefab, progressBar.transform);
SetGreenCuePosition();

// Create the arrow indicator
arrow = Instantiate(arrowPrefab, progressBar.transform);
arrow.GetComponent<SpriteRenderer>().sortingOrder = 2; // Ensures arrow appears on top
}

// Randomly positions the green cue within the bounds of the orange cue
private void SetGreenCuePosition()
{
RectTransform orangeCueRect = orangeCue.GetComponent<RectTransform>();
RectTransform greenCueRect = greenCue.GetComponent<RectTransform>();

float minX = orangeCueRect.rect.xMin + greenCueRect.rect.width / 2.1f;
float maxX = orangeCueRect.rect.xMax - greenCueRect.rect.width / 2.3f;
float randomX = Random.Range(minX, maxX);

greenCue.transform.localPosition = new Vector3(randomX, 0, 0);
}

// Resets the timer to its starting value
public void ResetTimer()
{
if (shouldReset)
{
currentTime = maxTime;
Debug.Log("Timer reset.");
shouldReset = false;
}
}

// Stops the timer and logs the event
public void StopTimer()
{
currentTime = 0;
Debug.Log("Timer stopped.");
}

// Main update loop for the timer
void Update()
{
// Decrease timer and reset if needed
UpdateTimer();

// Check for spacebar input to stop the timer
if (Input.GetKeyDown(KeyCode.Space))
{
CheckArrowPosition();
StopTimer();
}
}

// Updates the timer and UI progress
private void UpdateTimer()
{
currentTime -= Time.deltaTime;

// Reset the timer if the countdown reaches zero
if (currentTime <= 0 && shouldReset)
{
ResetTimer();
}

// Update progress bar
UpdateProgressBar();
}

// Updates the progress bar based on the current time
private void UpdateProgressBar()
{
float progress = Mathf.Clamp01(currentTime / maxTime);
progressBar.fillAmount = progress;

// Update arrow position along the progress bar
UpdateArrowPosition(progress);
}

// Moves the arrow based on the current progress
private void UpdateArrowPosition(float progress)
{
float arrowX = Mathf.Lerp(progressBar.rectTransform.rect.xMin, progressBar.rectTransform.rect.xMax, progress);
arrow.transform.localPosition = new Vector3(arrowX, 0, 0);
}

// Checks the position of the arrow relative to the cues
void CheckArrowPosition()
{
// Calculate arrow positions relative to cues
float arrowPositionRelativeToGreenCue = Mathf.Abs(arrow.transform.localPosition.x - greenCue.transform.localPosition.x);
float arrowPositionRelativeToOrangeCue = Mathf.Abs(arrow.transform.localPosition.x - orangeCue.transform.localPosition.x);

// Determine the outcome based on the arrow's position
DetermineAttackBonusOutcome(arrowPositionRelativeToGreenCue, arrowPositionRelativeToOrangeCue);
}

// Sets the AttackBonusConclusion based on the arrow's position
private void DetermineAttackBonusOutcome(float positionRelativeToGreen, float positionRelativeToOrange)
{
float cueWidth = greenCue.GetComponent<RectTransform>().rect.width;

if (positionRelativeToGreen <= cueWidth / 2f)
{
AttackBonusConclusion = "green";
Debug.Log("<color=green>Space pressed within the green cue.</color>");
}
else if (positionRelativeToOrange <= cueWidth / 2f)
{
AttackBonusConclusion = "orange";
Debug.Log("<color=orange>Space pressed within the orange cue.</color>");
}
else
{
AttackBonusConclusion = "red";
Debug.Log("<color=red>Space pressed outside the cues.</color>");
}
}

}
  • You can set variables in code with this:

    Maki.Game.Variables.Set(variablename, value)

    So just set it at the end of your schematic, and it should be picked up by your variable fork later (you can also make this variable name a string parameter for your node if you don't want to hardcode it), and I believe by default Variables.Set puts it at Global level but GiL might have to clarify that one.

    Maki.Game.Variables.GetFloat, GetBool, etc. is how you retrieve them too if needed
  • Maki.Game.Variables is the access to global variables :)
    Please consider rating/reviewing my products on the Asset Store (hopefully positively), as that helps tremendously with getting found.
    If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
  • Thanks! I got it working!
Sign In or Register to comment.