0
Answered

Set timescale to pause (0) in corgi engine

kemikalx61 2 months ago • updated by Lazlo Bonin (Lead Developer) 3 weeks ago 1

Greetings

I am working on my game which uses Chronos and Corgi engine and everything is mostly ok except for 1 issue.

I have a gameobject spawner which you might have guessed spawns stuff. My problem is, even if the game is paused the spawning still occurs. I believe I am having trouble with the time scale. 

I have posted my code in it's entirety and interestingly enough,  the debug logs shows 0 and 1 when the game is paused so the Time.timescale is 0 but time.timescale is still 1. When i check all the clocks, the time scale is 1.

Towards the end of this, I have added the [pause code for corgi, I have tried to extend the pause and in that set the clock time scale to 0.

However, what happens is, the game is paused locally, and a pause is also triggered on the editor (button next to the play button) and yet my time scale is still 1 on the time keeper clock.

I've added all the code that I believe is relevant, grateful for any assistance. 


Spawner Code

using UnityEngine;
using System.Collections;
using Chronos;

public class ObjectSpawn : BaseBehaviour //MonoBehaviour
{

public float minTime = 3f;
public float maxTime = 9f;
public float minX = -65.5f;
public float maxX = -5.5f;
public float topY = -5.5f;
public float z = 0.0f;
public int count = 50;
public GameObject prefab;
public bool doSpawn = true;

public float fallGrav =1.0f;
int first = 1;

void Start()
{
Clock clock = Timekeeper.instance.Clock("MovingOneWayPlatforms");

StartCoroutine(Spawner());

}



IEnumerator Spawner()
{

while (first == 1) {
yield return time.WaitForSeconds(8.0f);
first = 0;
}


while (doSpawn && count > 0 && Time.timeScale != 1 )
{
if (Time.timeScale == 1)
{
Debug.Log("Not paused");
}
if (Time.timeScale == 0)
{
Debug.Log("Paused");
}


Debug.Log(Time.timeScale);
Debug.Log(time.timeScale);


Renderer renderer = GetComponent();
float min = renderer.bounds.min.x;
float max = renderer.bounds.max.x;
Vector3 v12 = new Vector3(Random.Range(minX, maxX), this.gameObject.transform.position.y, 0f);

prefab.GetComponent().gravityScale = fallGrav;
prefab = Instantiate(prefab, v12, Random.rotation);

count--;
// yield return new WaitForSeconds(Random.Range(minTime, maxTime));
yield return time.WaitForSeconds(Random.Range(minTime, maxTime));

Destroy(prefab, 6);

}
// }
}
}
 

Pause from Corgi

///
/// Pauses the game or unpauses it depending on the current state
///
public virtual void Pause(PauseMethods pauseMethod = PauseMethods.PauseMenu)
{
if ((pauseMethod == PauseMethods.PauseMenu) && _inventoryOpen)
{
return;
}

// if time is not already stopped
if (Time.timeScale>0.0f)
{
Instance.SetTimeScale(0.0f);

Instance.Paused=true;
if ((GUIManager.Instance!= null) && (pauseMethod == PauseMethods.PauseMenu))
{
GUIManager.Instance.SetPause(true);
_pauseMenuOpen = true;
SetActiveInventoryInputManager (false);
}
if (pauseMethod == PauseMethods.Inventory)
{
_inventoryOpen = true;
}
}
else
{
UnPause(pauseMethod);
}
LevelManager.Instance.ToggleCharacterPause();
}

///
/// Unpauses the game
///
public virtual void UnPause(PauseMethods pauseMethod = PauseMethods.PauseMenu)
{
Instance.ResetTimeScale();
Instance.Paused = false;
if ((GUIManager.Instance!= null) && (pauseMethod == PauseMethods.PauseMenu))
{
GUIManager.Instance.SetPause(false);
_pauseMenuOpen = false;
SetActiveInventoryInputManager (true);
}
if (_inventoryOpen)
{
_inventoryOpen = false;
}
}

Pause Method Extended to Stop clock

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Chronos;
using MoreMountains.CorgiEngine;

public class ExtendPauses : GameManager
{

public override void Pause(PauseMethods pauseMethod = PauseMethods.PauseMenu)
{
if ((pauseMethod == PauseMethods.PauseMenu) && _inventoryOpen)
{
return;
}

// if time is not already stopped
if (Time.timeScale > 0.0f)
{
Clock clock = Timekeeper.instance.Clock("MovingOneWayPlatforms");
clock.localTimeScale = 0;
Instance.SetTimeScale(0.0f);

Instance.Paused = true;
if ((GUIManager.Instance != null) && (pauseMethod == PauseMethods.PauseMenu))
{
GUIManager.Instance.SetPause(true);
_pauseMenuOpen = true;
SetActiveInventoryInputManager(false);
}
if (pauseMethod == PauseMethods.Inventory)
{
_inventoryOpen = true;
}
}
else
{
UnPause(pauseMethod);
}
LevelManager.Instance.ToggleCharacterPause();
}
// Update is called once per frame
// void Update()
// {

// }
}

Chronos Version:
Unity Version:
2018.2.11f1
Answered

Hi kemikalx, sorry for the late reply on this.

The problem here is that Corgi does not pause Chronos, it only pauses Unity.

These lines specifically:

if (Time.timeScale>0.0f)
{
Instance.SetTimeScale(0.0f);

Here, you should also add something like Timekeeper.instance.Clock("yourClock").timeScale = 0.