Enemy AI in game is eating too much RAM - c#

I need help. I'm currently trying to make a game and so far I'm doing okay. The only major problem I've encountered is adding enemy AI. Without the AI, my game only uses about 200-400mb of ram. When I made the AI, the game now eats upwards of 2GB of RAM. That's too much for a simple 2D Game. Here's a sample of the AI code(please assume that all necessary syntax is in my program. There are no errors since I can get it to run):
public void launch()
{
enemyGen.Tick += enemyGen_Tick;// in charge of generating enemies
enemyGen.Interval = 1000;
enemyGen.Start();
}
private void eneMove_Tick(object sender, EventArgs e)
{
for (int a = 0; a < 3; a++)
{
if (eneGen[a] == 1)//creates BLUE ENEMY
{
EnAIaY[a] += 2;
eneCount++;
if (a == 3)
{ a = 0; }
}
}
}
private void enemyGen_Tick(object sender, EventArgs e)
{
if (eneCount < 3)//creates an enemy if there are less than 3 enemies
{
for (int a = 0; a < 3; a++)
{
if (eneGen[a] != 1)
{
Random rnd = new Random();
eneGen[a] = rnd.Next(0, 5);//Generates a random number for enemy generation
if (a == 3)
{ a = 0; }
}
}
}
}
To explain the code I made, there is an array of 3 objects. In "enemygen_Tick", I had it generate a random number between 0 and 5 per array object. Every time the number generated was a 1, it would spawn an enemy.
I also added an if statement in the for loops so that every time the for loop would reach 3, the maximum number of objects, it would start generating random numbers again per object.
My question is how do I reduce RAM usage? When the program reaches the 2GB memory usage point, it starts lagging so bad.
There is a null reference exception after the first enemy despawns(gets eliminated), but that's a question for another time. I can't debug the game if a Powerpoint slide has a faster FPS than the game.

Related

Gameobject not spawning in desired position after InvokeRepeating and Instantiate

