Forum rules - please read before posting.

Linking with custom script help?

Hey! I'm really, really new to all things code, and my game time script was made with a lot of hand-holding from tutorials and friends, and I'm having trouble figuring out where to add the (public int variableIDToSyncWith = _____) in it. Any help would be very much appreciated! I apologize if this is a simple fix ;;
I want to link the variables for minute, hour, day, second, month, and year.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameTimeCalendar : MonoBehaviour
{

    private const int TIMESCALE = 45;
    private Text clockText;
    private Text dayText;
    private Text seasonText;
    private Text yearText;

    public double minute, hour, day, second, month, year;

    // Use this for initialization
    void Start()
    {
        month = 1;
        day = 1;
        hour = 7;
        minute = 30;
        year = 1890;

        clockText = GameObject.Find("Clock").GetComponent<Text>();
        dayText = GameObject.Find("Day").GetComponent<Text>();
        seasonText = GameObject.Find("Season").GetComponent<Text>();
        yearText = GameObject.Find("Year").GetComponent<Text>();
        CalculateSeason();
     }

Comments

  • Welcome to the community, @WarieLym.

    The script doesn't look comprehensive - did you post all of it?  Scripts are best copy/pasted in e.g. http://pasteall.org, and links posted here.

    By "variableIDToSyncWith", I'm assuming you mean the int variable that's present in the included VariableLinkingExample file.

    You need to create a new such variable for each AC Global Variable you wish to sync with.  I would recommend just syncing one variable (e.g. Year) succesfully before moving onto the others:

    public int yearVariableID;

    Place that among your other declared variables, and you'll see it exposed in the Inspector.  Set it to the ID value of your AC Global Variable (mind that 'double' is not an option for AC variables - so you'll have to either rely on ints or floats, or convert the value at runtime).

    You can then use the OnUploadVariable and OnDownloadVariable custom events to sync the AC variable's value in the same way that the example script does.  These events work by checking to see if the ID number of the affected AC Global Variable matches "yearVariableID", and updates values accordingly.

    In this example, I've gone down the conversion route:

    http://pasteall.org/1079101/csharp

    Once that's working and you want to sync the others, you don't have to copy the methods again - just update the OnDownload and OnUpload methods with the other "syncID" variables you make.

  • This is the full script, sorry about that! I was hoping just the variable declarations would be enough. http://pasteall.org/1080258/csharp
    Thank you for such a fast response, I'll give it another go based on your example!
  • I managed to get it working! Thank you so much for the help <3
    Is there a way to check a variable every frame? As I have it now, if the npc's path gets interrupted or slowed (either by obstacles, PC interaction, or just me not planning their path well enough) they can miss the variable check and won't move on to the next path.
  • You can read a variable's value every frame by doing so in an Update method, i.e.:

    void Update ()
    {
      float myFloatValue = GlobalVariables.GetFloatValue (myFloatVarID);
    }

  • Awesome, works perfectly! Thank you so much!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.