Problem with idle game (Running mutiple lines of code at the same time)

LegitMeme

Member
Joined
May 5, 2019
Messages
13
Programming Experience
Beginner
I'm trying to make an idle game, but I don't know how to run 2 scripts at the same time.

Scripts:

1. (Main Program):
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using ClassLibrary;
using System.Threading.Tasks;
using System.Security.Cryptography;

namespace GameMain
{
    class Game
    {
        public static convertToMiliseconds convert = new convertToMiliseconds();
        static void Main(string[] args)
        {
        Start:

            string input = Start();

            Console.Clear();

            if (input == "Start" || input == "start")
            {
                countdown(10);
                Console.Clear();
                Console.ForegroundColor = ConsoleColor.White;
                //Call other script to generate money
               
            }
            else if (input == "Exit" || input == "exit")
            {
                return;
            }
            else
            {
                goto Start;
            }

            Console.ReadLine();
        }
        static string Start()
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write("Idle ");

            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write("Loading");

            Console.ForegroundColor = ConsoleColor.White;
            Console.Write("...");

            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write("\nBy ");

            Console.ForegroundColor = ConsoleColor.White;
            Console.Write("HubStudios\n\n");

            Console.Write("Input: ");

            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write("Exit ");

            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write("Start");

            Console.ForegroundColor = ConsoleColor.White;
            Console.Write(": ");

            string input = Console.ReadLine();
            return input;
        }
        public static void countdown(int timerLength)
        {
            double percent1 = 0;
            int counter1 = 0;
            double percent2 = 0;
            int counter2 = 0;
            double percent3 = 0;

            for (int i = timerLength; i < 20; i--)
            {
                Console.Write("Loading: ");
                Console.Write(i);

                if (i <= 3 && i > 1)
                {
                    Console.WriteLine("\n\nFinishing Up");
                    percent3 += 33.33;
                    Console.WriteLine("Finishing: {0}%", percent3);
                }
                else if (i <= 6 && i > 3)
                {
                    Console.WriteLine("\n\nLoading Scripts");

                    percent2 += 33.33;
                    Console.WriteLine("Scripts: {0}%", percent2);

                    string script = GetUniqueKey(2500);

                    if (counter2 == 0)
                    {
                        Console.WriteLine("\n\nLoading: {0}", script);
                    }
                    else if (counter2 == 1)
                    {
                        Console.WriteLine("\n\nLoading: {0}", script);
                    }
                    else if (counter2 == 2)
                    {
                        Console.WriteLine("\n\nLoading: {0}", script);
                    }

                    counter2 += 1;
                }
                else if (i <= 9 && i > 6)
                {
                    Console.WriteLine("\n\nStarting Console");

                    percent1 += 33.33;
                    Console.WriteLine("Console: {0}%", percent1);

                    if (counter1 == 0)
                    {
                        Console.WriteLine("\n\nLoading settings.dll");
                    }
                    else if (counter1 == 1)
                    {
                        Console.WriteLine("\n\nCompiling script data");
                    }
                    else if (counter1 == 2)
                    {
                        Console.WriteLine("\n\nRendering fonts");
                    }

                    counter1 += 1;
                }

                if (i == 1)
                {
                    Console.WriteLine("\n\nFinishing Up");
                    percent3 += 33.33;
                    Console.WriteLine("Finishing: {0}%", percent3);

                    Thread.Sleep(1000);

                    Console.Clear();

                    starting(); starting();
                    return;
                }

                Thread.Sleep(1000);
                Console.Clear();
            }
        }
        public static string GetUniqueKey(int size)
        {
            char[] chars =
                "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
            byte[] data = new byte[size];
            using (RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider())
            {
                crypto.GetBytes(data);
            }
            StringBuilder result = new StringBuilder(size);
            foreach (byte b in data)
            {
                result.Append(chars[b % (chars.Length)]);
            }
            return result.ToString();
        }
        static void starting()
        {
            Console.WriteLine("-=-");
            Console.WriteLine("starting");
            Thread.Sleep(500);

            Console.Clear();

            Console.WriteLine("-=-");
            Console.WriteLine("starting.");
            Thread.Sleep(500);

            Console.Clear();

            Console.WriteLine("-=-");
            Console.WriteLine("starting..");
            Thread.Sleep(500);

            Console.Clear();

            Console.WriteLine("-=-");
            Console.WriteLine("starting...");
            Thread.Sleep(500);

            Console.Clear();
        }
    }
}

2. (Money generator script):
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MoneyScript
{
    class Program
    {
        public static int money;
        static void Main(string[] args)
        {
            bool starting = true;
            while (starting)
            {
                money++;
                Thread.Sleep(2500);
            }
        }
    }
}

I'm trying to get the scripts to communicate and run at the same time? I don't know how though, please help. Thanks!
 
Firstly, there's no scripts there. C# is not a scripting language so C# code is not a script.

As for the question, there are various ways to execute code in parallel these days. The preferred option is generally to use the Tasks Parallel Library (TPL), although there are various specific options within that too. One thing you might do is declare two async methods and call them both, then await them both. If you want to synchronise multiple threads then you can look at using lock statements or WaitHandles.
 
Firstly, there's no scripts there. C# is not a scripting language so C# code is not a script.

As for the question, there are various ways to execute code in parallel these days. The preferred option is generally to use the Tasks Parallel Library (TPL), although there are various specific options within that too. One thing you might do is declare two async methods and call them both, then await them both. If you want to synchronize multiple threads then you can look at using lock statements or WaitHandles.

Could you please give me an example code? I'm a bit confused. Isn't there another way to make a game with c#? I'm trying to make an idle game so you generate money over time but isn't there an easier way to do this? I want to make a program where you can buy "factories" that produce money over time, so I think a console application isn't the best way to achieve this, but I don't know alternatives other than creating a console application. So if you could give a code example and/or tell me a better way to achieve my goal for creating an idle game, would be a great help.

PS: thanks for clarifying that's it's not a scripting language I wasn't sure what I should've called it.
 
Firstly, there's no scripts there. C# is not a scripting language so C# code is not a script.

As for the question, there are various ways to execute code in parallel these days. The preferred option is generally to use the Tasks Parallel Library (TPL), although there are various specific options within that too. One thing you might do is declare two async methods and call them both, then await them both. If you want to synchronise multiple threads then you can look at using lock statements or WaitHandles.
Could you please give me an example code? I'm a bit confused. Isn't there another way to make a game with c#? I'm trying to make an idle game so you generate money over time but isn't there an easier way to do this? I want to make a program where you can buy "factories" that produce money over time, so I think a console application isn't the best way to achieve this, but I don't know alternatives other than creating a console application. So if you could give a code example and/or tell me a better way to achieve my goal for creating an idle game, would be a great help.

PS: thanks for clarifying that's it's not a scripting language I wasn't sure what I should've called it.

Maybe there is a special package I need? I just learned about them, do you have any advice?
 
If you are using a modern version of the .NET Framework, you don't need to get a special package. It is built into the framework. For example see Task-based asynchronous programming
 
Back
Top Bottom