Argument out of range error - c#

My code is working intermittently, but sometimes gives me the argument out of range error and I can't figure out why, I've narrowed it down to one function:
void corridorFill()
{
int dir = 0;
//Set initial coords
int rand = Random.Range(0,freeCol.Count);
List<int> freeCorRow = new List<int>();
List<int> freeCorCol = new List<int>();
row = freeRow[rand];
col = freeCol[rand];
int fill = 0;
while(fill < area)
{
//Pick a random direction and go that way
//0 = north, 1 = east, 2 = south, 3 = west
dir = Random.Range(0,4);
if(directionIsSafe(dir, row, col, (int)room.Unreserved, roomType)
&& directionIsSafe(dir, row, col, (int)connect.Empty, connections))
{
//move in direction
moveDirection(dir);
freeCorRow.Add(row);
freeCorCol.Add(col);
if(fill > 0)
{
//place exit to previous tile
addExit(row, col, (dir+2)%4);
//change exits of previous room to connect
addExit(freeCorRow[freeCorRow.Count-2], freeCorCol[freeCorCol.Count-2], dir);
}
fill++;
}
else
{
bool set = false;
while(!set)
{
//direction is not safe therefore start again somewhere else, attached to what we already have
int r = Random.Range(0,freeCorRow.Count);
//check if a tile beside a known tile is free
dir = Random.Range(0,4);
//if the direction is safe, go that way
if(directionIsSafe(dir, freeCorRow[r], freeCorCol[r], (int)room.Unreserved, roomType)
&& directionIsSafe(dir, freeCorRow[r], freeCorCol[r], (int)connect.Empty, connections))
{
addExit(freeCorRow[r], freeCorCol[r], dir);
row = freeCorRow[r];
col = freeCorCol[r];
moveDirection(dir); //move in direction
addExit(row, col, (dir+2)%4); //place exit to previous tile
freeCorRow.Add(row);
freeCorCol.Add(col);
set = true;
}
}
fill++;
}
}
}
which uses the addExit function, though I don't think the problem is here:
//check previous tile corridor configuration and change to match current
void addExit(int row, int col, int dir)
{
//Add northourn exit to room
if(dir == 0)
{
if(connections[row,col] == (int)connect.Empty)
{
connections[row,col] = (int)connect.N;
}
else if(connections[row,col] == (int)connect.E)
{
connections[row,col] = (int)connect.NE;
}
else if(connections[row,col] == (int)connect.S)
{
connections[row,col] = (int)connect.NS;
}
else if(connections[row,col] == (int)connect.W)
{
connections[row,col] = (int)connect.NW;
}
else if(connections[row,col] == (int)connect.SE)
{
connections[row,col] = (int)connect.NES;
}
else if(connections[row,col] == (int)connect.SW)
{
connections[row,col] = (int)connect.SWN;
}
else if(connections[row,col] == (int)connect.EW)
{
connections[row,col] = (int)connect.WNE;
}
else if(connections[row,col] == (int)connect.ESW)
{
connections[row,col] = (int)connect.NESW;
}
}
//Add eastern exit to room
if(dir == 1)
{
if(connections[row,col] == (int)connect.Empty)
{
connections[row,col] = (int)connect.E;
}
else if(connections[row,col] == (int)connect.N)
{
connections[row,col] = (int)connect.NE;
}
else if(connections[row,col] == (int)connect.S)
{
connections[row,col] = (int)connect.SE;
}
else if(connections[row,col] == (int)connect.W)
{
connections[row,col] = (int)connect.EW;
}
else if(connections[row,col] == (int)connect.NW)
{
connections[row,col] = (int)connect.WNE;
}
else if(connections[row,col] == (int)connect.SW)
{
connections[row,col] = (int)connect.ESW;
}
else if(connections[row,col] == (int)connect.NS)
{
connections[row,col] = (int)connect.NES;
}
else if(connections[row,col] == (int)connect.SWN)
{
connections[row,col] = (int)connect.NESW;
}
}
//Add southourn exit to room
if(dir == 2)
{
if(connections[row,col] == (int)connect.Empty)
{
connections[row,col] = (int)connect.S;
}
else if(connections[row,col] == (int)connect.N)
{
connections[row,col] = (int)connect.NS;
}
else if(connections[row,col] == (int)connect.E)
{
connections[row,col] = (int)connect.SE;
}
else if(connections[row,col] == (int)connect.W)
{
connections[row,col] = (int)connect.SW;
}
else if(connections[row,col] == (int)connect.NE)
{
connections[row,col] = (int)connect.NES;
}
else if(connections[row,col] == (int)connect.NW)
{
connections[row,col] = (int)connect.SWN;
}
else if(connections[row,col] == (int)connect.EW)
{
connections[row,col] = (int)connect.ESW;
}
else if(connections[row,col] == (int)connect.WNE)
{
connections[row,col] = (int)connect.NESW;
}
}
//Add western exit to room
if(dir == 3)
{
if(connections[row,col] == (int)connect.Empty)
{
connections[row,col] = (int)connect.W;
}
else if(connections[row,col] == (int)connect.N)
{
connections[row,col] = (int)connect.NW;
}
else if(connections[row,col] == (int)connect.E)
{
connections[row,col] = (int)connect.EW;
}
else if(connections[row,col] == (int)connect.S)
{
connections[row,col] = (int)connect.SW;
}
else if(connections[row,col] == (int)connect.NE)
{
connections[row,col] = (int)connect.WNE;
}
else if(connections[row,col] == (int)connect.SE)
{
connections[row,col] = (int)connect.ESW;
}
else if(connections[row,col] == (int)connect.NS)
{
connections[row,col] = (int)connect.SWN;
}
else if(connections[row,col] == (int)connect.NES)
{
connections[row,col] = (int)connect.NESW;
}
}
}
and the directionIsSafe function:
bool directionIsSafe(int dir, int row, int col, int roomname, int[,] checkType)
{
if(dir == 0 && col+1 < stationHeight)
{
if(checkType[row, col+1] == roomname)
{
return true;
}
else
{
return false;
}
}
else if(dir == 1 && row+1 < stationWidth)
{
if(checkType[row+1,col] == roomname)
{
return true;
}
else
{
return false;
}
}
else if(dir == 2 && col > 0)
{
if(checkType[row,col-1] == roomname)
{
return true;
}
else
{
return false;
}
}
else if(dir == 3 && row > 0)
{
if(checkType[row-1,col] == roomname)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
Reading through the code it seems like it should work, and it does some of the time, but not all of the time. I can't figure out why only sometimes it doesn't work. Appreciate any light you might shed on the problem
EDT
freeCol and freeRow are created using:
if(genType == (int)shapes.cross)
{
//for odd numbers
if(stationWidth%2 == 1)
{
for(row = 0; row < stationWidth; row++)
{
for(col = 0; col < stationHeight; col++)
{
if((row <= stationWidth/2 + (crossArmSize/2.0f + 0.5f) - 1 && row >= stationWidth/2 - (crossArmSize/2.0f + 0.5f) + 1)
|| (col <= stationHeight/2 + (crossArmSize/2.0f + 0.5f) - 1 && col >= stationHeight/2 - (crossArmSize/2.0f + 0.5f) + 1))
{
roomType[row,col] = (int)room.Unreserved;
freeRow.Add(row);
freeCol.Add(col);
}
}
}
}
//for even numbers
else if(stationWidth%2 == 0)
{
for(row = 0; row < stationWidth; row++)
{
for(col = 0; col < stationHeight; col++)
{
if((row < stationWidth/2 + crossArmSize/2 && row >= stationWidth/2 - crossArmSize/2)
|| (col < stationWidth/2 + crossArmSize/2 && col >= stationWidth/2 - crossArmSize/2))
{
roomType[row,col] = (int)room.Unreserved;
freeRow.Add(row);
freeCol.Add(col);
}
}
}
}
corridorFill();
}

I think Random.Range is inclusive. Try int rand = Random.Range(0,freeCol.Count - 1);

Are Width and Height swapped in the last function?
Normally rows go along the height and columns along the width.

OK, so I changed:
row = freeRow[rand];
col = freeCol[rand];
to:
row = freeRow[Mathf.RoundToInt(stationWidth/2)];
col = freeCol[Mathf.RoundToInt(stationHeight/2)];
and it works every time now. I just can't figure out why the former was throwing out weird results and just plain not working, if you can shed light on it I'd love to hear it!

Related

Unity Color Matching Game

I have searched the similar games on forum and google but i could not find exactly.
I am making a puzzle game. and user can get point if the nodes (horizontal sticks) are same color then he can get.
when they are in same direction it says colors matched but in generated node whenever i rotate the sticks it says also same.
Can you take a look? and tell me how to fix. Also if you have better idea about this matching I will be appreciated.
---------
void Update()
{
if (Input.GetMouseButtonDown(0))
{
clickTime = Time.time;
rayhit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero, Mathf.Infinity, selectableObjLayerMask);
}
else if (Input.GetMouseButtonUp(0))
{
if (rayhit)
{
if (Time.time - clickTime < .2f)
{
Node node = rayhit.transform.GetComponent<Node>();
if (node != null)
{
for (int i = 0; i < node.sticks.Count; i++)
{
Vector3 newAngles = new Vector3(0, 0, (node.sticks[i].transform.localEulerAngles.z - 45));
newAngles.z = newAngles.z < 0 ? newAngles.z + 180 : newAngles.z;
newAngles.z = newAngles.z >180 ? newAngles.z - 180 : newAngles.z;
node.sticks[i].transform.localEulerAngles = newAngles;
node.sticks[i].degree = (int)newAngles.z;
//******** HERE IS COLOR MATCHING*******
if (node.transform.parent.name=="Node1" && node.sticks[i].degree == 90)
{
colorMatch[1] = node.sticks[i].color;
Debug.Log("COLOR 1___"+ colorMatch[1]);
//Debug.Log(colorMatch1);
}
if (node.transform.parent.name == "Node2" && node.sticks[i].degree == 90)
{
colorMatch[2] = node.sticks[i].color;
Debug.Log("COLOR 2___" + colorMatch[2]);
}
if (node.transform.parent.name == "Node3" && node.sticks[i].degree == 90)
{
colorMatch[3] = node.sticks[i].color;
Debug.Log("COLOR 3___" + colorMatch[3]);
//if (colorMatch[1] == colorMatch[2] && colorMatch[2] == colorMatch[3])
//{
// Debug.Log("COLORS MATCHED : " + colorMatch[1]);
//}
}
if (colorMatch[1]==colorMatch[2] && colorMatch[2]==colorMatch[3])
{
Debug.Log("COLOR MATCHED");
}
}
}
}
else
{
Node currNode = rayhit.transform.GetComponent<Node>();
if(currNode.isMoved == false)
{
smallestId = 0;
smallestDistance = 999;
for (int i = 0; i < nodes.Length; i++)
{
float distance = Vector2.Distance(rayhit.transform.position, nodes[i].transform.position);
if (smallestDistance > distance)
{
smallestDistance = distance;
smallestId = i;
}
}
rayhit.transform.position = nodes[smallestId].transform.position;
if (rayhit.transform.parent != nodes[smallestId].transform)
{
if (nodes[smallestId].transform.childCount > 0 && nodes[smallestId].transform != rayhit.transform.parent)
{
if (currNode != null)
{
for (int i = 0; i < currNode.sticks.Count; i++)
{
nodes[smallestId].transform.GetChild(0).GetComponent<Node>().sticks.Add(currNode.sticks[i]);
currNode.sticks[i].transform.SetParent(nodes[smallestId].transform.GetChild(0));
}
Destroy(rayhit.transform.gameObject);
}
}
else
{
if (currNode != null)
{
currNode.isMoved = true;
}
rayhit.transform.SetParent(nodes[smallestId].transform);
}
}
}
}
}
rayhit = new RaycastHit2D();
}
else if (Input.GetMouseButton(0))
{
if(rayhit.transform != null)
{
Node currNode = rayhit.transform.GetComponent<Node>();
if(currNode != null)
if (currNode.isMoved == false)
{
if (Time.time - clickTime >= 0.2f)
{
Vector2 newPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
rayhit.transform.position = newPos;
}
}
}
}
}
if (node.transform.parent.name=="Node1" && node.sticks[i].degree == 90)
{
colorMatch[1] = node.sticks[i].color;
Debug.Log("COLOR 1___"+ colorMatch[1]);
//Debug.Log(colorMatch1);
}
if (node.transform.parent.name == "Node2" && node.sticks[i].degree == 90)
{
colorMatch[2] = node.sticks[i].color;
Debug.Log("COLOR 2___" + colorMatch[2]);
}
if (node.transform.parent.name == "Node3" && node.sticks[i].degree == 90)
{
colorMatch[3] = node.sticks[i].color;
Debug.Log("COLOR 3___" + colorMatch[3]);
if (colorMatch[1] == colorMatch[2] && colorMatch[2] == colorMatch[3])
{
Debug.Log("COLORS MATCHED : " + colorMatch[1]);
}
}
Here is working code. but how i can destroy the matched sticks?

How to make my code evaluate the whole arithmetic expression?

I am trying to make a string calculator, it works fine with two numbers but I always encounter a problem when evaluating multiple operations:
7*2+4=
Also can you help me with my multiplication and division code. I don't understand why it prints 0 even with just two numbers(7*5)
using System;
using System.Text.RegularExpressions;
namespace Sariling_Calcu
{
class Program
{
private static string exp;
private static int[] i = new int[1000];
private static char[] oper = new char[10];
private static int cntr2;
private static int result;
private static int pluscount;
private static int subcount;
private static int mulcount;
private static int divcount;
static void getNum()
{
string[] strNum = (Regex.Split(exp, #"\D+"));
for (int cntr = 0; cntr < exp.Length; cntr++)
{
foreach (string item in strNum)
{
if (!string.IsNullOrEmpty(item))
{
i[cntr] = int.Parse(item);
cntr += 1;
}
}
}
}
static void counter()
{
for (int cntr = 0; cntr < exp.Length; cntr++)
{
if (exp[cntr] == '+')
{
pluscount++;
}
else if (exp[cntr] == '-')
{
subcount++;
}
else if (exp[cntr] == '*')
{
mulcount++;
}
else if (exp[cntr] == '/')
{
divcount--;
}
}
}
static void oprtr()
{
for (int cntr = 0; cntr < exp.Length; cntr++)
{
if (exp[cntr] != '1'
&& exp[cntr] != '2'
&& exp[cntr] != '3'
&& exp[cntr] != '4'
&& exp[cntr] != '5'
&& exp[cntr] != '6'
&& exp[cntr] != '7'
&& exp[cntr] != '8'
&& exp[cntr] != '9')
{
if (exp[cntr] == '+')
{
result += i[cntr2];
cntr2 += 1;
pluscount--;
if (pluscount == 0 && subcount == 0 && mulcount==0 && divcount==0)
{
cntr2 += 3;
result += i[cntr2];
}
}
else if (exp[cntr] == '-')
{
result -= i[cntr2];
cntr2 += 1;
subcount--;
result = -result;
if (pluscount == 0 && subcount == 0 && mulcount == 0 && divcount == 0)
{
cntr2 += 3;
result -= i[cntr2];
}
}
else if (exp[cntr] == '*')
{
if (result == 0)
{
result += 1;
}
result *= i[cntr2];
cntr2 += 1;
mulcount--;
if (pluscount == 0 && subcount == 0 && mulcount == 0 && divcount == 0)
{
cntr2 += 3;
result *= i[cntr2];
}
}
else if (exp[cntr] == '/')
{
if (result == 0)
{
result += 1;
}
result /= i[cntr2];
cntr2 += 1;
divcount--;
if (pluscount == 0 && subcount == 0 && mulcount == 0 && divcount == 0)
{
cntr2 += 3;
result /= i[cntr2];
}
}
}
}
}
static void Main(string[] args)
{
Console.Write("Expression: ");
exp = Console.ReadLine();
counter();
getNum();
oprtr();
Console.Write("Answer: \n" + result);
Console.ReadLine();
}
}
}
you could use LINQ to reduce your code to a few lines but looks like its a school assignment where you would have to go with Arrays and loops.
I have tried to refine your code a bit and did a few fixes, change getNum(), counter() and oprtr() methods as below and let me know if it works, then I would add some comments in the code to explain changes I made.
static void getNum()
{
string[] strNum = (Regex.Split(exp, #"\D+"));
for (int cntr = 0; cntr < strNum.Length; cntr++)
{
if (!string.IsNullOrEmpty(strNum[cntr]))
{
i[cntr] = int.Parse(strNum[cntr]);
}
}
}
static void counter()
{
cntr2 = 0;
for (int cntr = 0; cntr < exp.Length; cntr++)
{
if (exp[cntr] == '+')
{
oper[cntr2] = '+';
cntr2++;
}
else if (exp[cntr] == '-')
{
oper[cntr2] = '-';
cntr2++;
}
else if (exp[cntr] == '*')
{
oper[cntr2] = '*';
cntr2++;
}
else if (exp[cntr] == '/')
{
oper[cntr2] = '/';
cntr2++;
}
}
}
static void oprtr()
{
result = i[0];
cntr2 = 1;
for (int cntr = 0; cntr < oper.Length; cntr++)
{
if (oper[cntr] == '+')
{
result += i[cntr2];
}
else if (oper[cntr] == '-')
{
result -= i[cntr2];
}
else if (oper[cntr] == '*')
{
result *= i[cntr2];
}
else if (oper[cntr] == '/')
{
if (i[cntr2] == 0)
{
throw new DivideByZeroException();
}
result /= i[cntr2];
}
cntr2 += 1;
}
}

How to fix this memory error

After googling it, I think it is a memory leak but I do not know how to fix this.
Assertion failed: TLS Allocator ALLOC_TEMP_THREAD, underlying allocator ALLOC_TEMP_THREAD has unfreed allocations, size 1528
Im Using Unity 2018.1.0.0f2.
Here is the script that causes the error (Sorry for my awful code organization skills, new to world generation)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WorldGeneration : MonoBehaviour {
public float GenerationDelay;
public float RoomPicker;
public float LevelNumber;
public float RoomCount;
public float MaxRoomCount;
public float LevelSample1Rooms;
public float MaxLevelSample1Rooms;
public float LevelSample2Rooms;
public float MaxLevelSample2Rooms;
public float LevelSample3Rooms;
public float MaxLevelSample3Rooms;
public float LevelSample4Rooms;
public float MaxLevelSample4Rooms;
public float LevelSample5Rooms;
public float MaxLevelSample5Rooms;
public GameObject LevelSample1;
public GameObject LevelSample2;
public GameObject LevelSample3;
public GameObject LevelSample4;
public GameObject LevelSample5;
public Transform SpawnLevelPoint;
public Transform SpawnLevelPoint1;
public bool Leveloadedatpoint1;
public Transform SpawnLevelPoint2;
public bool Leveloadedatpoint2;
public Transform SpawnLevelPoint3;
public bool Leveloadedatpoint3;
public Transform SpawnLevelPoint4;
public bool Leveloadedatpoint4;
public Transform SpawnLevelPoint5;
public bool Leveloadedatpoint5;
public Transform SpawnLevelPoint6;
public bool Leveloadedatpoint6;
void Start()
{
StartCoroutine("Createpoint1room");
}
public IEnumerator Createpoint1room()
{
if (Leveloadedatpoint1 == false)
{
RoomPicker = Random.Range(1, 20);
yield return new WaitForSeconds(GenerationDelay);
if (RoomPicker == 1 || RoomPicker == 2 || RoomPicker == 3 || RoomPicker == 4 || RoomPicker == 5)
{
if (Leveloadedatpoint1 == false)
{
if (LevelSample1Rooms < MaxLevelSample1Rooms)
{
LevelSample1Rooms += 1;
Instantiate(LevelSample1, SpawnLevelPoint1.transform.position, Quaternion.identity);
Leveloadedatpoint1 = true;
yield return new WaitForSeconds(GenerationDelay);
StartCoroutine("Createpoint2room");
}
else
{
StartCoroutine("Createpoint1room");
}
}
else
{
StartCoroutine("Createpoint1room");
}
}
if (RoomPicker == 6 || RoomPicker == 7 || RoomPicker == 8)
{
if (Leveloadedatpoint1 == false)
{
if (LevelSample2Rooms < MaxLevelSample2Rooms)
{
LevelSample2Rooms += 1;
Instantiate(LevelSample2, SpawnLevelPoint1.transform.position, Quaternion.identity);
Leveloadedatpoint1 = true;
yield return new WaitForSeconds(GenerationDelay);
StartCoroutine("Createpoint2room");
}
else
{
StartCoroutine("Createpoint1room");
}
}
else
{
StartCoroutine("Createpoint1room");
}
}
if (RoomPicker == 9 || RoomPicker == 10 || RoomPicker == 11)
{
if (Leveloadedatpoint1 == false)
{
if (LevelSample3Rooms < MaxLevelSample3Rooms)
{
LevelSample3Rooms += 1;
Instantiate(LevelSample3, SpawnLevelPoint1.transform.position, Quaternion.identity);
Leveloadedatpoint1 = true;
yield return new WaitForSeconds(GenerationDelay);
StartCoroutine("Createpoint2room");
}
else
{
StartCoroutine("Createpoint1room");
}
}
else
{
StartCoroutine("Createpoint1room");
}
}
if (RoomPicker == 12 || RoomPicker == 13 || RoomPicker == 14)
{
if (Leveloadedatpoint1 == false)
{
if (LevelSample4Rooms < MaxLevelSample4Rooms)
{
LevelSample4Rooms += 1;
Instantiate(LevelSample4, SpawnLevelPoint1.transform.position, Quaternion.identity);
Leveloadedatpoint1 = true;
yield return new WaitForSeconds(GenerationDelay);
StartCoroutine("Createpoint2room");
}
else
{
StartCoroutine("Createpoint1room");
}
}
else
{
StartCoroutine("Createpoint1room");
}
}
if (RoomPicker == 15 || RoomPicker == 16 || RoomPicker == 17)
{
if (Leveloadedatpoint1 == false)
{
if (LevelSample5Rooms < MaxLevelSample5Rooms)
{
LevelSample5Rooms += 1;
Instantiate(LevelSample5, SpawnLevelPoint1.transform.position, Quaternion.identity);
Leveloadedatpoint1 = true;
yield return new WaitForSeconds(GenerationDelay);
StartCoroutine("Createpoint2room");
}
else
{
StartCoroutine("Createpoint1room");
}
}
else
{
StartCoroutine("Createpoint1room");
}
}
if (RoomPicker == 18 || RoomPicker == 19 || RoomPicker == 20)
{
StartCoroutine("Createpoint1room");
}
Debug.Log("Point 1: The number is " + RoomPicker);
}
else
{
StartCoroutine("Createpoint2room");
}
}
public IEnumerator Createpoint2room()
{
if (Leveloadedatpoint2 == false)
{
RoomPicker = Random.Range(1, 20);
yield return new WaitForSeconds(GenerationDelay);
if (RoomPicker == 1 || RoomPicker == 2 || RoomPicker == 3 || RoomPicker == 4 || RoomPicker == 5)
{
if (Leveloadedatpoint2 == false)
{
if (LevelSample1Rooms < MaxLevelSample1Rooms)
{
LevelSample1Rooms += 1;
Instantiate(LevelSample1, SpawnLevelPoint2.transform.position, Quaternion.identity);
Leveloadedatpoint2 = true;
yield return new WaitForSeconds(GenerationDelay);
StartCoroutine("Createpoint3room");
}
else
{
StartCoroutine("Createpoint2room");
}
}
else
{
StartCoroutine("Createpoint2room");
}
}
if (RoomPicker == 6 || RoomPicker == 7 || RoomPicker == 8)
{
if (Leveloadedatpoint2 == false)
{
if (LevelSample2Rooms < MaxLevelSample2Rooms)
{
LevelSample2Rooms += 1;
Instantiate(LevelSample2, SpawnLevelPoint2.transform.position, Quaternion.identity);
Leveloadedatpoint2 = true;
yield return new WaitForSeconds(GenerationDelay);
StartCoroutine("Createpoint3room");
}
else
{
StartCoroutine("Createpoint2room");
}
}
else
{
StartCoroutine("Createpoint2room");
}
}
if (RoomPicker == 9 || RoomPicker == 10 || RoomPicker == 11)
{
if (Leveloadedatpoint2 == false)
{
if (LevelSample3Rooms < MaxLevelSample3Rooms)
{
LevelSample3Rooms += 1;
Instantiate(LevelSample3, SpawnLevelPoint2.transform.position, Quaternion.identity);
Leveloadedatpoint2 = true;
yield return new WaitForSeconds(GenerationDelay);
StartCoroutine("Createpoint3room");
}
else
{
StartCoroutine("Createpoint2room");
}
}
else
{
StartCoroutine("Createpoint2room");
}
}
if (RoomPicker == 12 || RoomPicker == 13 || RoomPicker == 14)
{
if (Leveloadedatpoint2 == false)
{
if (LevelSample4Rooms < MaxLevelSample4Rooms)
{
LevelSample4Rooms += 1;
Instantiate(LevelSample4, SpawnLevelPoint2.transform.position, Quaternion.identity);
Leveloadedatpoint2 = true;
yield return new WaitForSeconds(GenerationDelay);
StartCoroutine("Createpoint3room");
}
else
{
StartCoroutine("Createpoint2room");
}
}
else
{
StartCoroutine("Createpoint2room");
}
}
if (RoomPicker == 15 || RoomPicker == 16 || RoomPicker == 17)
{
if (Leveloadedatpoint2 == false)
{
if (LevelSample5Rooms < MaxLevelSample5Rooms)
{
LevelSample5Rooms += 1;
Instantiate(LevelSample5, SpawnLevelPoint2.transform.position, Quaternion.identity);
Leveloadedatpoint2 = true;
yield return new WaitForSeconds(GenerationDelay);
StartCoroutine("Createpoint3room");
}
else
{
StartCoroutine("Createpoint2room");
}
}
else
{
StartCoroutine("Createpoint2room");
}
}
if (RoomPicker == 18 || RoomPicker == 19 || RoomPicker == 20)
{
StartCoroutine("Createpoint2room");
}
Debug.Log("Point 2: The number is " + RoomPicker);
}
else
{
StartCoroutine("Createpoint3room");
}
}
public IEnumerator Createpoint3room()
{
if (Leveloadedatpoint3 == false)
{
RoomPicker = Random.Range(1, 20);
yield return new WaitForSeconds(GenerationDelay);
if (RoomPicker == 1 || RoomPicker == 2 || RoomPicker == 3 || RoomPicker == 4 || RoomPicker == 5)
{
if (Leveloadedatpoint3 == false)
{
if (LevelSample1Rooms < MaxLevelSample1Rooms)
{
LevelSample1Rooms += 1;
Instantiate(LevelSample1, SpawnLevelPoint3.transform.position, Quaternion.identity);
Leveloadedatpoint3 = true;
yield return new WaitForSeconds(GenerationDelay);
StartCoroutine("Createpoint4room");
}
else
{
StartCoroutine("Createpoint3room");
}
}
else
{
StartCoroutine("Createpoint3room");
}
}
if (RoomPicker == 6 || RoomPicker == 7 || RoomPicker == 8)
{
if (Leveloadedatpoint3 == false)
{
if (LevelSample2Rooms < MaxLevelSample2Rooms)
{
LevelSample2Rooms += 1;
Instantiate(LevelSample2, SpawnLevelPoint3.transform.position, Quaternion.identity);
Leveloadedatpoint3 = true;
yield return new WaitForSeconds(GenerationDelay);
StartCoroutine("Createpoint4room");
}
else
{
StartCoroutine("Createpoint3room");
}
}
else
{
StartCoroutine("Createpoint3room");
}
}
if (RoomPicker == 9 || RoomPicker == 10 || RoomPicker == 11)
{
if (Leveloadedatpoint3 == false)
{
if (LevelSample3Rooms < MaxLevelSample3Rooms)
{
LevelSample3Rooms += 1;
Instantiate(LevelSample3, SpawnLevelPoint3.transform.position, Quaternion.identity);
Leveloadedatpoint3 = true;
yield return new WaitForSeconds(GenerationDelay);
StartCoroutine("Createpoint4room");
}
else
{
StartCoroutine("Createpoint3room");
}
}
else
{
StartCoroutine("Createpoint3room");
}
}
if (RoomPicker == 12 || RoomPicker == 13 || RoomPicker == 14)
{
if (Leveloadedatpoint3 == false)
{
if (LevelSample4Rooms < MaxLevelSample4Rooms)
{
LevelSample4Rooms += 1;
Instantiate(LevelSample4, SpawnLevelPoint3.transform.position, Quaternion.identity);
Leveloadedatpoint3 = true;
yield return new WaitForSeconds(GenerationDelay);
StartCoroutine("Createpoint4room");
}
else
{
StartCoroutine("Createpoint3room");
}
}
else
{
StartCoroutine("Createpoint3room");
}
}
if (RoomPicker == 15 || RoomPicker == 16 || RoomPicker == 17)
{
if (Leveloadedatpoint3 == false)
{
if (LevelSample5Rooms < MaxLevelSample5Rooms)
{
LevelSample5Rooms += 1;
Instantiate(LevelSample5, SpawnLevelPoint3.transform.position, Quaternion.identity);
Leveloadedatpoint3 = true;
yield return new WaitForSeconds(GenerationDelay);
StartCoroutine("Createpoint4room");
}
else
{
StartCoroutine("Createpoint3room");
}
}
else
{
StartCoroutine("Createpoint3room");
}
}
if (RoomPicker == 18 || RoomPicker == 19 || RoomPicker == 20)
{
StartCoroutine("Createpoint3room");
}
Debug.Log("Point 3: The number is " + RoomPicker);
}
else
{
StartCoroutine("Createpoint4room");
}
}
public IEnumerator Createpoint4room()
{
if (Leveloadedatpoint4 == false)
{
RoomPicker = Random.Range(1, 20);
yield return new WaitForSeconds(GenerationDelay);
if (RoomPicker == 1 || RoomPicker == 2 || RoomPicker == 3 || RoomPicker == 4 || RoomPicker == 5)
{
if (Leveloadedatpoint4 == false)
{
if (LevelSample1Rooms < MaxLevelSample1Rooms)
{
LevelSample1Rooms += 1;
Instantiate(LevelSample1, SpawnLevelPoint4.transform.position, Quaternion.identity);
Leveloadedatpoint4 = true;
yield return new WaitForSeconds(GenerationDelay);
StartCoroutine("Createpoint5room");
}
else
{
StartCoroutine("Createpoint4room");
}
}
else
{
StartCoroutine("Createpoint4room");
}
}
if (RoomPicker == 6 || RoomPicker == 7 || RoomPicker == 8)
{
if (Leveloadedatpoint4 == false)
{
if (LevelSample2Rooms < MaxLevelSample2Rooms)
{
LevelSample2Rooms += 1;
Instantiate(LevelSample2, SpawnLevelPoint4.transform.position, Quaternion.identity);
Leveloadedatpoint4 = true;
yield return new WaitForSeconds(GenerationDelay);
StartCoroutine("Createpoint5room");
}
else
{
StartCoroutine("Createpoint4room");
}
}
else
{
StartCoroutine("Createpoint4room");
}
}
if (RoomPicker == 9 || RoomPicker == 10 || RoomPicker == 11)
{
if (Leveloadedatpoint4 == false)
{
if (LevelSample3Rooms < MaxLevelSample3Rooms)
{
LevelSample3Rooms += 1;
Instantiate(LevelSample3, SpawnLevelPoint4.transform.position, Quaternion.identity);
Leveloadedatpoint4 = true;
yield return new WaitForSeconds(GenerationDelay);
StartCoroutine("Createpoint5room");
}
else
{
StartCoroutine("Createpoint4room");
}
}
else
{
StartCoroutine("Createpoint4room");
}
}
if (RoomPicker == 12 || RoomPicker == 13 || RoomPicker == 14)
{
if (Leveloadedatpoint4 == false)
{
if (LevelSample4Rooms < MaxLevelSample4Rooms)
{
LevelSample4Rooms += 1;
Instantiate(LevelSample4, SpawnLevelPoint4.transform.position, Quaternion.identity);
Leveloadedatpoint3 = true;
yield return new WaitForSeconds(GenerationDelay);
StartCoroutine("Createpoint4room");
}
else
{
StartCoroutine("Createpoint4room");
}
}
else
{
StartCoroutine("Createpoint4room");
}
}
if (RoomPicker == 15 || RoomPicker == 16 || RoomPicker == 17)
{
if (Leveloadedatpoint3 == false)
{
if (LevelSample5Rooms < MaxLevelSample5Rooms)
{
LevelSample5Rooms += 1;
Instantiate(LevelSample5, SpawnLevelPoint4.transform.position, Quaternion.identity);
Leveloadedatpoint3 = true;
yield return new WaitForSeconds(GenerationDelay);
StartCoroutine("Createpoint5room");
}
else
{
StartCoroutine("Createpoint4room");
}
}
else
{
StartCoroutine("Createpoint4room");
}
}
if (RoomPicker == 18 || RoomPicker == 19 || RoomPicker == 20)
{
StartCoroutine("Createpoint4room");
}
Debug.Log("Point 4: The number is " + RoomPicker);
}
else
{
StartCoroutine("Createpoint4room");
}
}
public IEnumerator Createpoint5room()
{
yield return new WaitForSeconds(GenerationDelay);
Debug.Log("Generation finished");
}
}
Edit1:
This error also seems to crash my Editor and crashes the build of the game as well.
I was getting tons of Stack allocator ALLOC_TEMP_THREAD has unfreed allocations, size ... messages. Which is almost, but not exactly the same message you're getting, but that might be because of the different Unity version (I'm on 2018.2.9).
But as described here, simply restarting Unity fixed it for me.
The real solution is really simple and doesn't require the restart of Unity. Just go to preferences and under GI tab, hit Clean Cache button. No more errors for you !-)
I had the same issue in Unity 2021, I tried the Clean Cache method and restarted Unity. The memory error was gone but some new warnings about missing referenced script showed up.

Performance issue with traversing&&filtering datagridview in c#

Currently, I am doing a log analyzer for my personal project.
My issue here is that I am new to c# and I have an performance issue with my tool.
Everytime the device(iOS) is interacted, I get an output syslog from a library and it comes in to the output handler.
public void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine, iosSyslogger form, string uuid)
{
string currentPath = System.Environment.CurrentDirectory;
bool exit = false;
if (exit == true) return;
try
{
form.BeginInvoke(new Action(() =>
{
form.insertLogText = outLine.Data;
}));
using (System.IO.StreamWriter file = new System.IO.StreamWriter(currentPath + #"\syslog" + uuid + ".txt", true))
{
file.WriteLine(outLine.Data);
}
}
catch
{
return ;
}
//*Most of the logic for outputing the log should be dealt from this output Handler
}
Then, I write the outline.Data to Data grid view. My concern is that I need to be able to search and filter through data gridview.
Curently I am using visibility = false for search filtering ( if the row does not match the given filter specification I set the row to visibility =false)
This requires the program to traverse the entire datagridview to check whether the condition is met.
Will there be any better way to filter and search within ?
(When I have thousands of lines of row, it takes at least 20 seconds to process it)
Below is the code for filtering, and searching through the results function.
private void searchResult(string term)
{
if (term != null)
{
int i = 0;
while (i < dataGridView1.Rows.Count - 1)
{
if (dataGridView1.Rows[i].Cells[3] == null)
{
i++;
continue;
}
if (dataGridView1.Rows[i].Visible == false)
{
i++;
continue;
}
else if (dataGridView1.Rows[i].Cells[2].Value.ToString().Contains(term) || dataGridView1.Rows[i].Cells[3].Value.ToString().Contains(term) || dataGridView1.Rows[i].Cells[4].Value.ToString().Contains(term))
{
string multirow = dataGridView1.Rows[i].Cells[5].Value.ToString();
int count = Convert.ToInt32(multirow);
if (count > 0)
{
int z = 0;
for (z = 0; z <= count; z++)
{
dataGridView1.Rows[i + z].Visible = true;
}
i = i + z;
}
else
{
dataGridView1.Rows[i].Visible = true;
i++;
}
}
else
{
dataGridView1.Rows[i].Visible = false;
i++;
}
}
}
}
public filteringThelist(){
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
dataGridView1.Rows[i].Visible = false;
int count1,count2,count3=0;
count1 = 1;
count2 = 1;
count3 = 1;
int z = 0;
foreach (KeyValuePair<string, string> entry in totalSelected)
{
if (entry.Value == "devicename" && dataGridView1.Rows[i].Cells[1].Value != null)
{
if (dataGridView1.Rows[i].Cells[1].Value.ToString().Trim().Equals(entry.Key.Trim()))
{
string multirow1 = dataGridView1.Rows[i].Cells[5].Value.ToString();
int counts = Convert.ToInt32(multirow1);
if (counts > 0)
{
for (z = 0; z < counts; z++)
{
dataGridView1.Rows[i + z].Visible = true;
}
}
else
{
dataGridView1.Rows[i].Visible = true;
}
}
else if (devicename.CheckedItems.Count > 1&&count1!= devicename.CheckedItems.Count)
{
count1++;
continue;
}
else
{
dataGridView1.Rows[i].Visible = false;
break;
}
}
else if (entry.Value == "process" && dataGridView1.Rows[i].Cells[2].Value != null)
{
if (dataGridView1.Rows[i].Cells[2].Value.ToString().Trim().Equals(entry.Key.Trim()))
{
string multirow1 = dataGridView1.Rows[i].Cells[5].Value.ToString();
int counts = Convert.ToInt32(multirow1);
if (counts > 0)
{
for (z = 0; z < counts; z++)
{
dataGridView1.Rows[i + z].Visible = true;
}
}
else
{
dataGridView1.Rows[i].Visible = true;
}
}
else if (processlistname.CheckedItems.Count > 1 && count2 != processlistname.CheckedItems.Count)
{
count2++;
continue;
}
else
{
dataGridView1.Rows[i].Visible = false;
break;
}
}
else if (entry.Value == "loglevel" && dataGridView1.Rows[i].Cells[3].Value != null)
{
if (dataGridView1.Rows[i].Cells[3].Value.ToString().Trim().Contains(entry.Key.Trim()))
{
string multirow1 = dataGridView1.Rows[i].Cells[5].Value.ToString();
int counts = Convert.ToInt32(multirow1);
if (counts > 0)
{
for (z = 0; z < counts; z++)
{
dataGridView1.Rows[i + z].Visible = true;
}
}
else
{
dataGridView1.Rows[i].Visible = true;
}
continue;
}
else if (loglevelCheckBox.CheckedItems.Count > 1 && count3 != loglevelCheckBox.CheckedItems.Count)
{
count3++;
continue;
}
else
{
dataGridView1.Rows[i].Visible = false;
break;
}
}
// do something with entry.Value or entry.Key
}
string multirow = dataGridView1.Rows[i].Cells[5].Value.ToString();
int count = Convert.ToInt32(multirow);
if (count > 0&& dataGridView1.Rows[i].Visible==false)
{
for (int k = 0; k <= count; k++)
{
dataGridView1.Rows[i + k].Visible = false;
}
}
i = i + z;
}
The performance problem is because your DataGridView is redrawing at each modification.
Don't fill the DataGridView directly from your Data. Rather create a DataTable and bind it to DataGridView with a BindingSource.
Then use the "Filter" property of the binding source to view extracts of dataTable in the DataGridView. The "Filter" property is a string whose content is similar to that of a WHERE SQL clause.

WPF Tree View event

I want to make an application in WPF same as windows application. When i use treeview event in wpf i did not find any event similar to treeview_NodeMouseClick of WIndows Application.
//Windows Application Code
private void tv_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Node != null)
{
GetAllchield(e.Node, e.Node.Level);
}
}
// GetAllchield
public void GetAllchield(TreeNode clickednode, int indexDepth)
{
if (clickednode.Nodes.Count > 0 && !clickednode.IsExpanded)
{
clickednode.Collapse();
return;
}
string[] FullPath = clickednode.FullPath.Split('\\');
string rootnode = FullPath[0].ToString();
//get all market for root event type
int eventTypeID = DictionaryAllActiveEventTypes[rootnode];
string[] allMarkets = GetAllMarketForEventID(eventTypeID);
//selecting unque chield node and populating in tree
for (int i = 0; i < allMarkets.Length; i++)
{
if (allMarkets[i].Contains(clickednode.Text))
{
string[] marketDetails = allMarkets[i].Split('~');
int marketID = Convert.ToInt32(marketDetails[0]);
string MarketName = marketDetails[1].ToString();
string MarketStatus = marketDetails[3].ToString();
string EventHeirarchy = marketDetails[6].ToString();
string Menupath = marketDetails[5].ToString();
string[] Arrmenupath = Menupath.Trim(':').Split('\\');
string chieldText = "";
if (indexDepth == 0)
{
if (rootnode == "Cricket" || rootnode == "Tennis" || rootnode == "Golf" || rootnode == "Rugby")
{
if (Arrmenupath[2].Contains("Group") && Arrmenupath[2].Length == 7)
{
if ((indexDepth + 3) <= Arrmenupath.Length - 1)
{
chieldText = Arrmenupath[indexDepth + 3].ToString();
}
}
else
{
if ((indexDepth + 2) <= Arrmenupath.Length - 1)
chieldText = Arrmenupath[indexDepth + 2].ToString();
}
}
else
if ((indexDepth + 2) <= Arrmenupath.Length - 1)
chieldText = Arrmenupath[indexDepth + 2].ToString();
}
else
{
if (Arrmenupath[Arrmenupath.Length - 1] == clickednode.Text)
chieldText = MarketName;
else
{
if (allMarkets[i].Contains(clickednode.Text) && allMarkets[i].Contains(clickednode.Parent.Text) && allMarkets[i].Contains(rootnode))
{
if (rootnode == "Cricket" || rootnode == "Tennis" || rootnode == "Golf" || rootnode == "Rugby")
{
if (Arrmenupath[2].Contains("Group") && Arrmenupath[2].Length == 7)
{
if ((indexDepth + 3) <= Arrmenupath.Length - 1)
{
chieldText = Arrmenupath[indexDepth + 3].ToString();
}
}
else
{
if ((indexDepth + 2) <= Arrmenupath.Length - 1)
chieldText = Arrmenupath[indexDepth + 2].ToString();
}
}
else
if ((indexDepth + 2) <= Arrmenupath.Length - 1)
chieldText = Arrmenupath[indexDepth + 2].ToString();
}
}
}
//check whether node is already added
//if (chieldText.Contains("MiWay"))
//{ }
if (!string.IsNullOrEmpty(chieldText))
{
if (clickednode.Nodes.Count >= 1)
{
bool doesNodeAlreadyExist = false;
foreach (TreeNode node in clickednode.Nodes)
{
if (node.Text == chieldText)
{
doesNodeAlreadyExist = true;
break;
}
}
if (!doesNodeAlreadyExist)
{
clickednode.Nodes.Add(chieldText);
}
}
else
{
clickednode.Nodes.Add(chieldText);
}
}
}
}
clickednode.Expand();
}
I want to use same as in WPF. Please help me or if you get it.
You can use MouseLeftButtonDown and check sender argument or use SelectedItemChanged

Categories

Resources