Search Unity

Problems with Creator Kit: Beginner Code

Discussion in 'Scripting' started by DJVarma, Oct 10, 2020.

  1. DJVarma

    DJVarma

    Joined:
    Aug 19, 2020
    Posts:
    4
    I was just following the tutorial when it suddenly said there are 21 errors. All these errors had nothing to do with the code I was editing (I think) based on the tutorial. According to Visual Studio, there are no issues found. I have tried everything from my limited knowledge, including undoing everything, clearing the errors and restoring the code from before. When I double-click the error, it doesn't show me where the error has occured in the code. The problems came from 6 different codes:

    The code from "BaseElementalEffects.cs":

    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using CreatorKitCode;
    6. using TMPro;
    7. using UnityEngine;
    8. namespace CreatorKitCode
    9. {
    10.     /// <summary>
    11.     /// The base class to derive from to write you own custom Elemental effect that can be added to a StatsSystem. There
    12.     /// is a default implementation called ElementalEffect that can be used to make Physical/Fire/Electrical/Cold damage
    13.     /// across time.
    14.     ///
    15.     /// A derived class *must* implement the Equals function so we can check if 2 effects are the same (e.g. the default
    16.     /// implementation ElementalEffect will consider 2 effect equal if they do the same DamageType).
    17.     /// </summary>
    18.     public abstract class BaseElementalEffect : IEquatable<BaseElementalEffect>
    19.     {
    20.         public bool Done => m_Timer <= 0.0f;
    21.         public float CurrentTime => m_Timer;
    22.         public float Duration => m_Duration;
    23.      
    24.         protected float m_Duration;
    25.         protected float m_Timer;
    26.         protected CharacterData m_Target;
    27.         public BaseElementalEffect(float duration)
    28.         {
    29.             m_Duration = duration;
    30.         }
    31.         public virtual void Applied(CharacterData target)
    32.         {
    33.             m_Timer = m_Duration;
    34.             m_Target = target;
    35.         }
    36.         public virtual void Removed()
    37.         {
    38.      
    39.         }
    40.         public virtual void Update(StatSystem statSystem)
    41.         {
    42.             m_Timer -= Time.deltaTime;
    43.         }
    44.         public abstract bool Equals(BaseElementalEffect other);
    45.     }
    46.     /// <summary>
    47.     /// Default implementation of the BaseElementalEffect. The constructor allows the caller to specify what type of
    48.     /// damage is done, how much is done and the speed (time) between each instance of damage (default 1 = every second).
    49.     /// </summary>
    50.     public class ElementalEffect : BaseElementalEffect
    51.     {
    52.         int m_Damage;
    53.         StatSystem.DamageType m_DamageType;
    54.         float m_DamageSpeed;
    55.         float m_SinceLastDamage = 0.0f;
    56.         VFXManager.VFXInstance m_FireInstance;
    57.         public ElementalEffect(float duration, StatSystem.DamageType damageType, int damage, float speed = 1.0f) :
    58.             base(duration)
    59.         {
    60.             m_Damage = damage;
    61.             m_DamageType = damageType;
    62.             m_DamageSpeed = speed;
    63.         }
    64.      
    65.         public override void Update(StatSystem statSystem)
    66.         {
    67.             base.Update(statSystem);
    68.             m_SinceLastDamage += Time.deltaTime;
    69.             if (m_SinceLastDamage > m_DamageSpeed)
    70.             {
    71.                 m_SinceLastDamage = 0;
    72.                 Weapon.AttackData data = new Weapon.AttackData(m_Target);
    73.                 data.AddDamage(m_DamageType, m_Damage);
    74.              
    75.                 statSystem.Damage(data);
    76.             }
    77.          
    78.             //we do not parent as if the original object is destroy it would destroy the instance
    79.             m_FireInstance.Effect.transform.position = m_Target.transform.position + Vector3.up;
    80.         }
    81.         public override bool Equals(BaseElementalEffect other)
    82.         {
    83.             ElementalEffect eff = other as ElementalEffect;
    84.             if (other == null)
    85.                 return false;
    86.             return eff.m_DamageType == m_DamageType;
    87.         }
    88.         public override void Applied(CharacterData target)
    89.         {
    90.             base.Applied(target);
    91.             //We use the fire effect as it's the only one existing in the project.
    92.             //You can add new VFX and use an if or switch statement to use the right VFXType
    93.             //depending on this effect m_DamageType
    94.             m_FireInstance = VFXManager.GetVFX(VFXType.FireEffect);
    95.             m_FireInstance.Effect.transform.position = target.transform.position + Vector3.up;
    96.         }
    97.         public override void Removed()
    98.         {
    99.             base.Removed();
    100.          
    101.             m_FireInstance.Effect.SetActive(false);
    102.         }
    103.     }
    104. }
    105.  
    The code from "TestResultRendererCallback.cs":

    Code (csharp):
    1.  
    2. using NUnit.Framework.Interfaces;
    3. namespace UnityEngine.TestTools.TestRunner.Callbacks
    4. {
    5.     internal class TestResultRendererCallback : MonoBehaviour, ITestRunnerListener
    6.     {
    7.         private TestResultRenderer m_ResultRenderer;
    8.         public void RunStarted(ITest testsToRun)
    9.         {
    10.         }
    11.         public void RunFinished(ITestResult testResults)
    12.         {
    13.             if (Camera.main == null)
    14.             {
    15.                 gameObject.AddComponent<Camera>();
    16.             }
    17.             m_ResultRenderer = new TestResultRenderer(testResults);
    18.             m_ResultRenderer.ShowResults();
    19.         }
    20.         public void OnGUI()
    21.         {
    22.             if (m_ResultRenderer != null)
    23.                 m_ResultRenderer.Draw();
    24.         }
    25.         public void TestStarted(ITest test)
    26.         {
    27.         }
    28.         public void TestFinished(ITestResult result)
    29.         {
    30.         }
    31.     }
    32. }
    33.  
    34.  
    The code from "PlayModeRunnerCallback.cs":

    Code (csharp):
    1.  
    2. using NUnit.Framework;
    3. using NUnit.Framework.Interfaces;
    4. namespace UnityEngine.TestTools.TestRunner.Callbacks
    5. {
    6.     [AddComponentMenu("")]
    7.     internal class PlayModeRunnerCallback : MonoBehaviour, ITestRunnerListener
    8.     {
    9.         private TestResultRenderer m_ResultRenderer;
    10.         public void RunFinished(ITestResult testResults)
    11.         {
    12.             Application.logMessageReceivedThreaded -= LogRecieved;
    13.             if (Camera.main == null)
    14.             {
    15.                 gameObject.AddComponent<Camera>();
    16.             }
    17.             m_ResultRenderer = new TestResultRenderer(testResults);
    18.             m_ResultRenderer.ShowResults();
    19.         }
    20.         public void TestFinished(ITestResult result)
    21.         {
    22.         }
    23.         public void OnGUI()
    24.         {
    25.             if (m_ResultRenderer != null)
    26.                 m_ResultRenderer.Draw();
    27.         }
    28.         public void RunStarted(ITest testsToRun)
    29.         {
    30.             Application.logMessageReceivedThreaded += LogRecieved;
    31.         }
    32.         public void TestStarted(ITest test)
    33.         {
    34.         }
    35.         private void LogRecieved(string message, string stacktrace, LogType type)
    36.         {
    37.             if (TestContext.Out != null)
    38.                 TestContext.Out.WriteLine(message);
    39.         }
    40.     }
    41. }
    42.  
    43.  
    The code from "PlaymodeTestsController.cs":

    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.Linq;
    6. using UnityEngine.SceneManagement;
    7. using UnityEngine.TestRunner.NUnitExtensions;
    8. using UnityEngine.TestRunner.NUnitExtensions.Runner;
    9. using UnityEngine.TestTools.NUnitExtensions;
    10. using UnityEngine.TestTools.Utils;
    11. namespace UnityEngine.TestTools.TestRunner
    12. {
    13.     [Serializable]
    14.     [AddComponentMenu("")]
    15.     internal class PlaymodeTestsController : MonoBehaviour
    16.     {
    17.         private IEnumerator m_TestSteps;
    18.         [SerializeField]
    19.         private List<string> m_AssembliesWithTests;
    20.         public List<string> AssembliesWithTests
    21.         {
    22.             get
    23.             {
    24.                 return m_AssembliesWithTests;
    25.             }
    26.             set
    27.             {
    28.                 m_AssembliesWithTests = value;
    29.             }
    30.         }
    31.         [SerializeField]
    32.         internal TestStartedEvent testStartedEvent = new TestStartedEvent();
    33.         [SerializeField]
    34.         internal TestFinishedEvent testFinishedEvent = new TestFinishedEvent();
    35.         [SerializeField]
    36.         internal RunStartedEvent runStartedEvent = new RunStartedEvent();
    37.         [SerializeField]
    38.         internal RunFinishedEvent runFinishedEvent = new RunFinishedEvent();
    39.         internal const string kPlaymodeTestControllerName = "Code-based tests runner";
    40.         [SerializeField]
    41.         public PlaymodeTestsControllerSettings settings = new PlaymodeTestsControllerSettings();
    42.         internal UnityTestAssemblyRunner m_Runner;
    43.         public IEnumerator Start()
    44.         {
    45.             //Skip 2 frame because Unity.
    46.             yield return null;
    47.             yield return null;
    48.             StartCoroutine(Run());
    49.         }
    50.         internal static bool IsControllerOnScene()
    51.         {
    52.             return GameObject.Find(kPlaymodeTestControllerName) != null;
    53.         }
    54.         internal static PlaymodeTestsController GetController()
    55.         {
    56.             return GameObject.Find(kPlaymodeTestControllerName).GetComponent<PlaymodeTestsController>();
    57.         }
    58.         public IEnumerator TestRunnerCoroutine()
    59.         {
    60.             while (m_TestSteps.MoveNext())
    61.             {
    62.                 yield return m_TestSteps.Current;
    63.             }
    64.             if (m_Runner.IsTestComplete)
    65.             {
    66.                 runFinishedEvent.Invoke(m_Runner.Result);
    67.                 Cleanup();
    68.                 yield return null;
    69.             }
    70.         }
    71.         public IEnumerator Run()
    72.         {
    73.             CoroutineTestWorkItem.monoBehaviourCoroutineRunner = this;
    74.             gameObject.hideFlags |= HideFlags.DontSave;
    75.             if (settings.sceneBased)
    76.             {
    77.                 SceneManager.LoadScene(1, LoadSceneMode.Additive);
    78.                 yield return null;
    79.             }
    80.             var testListUtil = new PlayerTestAssemblyProvider(new AssemblyLoadProxy(), m_AssembliesWithTests);
    81.             m_Runner = new UnityTestAssemblyRunner(new UnityTestAssemblyBuilder(), new PlaymodeWorkItemFactory());
    82.             var loadedTests = m_Runner.Load(testListUtil.GetUserAssemblies().Select(a => a.Assembly).ToArray(), TestPlatform.PlayMode, UnityTestAssemblyBuilder.GetNUnitTestBuilderSettings(TestPlatform.PlayMode));
    83.             loadedTests.ParseForNameDuplicates();
    84.             runStartedEvent.Invoke(m_Runner.LoadedTest);
    85.             var testListenerWrapper = new TestListenerWrapper(testStartedEvent, testFinishedEvent);
    86.             m_TestSteps = m_Runner.Run(testListenerWrapper, settings.BuildNUnitFilter()).GetEnumerator();
    87.             yield return TestRunnerCoroutine();
    88.         }
    89.         public void Cleanup()
    90.         {
    91.             if (m_Runner != null)
    92.             {
    93.                 m_Runner.StopRun();
    94.                 m_Runner = null;
    95.             }
    96.             if (Application.isEditor)
    97.             {
    98.                 Destroy(gameObject);
    99.             }
    100.         }
    101.         public static void TryCleanup()
    102.         {
    103.             var controller = GetController();
    104.             if (controller != null)
    105.             {
    106.                 controller.Cleanup();
    107.             }
    108.         }
    109.     }
    110. }
    111.  
    The code from "RemoteTestResultSender.cs"

    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.Linq;
    6. using System.Text;
    7. using NUnit.Framework.Interfaces;
    8. using UnityEngine.Networking.PlayerConnection;
    9. using UnityEngine.TestRunner.TestLaunchers;
    10. namespace UnityEngine.TestTools.TestRunner.Callbacks
    11. {
    12.     [AddComponentMenu("")]
    13.     internal class RemoteTestResultSender : MonoBehaviour, ITestRunnerListener
    14.     {
    15.         private class QueueData
    16.         {
    17.             public Guid id { get; set; }
    18.             public byte[] data { get; set; }
    19.         }
    20.         private const int k_aliveMessageFrequency = 120;
    21.         private float m_NextliveMessage = k_aliveMessageFrequency;
    22.         private readonly Queue<QueueData> m_SendQueue = new Queue<QueueData>();
    23.         private readonly object m_LockQueue = new object();
    24.         private readonly IRemoteTestResultDataFactory m_TestResultDataFactory = new RemoteTestResultDataFactory();
    25.         public void Start()
    26.         {
    27.             StartCoroutine(SendDataRoutine());
    28.         }
    29.         private byte[] SerializeObject(object objectToSerialize)
    30.         {
    31.             return Encoding.UTF8.GetBytes(JsonUtility.ToJson(objectToSerialize));
    32.         }
    33.         public void RunStarted(ITest testsToRun)
    34.         {
    35.             var data = SerializeObject(m_TestResultDataFactory.CreateFromTest(testsToRun));
    36.             lock (m_LockQueue)
    37.             {
    38.                 m_SendQueue.Enqueue(new QueueData
    39.                 {
    40.                     id = PlayerConnectionMessageIds.runStartedMessageId,
    41.                     data = data
    42.                 });
    43.             }
    44.         }
    45.         public void RunFinished(ITestResult testResults)
    46.         {
    47.             var data = SerializeObject(m_TestResultDataFactory.CreateFromTestResult(testResults));
    48.             lock (m_LockQueue)
    49.             {
    50.                 m_SendQueue.Enqueue(new QueueData { id = PlayerConnectionMessageIds.runFinishedMessageId, data = data, });
    51.             }
    52.         }
    53.         public void TestStarted(ITest test)
    54.         {
    55.             var data = SerializeObject(m_TestResultDataFactory.CreateFromTest(test));
    56.             lock (m_LockQueue)
    57.             {
    58.                 m_SendQueue.Enqueue(new QueueData
    59.                 {
    60.                     id = PlayerConnectionMessageIds.testStartedMessageId,
    61.                     data = data
    62.                 });
    63.             }
    64.         }
    65.         public void TestFinished(ITestResult result)
    66.         {
    67.             var testRunnerResultForApi = m_TestResultDataFactory.CreateFromTestResult(result);
    68.             var resultData = SerializeObject(testRunnerResultForApi);
    69.             lock (m_LockQueue)
    70.             {
    71.                 m_SendQueue.Enqueue(new QueueData
    72.                 {
    73.                     id = PlayerConnectionMessageIds.testFinishedMessageId,
    74.                     data = resultData,
    75.                 });
    76.             }
    77.         }
    78.         public IEnumerator SendDataRoutine()
    79.         {
    80.             while (!PlayerConnection.instance.isConnected)
    81.             {
    82.                 yield return new WaitForSeconds(1);
    83.             }
    84.             while (true)
    85.             {
    86.                 lock (m_LockQueue)
    87.                 {
    88.                     if (PlayerConnection.instance.isConnected && m_SendQueue.Count > 0)
    89.                     {
    90.                         ResetNextPlayerAliveMessageTime();
    91.                         var queueData = m_SendQueue.Dequeue();
    92.                         PlayerConnection.instance.Send(queueData.id, queueData.data);
    93.                         yield return null;
    94.                     }
    95.                     //This is needed so we dont stall the player totally
    96.                     if (!m_SendQueue.Any())
    97.                     {
    98.                         SendAliveMessageIfNeeded();
    99.                         yield return new WaitForSeconds(0.02f);
    100.                     }
    101.                 }
    102.             }
    103.         }
    104.         private void SendAliveMessageIfNeeded()
    105.         {
    106.             if (Time.timeSinceLevelLoad < m_NextliveMessage)
    107.             {
    108.                 return;
    109.             }
    110.             Debug.Log("Sending player alive message back to editor.");
    111.             ResetNextPlayerAliveMessageTime();
    112.             PlayerConnection.instance.Send(PlayerConnectionMessageIds.playerAliveHeartbeat, new byte[0]);
    113.         }
    114.         private void ResetNextPlayerAliveMessageTime()
    115.         {
    116.             m_NextliveMessage = Time.timeSinceLevelLoad + k_aliveMessageFrequency;
    117.         }
    118.     }
    119. }
    120.  
    The code from "PlayQuitHandler.cs":

    Code (csharp):
    1.  
    2. using NUnit.Framework.Interfaces;
    3. using UnityEngine.Networking.PlayerConnection;
    4. using UnityEngine.TestRunner.TestLaunchers;
    5. namespace UnityEngine.TestTools.TestRunner.Callbacks
    6. {
    7.     internal class PlayerQuitHandler : MonoBehaviour, ITestRunnerListener
    8.     {
    9.         public void Start()
    10.         {
    11.             PlayerConnection.instance.Register(PlayerConnectionMessageIds.quitPlayerMessageId, ProcessPlayerQuiteMessage);
    12.         }
    13.         private void ProcessPlayerQuiteMessage(MessageEventArgs arg0)
    14.         {
    15.             //Some platforms don't quit, so we need to disconnect to make sure they will not connect to another editor instance automatically.
    16.             PlayerConnection.instance.DisconnectAll();
    17.             //XBOX has an error when quitting
    18.             if (Application.platform == RuntimePlatform.XboxOne)
    19.             {
    20.                 return;
    21.             }
    22.             Application.Quit();
    23.         }
    24.         public void RunStarted(ITest testsToRun)
    25.         {
    26.         }
    27.         public void RunFinished(ITestResult testResults)
    28.         {
    29.         }
    30.         public void TestStarted(ITest test)
    31.         {
    32.         }
    33.         public void TestFinished(ITestResult result)
    34.         {
    35.         }
    36.     }
    37. }
    38.  
    39.  
    If there is anyone out there who sees the problem and/or who knows what to do, please help me.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    I'll say it again: we cannot read your mind or your computer screen.

    What are the errors, what does Google tell you about those errors?

    If you still cannot figure it out, COPY/PASTE THE ERROR HERE! Include full text, including line, referenced to the above source code.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    In the console window there is a line number and source file. Go there. See what the error is.

    If there is no line number, then perhaps it is a Unity internal error.

    You can tell this by looking at the stack trace, lower part of the consoel window.