Context
Hello, currently creating a clone of "Crossy Road" and what I'm trying to do is to spawn my moving object called "Vehicle" at a random speed and rate of spawn. This is also applicable to "Plank", but I will start first with the vehicle. So far, everything is working fine as intended for the game mechanics, but I would like to finalize with this issue so it is fully functional in terms of playability.
Problem
My issue now is I 3 different spawns objects: grass, river, and road. Each object holds other objects (let's call it spawners) depending of what field is being spawn. For example, if grass field object is spawned, it will spawn trees depending in a random varied selection. Another example is with road field. When the road is spawned, a vehicle will be spawned from either left or right in its current initial position. This vehicle will moves as intended with a random speed, but not with the original spawn position and rate (as shown in the GIF. The vehicle spawns in the middle of the road and not in the beginning of the left/right road).
As far I'm aware, my rate is currently unused because it is not the main issue I want to solve. However, the issue now is with the transform position not working as I have pictured in my head. So what is happening is that when the road is spawned again, the vehicle is spawned in the middle of the trajectory instead of resetting to the beginning.
Also, I have noticed that when I print the vehicle object, the Z-axis has a weird number compared to the original position.
Attempts done
I have been thinking that maybe it is the way I have set everything up. I have 4 vehicle objects with a child object called "Tank". However, in each vehicle object, I'm using SetActive(...) only and not really reusing the object itself to the beginning. Later on, I want to organize this spaghetti code and optimize it (e.g ObjectPool to spawn my roads and other GameObjects after hitting a certain range, adding a player range detection to spawn a field to name a few).
To be honest, my whole code feels bloated for something simple. This will be fixed once everything is working accordingly.
Code (DISCLAIMER: there is the possibility that there are unused variables)
SpawnManager.cs (some links provided too from learning to make this)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/*
** Weighted randomness: https://forum.unity.com/threads/random-numbers-with-a-weighted-chance.442190/
** Scriptable Object Weight spawn example: https://www.youtube.com/watch?v=FCksj9ofUgI&ab_channel=LlamAcademy
** From scratch loot tables with Scriptable Objects to make a loot table: https://www.youtube.com/watch?v=tX3RWsVLnzM&ab_channel=GregDevStuff
** Creating a random with an animation curve: https://www.youtube.com/watch?v=zw1OERK5xvU&ab_channel=HamzaHerbou
** Random Vehicle position spawn (maybe this can help me): https://stackoverflow.com/questions/51312481/move-spawn-object-to-random-position
*/
public class SpawnManager : MonoBehaviour
{
public GameObject Player;
public Spawn[] Field;
public GameObject[] SpawnObjectTrees;
public GameObject[] SpawnObjectVehicles; //different vehicles
public GameObject[] SpawnObjectPlanks; //3 sizes (small, medium, large)
private PlayerControl2 playerControlScript;
private int distancePlayer;
private int toggle;
private bool keepSpawning;
bool vehicleFlag = false;
bool plankFlag = false;
public float randomNumSpawn;
void Awake()
{
keepSpawning = true;
playerControlScript = GameObject.Find("PlayerObject").GetComponent<PlayerControl2>();
InvokeRepeating("Spawner", 3f, randomNumSpawn);
}
void Update()
{
if (Input.GetButtonDown("up") && !playerControlScript.gameOver)
SpawnField();
}
void Spawner()
{
bool activeLeft = false;
bool activeRight = false;
if (vehicleFlag)
{
print(initialObjectSpawn);
for (int i = 0; i < SpawnObjectVehicles.Length; i++)
{
print($"{SpawnObjectVehicles[i]}: {SpawnObjectVehicles[i].transform.position}"); //Here I get the weird position.z values pretty wonky
toggle = Random.Range(0, 2);
if (toggle == 1 && !activeLeft)
{
activeLeft = true;
SpawnObjectVehicles[i].SetActive(true);
}
if (toggle == 0 && !activeRight)
{
activeRight = true;
SpawnObjectVehicles[i].SetActive(true);
}
else
SpawnObjectVehicles[i].SetActive(false);
}
}
}
void SpawnField()
{
//I want to spawn the vehicles, planks, and trees in sets accordingly to the field (grass, river, road)
//For vehicles and planks, they can move horizontally from either -z or z boundaries
//NOTE: keepSpawning may be useless if i have a playerControlScript.gameOver already in here
if (keepSpawning)
{
distancePlayer += 3;
Vector3 intPos = new Vector3(0, 0, 0);
int i = Random.Range(0, 1000);
for (int j = 0; j < Field.Length; j++)
{
if (i >= Field[j].minProbabilityRange && i <= Field[j].maxProbabilityRange)
{
intPos = new Vector3(distancePlayer, -1f, 0);
GameObject Surface = Instantiate(Field[j].spawnField);
if (Surface.CompareTag("Grass"))
TreeToggle();
if (Surface.CompareTag("Road"))
{
vehicleFlag = true;
VehicleToggle();
}
// if (Surface.CompareTag("River")) this will be the same as vehicle
// {
// plankFlag = true;
// PlankToggle();
// }
//Add spawn for vehicles and planks with given spawnrate/spawn intervals
Surface.transform.position = intPos;
vehicleFlag = false;
plankFlag = false;
}
}
}
}
void TreeToggle()
{
int counter = 0;
for (int i = 0; i < SpawnObjectTrees.Length; i++)
{
int toggle = Random.Range(0, 2); //[0, 2)
if (toggle == 1 && counter < 5) //True and when there are already 5-4 trees to toggle
{
counter++;
SpawnObjectTrees[i].SetActive(true);
}
else //fills the rest to inactive Trees
SpawnObjectTrees[i].SetActive(false);
}
}
void VehicleToggle()
{
// I have Left and Right with 2 vehicles in each. My goal is to setActive one of them each side at a time with a different interval spawnrate and speed
Spawner();
}
void PlankToggle()
{
Spawner();
}
}
[System.Serializable]
public class Spawn
{
public GameObject spawnField;
public float minProbabilityRange = 0.0f;
public float maxProbabilityRange = 0.0f;
}
Hierarchy/Inspector
If there is any information you want to know, feel free to ask and I will make a quick edit to fulfill these goals. Again, thank you for your time and appreciate it :D I hope you are having a good day!

How to increase value on label score after each obstacle passed on passing obstacle game on windows form c#

I am making a simple game similar to crossy road game and my game game looks like this :
Im using timer to make the obstacle moving down and if my green character(button) didn't hit the obstacle (blue and red colored lable), it will increase the score for each obstacle passed.
The problem is i dont know how to increase the score for each obstacle passed.
I tried to make code to increase the score. Here is my code :
int score = 0;
private void timerMovement_Tick(object sender, EventArgs e)
{
foreach (Control wall in panel1.Controls)
{
if (wall is Label)
{
if (wall.Text == "")
{
wall.Top += 20;
if (wall .Top > button2.Top)
{
score += Convert.ToInt32(wall.Tag);
labelScore.Text = Convert.ToString(score);
}
}
}
}
}
But when i start the game, the score keep increasing overtime after i pass the first obstacle which i dont want this to happen. I want the score only increasing only after i pass one obstacle.
Anyone can help me make the check statement ? thank you

UNITY c#: Spawning character prefabs based on user selection

I have game for multiple players where each user selects their hero before game starts and that loads the selected heroes into the battle arena.
I have small issue with getting the instantiation to spawn in correct numbers of players
The method that I have for Spawning the characters:
private void Placement()
{
for (int i = 0; i < SelectedCards.Count; i++)
{
for (int t = 0; t < AvailableHeroes.Count; t++)
{
if (AvailableHeroes[t].name == SelectedCards[i].name)
{
Debug.Log(AvailableHeroes[t]);
// Instantiate(AvailableHeroes[t], PlayerSpawnLocation[t].transform.position, transform.rotation);
}
{
}
}
}
}
This script checks for amount of selected hero cards and puts it against my list that has all the available heroes to choose from(prefabs).
The debug.log shows that only the correct heroes get called.
Instantiate ends up spawning a loot of heroes instead of the selected amount.
For clarity I attach full class:
{
private int playerSize; //amount of choices for card selection
private GameManager GM;
[Header("Lists for Spawning in Heroes")]
public List<GameObject> SelectedCards;
public List<GameObject> AvailableHeroes;
public List<Transform> PlayerSpawnLocation;
[Header("Canvas used for the game")]
public Transform GameCanvas;
public Transform CharacterCanvas;
//When scene starts it takes how many players will be picking a card.
void Start()
{
//connects this script with gamenmanager to be able to manipulate the cameras
GM = GameObject.Find("GameManager").GetComponent<GameManager>();
//gets playersize information from main menu selection
PlayerPrefs.GetInt("PlayerSize");
playerSize = PlayerPrefs.GetInt("PlayerSize");
SelectedCards = new List<GameObject>();
//enables/disables correct canvas not to cause any problems when we initiate this scene
GameCanvas.gameObject.SetActive(false);
CharacterCanvas.gameObject.SetActive(true);
}
// Update is called once per frame
void Update()
{
if (playerSize <= 0)
{
Placement();
GM.CharacterSelectionCamera.enabled = false;
GameCanvas.gameObject.SetActive(true);
CharacterCanvas.gameObject.SetActive(false);
GM.BattleCamera.enabled = true;
}
}
public void PlayerSelected(int cardPicked)
{
playerSize -= cardPicked;
}
private void Placement()
{
for (int i = 0; i < SelectedCards.Count; i++)
{
for (int t = 0; t < AvailableHeroes.Count; t++)
{
if (AvailableHeroes[t].name == SelectedCards[i].name)
{
Debug.Log(AvailableHeroes[t]);
// Instantiate(AvailableHeroes[t], PlayerSpawnLocation[t].transform.position, transform.rotation);
}
{
}
}
}
}
}
I hope someone can explain where I am going wrong with this.
Thanks,
I got the answer, I guess I was just being tired from working and could not see the obvious.
For those who wonder what solution is
The method gets called each frame thus it continues to endlessly spawn objects
There are 2 ways to fix it
1 Make coroutine and then return after you make your initial batch
2 Use a boolean at update so not only it checks player size but also whenever it can spawn it or not, you set the boolean to false after method get called.
I did not even notice the update function part.
Just a heads up, in your start function, PlayerPrefs.GetInt("PlayerSize"); is not doing anything since the value is not saved anywhere.

C# infinite spawning of platforms - will it create a lag?

Alright, Im working in Unity in C# and Ive created a few simple functions to spawn a series of platforms (game objects) indefinitely in accordance with the z position of the player. I delete the game objects once they are securely out of sight and called this update method every time player moves forward:
void spawnSectorGroup()
{
int i = numSectorsToSpawn; //get position of last sectors, when its exceeded by player then delete and spawn again
while (i >= 0) {
int j = Random.Range (0, sectors.Length);
spawnSector (gamePosition.position, sectors [j]);
i--;
}
}
void checkAndUpdateSectors()
{
//print ("Player pos"+playerPosition.position);
//print ("last sector"+gamePosition.position);
if (gamePosition.position.z - playerPosition.position.z <= 20) { //safe value ahead
print ("theyre almost there, spawn new group");
print (gamePosition.position.z - playerPosition.position.z);
spawnSectorGroup ();
}
//destroy past ones
GameObject[] objects = FindObjectsOfType(typeof(GameObject)) as GameObject[];
//GameObject[] objects = GameObject.FindGameObjectsWithTag("Block");
foreach (GameObject o in objects) {
if (o.gameObject.tag == "Block" || o.gameObject.tag == "Cage" || o.gameObject.tag == "Animal" || o.gameObject.tag == "Enemy") {
if (playerPosition.position.z - o.transform.position.z >= 100) { //safe aways back
print ("destroying past object");
print (o.transform.position);
Destroy (o.gameObject);
}
}
}
}
void spawnSector(Vector3 relativePosition,GameObject sector){
GameObject newSector = GameObject.Instantiate (sector, relativePosition, transform.rotation) as GameObject;
gamePosition.position += newSector.GetComponent<Sector> ().length;
}
This all works, however Im worried in the long run if this spawning of about 10 new "sectors" each time the player is within 20 or so units of the last spawned sector will build up and create a lag from the multitude of game objects.
Im not experienced with indefinite spawning - will this be a problem? Or is this good practice with indefinite spawning?
Creating and destroying objects is expensive and something you want to avoid doing in large amounts while your game is running.
Check out this Unity tutorial on Object Pooling. The basic idea is that instead of creating and destroying the objects, you take them from a pool of existing objects and return them when you are done so they can be reused.

Changing GameState based on an if statement?

I'll get to the point. Having an issue with a beginner level game im coding at the moment. I have 2 lists to store "Objects" that are in the game. One is for "diamonds" pushable blocks that are to be moved onto the "Goals". Once all diamonds are on the goals, the level should change. I'm currently using "GameStates" to load each level. Here is the snippet of my code I'm having issues with. What happens currently is the game will allow me to push the "diamonds" onto the "goals", but the gamestate will not change once I do this. Not sure what I am missing - any help is appreciated. Thankyou for your time!
void Level1Update(KeyboardState cKB, KeyboardState oKB)
{
for (int i = 2; i < diamondlist.Count; i++)
{
if ((Goallist[i].Position == diamondlist[i].Position))
{
CurrentGameState = GameState.Level2;
}
}
}
If I understood correctly, you want to set current game state to the next level only if all the diamonds and goals are on the same tiles. The following code ensures that.
void Level1Update(KeyboardState cKB, KeyboardState oKB)
{
int i;
for (i = 0; i < diamondlist.Count; ++i)
{
if (Goallist[i].Position != diamondlist[i].Position)
break;
}
// When breaked off the loop, i is never equal to list count
if (i == diamondList.Count)
CurrentGameState = GameState.Level2;
}

Categories

Resources