Search Unity

How can i assign values to variables within objects in a 2d list radiating out from a given locatio

Discussion in 'Scripting' started by NinjaVampireRobot, Jul 25, 2017.

  1. NinjaVampireRobot

    NinjaVampireRobot

    Joined:
    Jul 25, 2017
    Posts:
    3
    Sorry that is probably a confusing question for a title. Im kind of a beginner here and i hope I'm using the correct terms. But any help would be appreciated even if its just pointing me in a general direction.

    Im using c#. I have a 2d list of objects(all being built with the same class). I want to go to a point in that 2d list and then give variables in the objects next to the starting obj a value depending on how far away from it is. I included a diagram below to illustrate what I'm saying a bit.

    Screen Shot 2017-07-25 at 12.33.19 PM.png

    As shown above If I were to go to the object stored in myList[5][5] I want to be able to make
    mylist[4][5].myvar = 3;
    mylist[3][5].myvar = 2;

    and so forth making a radiating pattern with the original object as the focal point.

    I have been able to do it with set distances from the origin point by just hard coding all the values but if i need to make it more dynamic say a radius of 4(instead of the above 3) one time but 7 another time I cant seem to wrap my head around what i need to do. i have been playing with for loops but it is getting really messy and i can't seem to get anything to work. Any suggestions?
     
  2. Basen1

    Basen1

    Joined:
    Dec 19, 2016
    Posts:
    76
    Would this work value = Mathf.Abs(radius - dist.x - dist.y); ?

    Assuming this works in a tile based system and that they are all spaced 1 spot apart?
     
  3. NinjaVampireRobot

    NinjaVampireRobot

    Joined:
    Jul 25, 2017
    Posts:
    3

    I like that I'm assuming I would define radius and then the others I could calculate the distance.

    the problem im running into is how do i go about only focusing on the objects i want to alter. do i have to use a loop to look at the entire 2d list and just do nothing to the objects that dont fall with in the range? or is there some way to just hone in on the objects within the range?

    i hope that makes sense...
     
  4. Basen1

    Basen1

    Joined:
    Dec 19, 2016
    Posts:
    76
    You need to reference the object you are calculating the values FROM, save that position. And then if you need the values for all of them then just use a for loop otherwise you can just use the reference position and calculate the value for those.

    You will have to go through the entire list yeah, like this:
    foreach(item i in List){
    if(Mathf.Abs(i.transform.position.x - referencePosition.x) < radius && Mathf.Abs(i.transform.position.y - referencePosition.y) < radius){
    //Do something with them
    }
    }
     
  5. NinjaVampireRobot

    NinjaVampireRobot

    Joined:
    Jul 25, 2017
    Posts:
    3

    Thanks you pointed me in the in the right direction. the code looked close to the following once i got it fixed up with my 2d list.

    List<List<someClass>> my2DList = some2DListIMade;

    void radiateChange(int originX, int originY, int areaRadius){
    //find start and end points of area containing the radiating effect
    int startX = originX - areaRadius;
    int startY = originY - areaRadius;
    int endX = originX + areaRadius;
    int endY = originY + areaRadius;

    // make sure start and end points are positions within the 2DList
    if(startX < 0){
    startX = 0;
    }
    if(startY < 0){
    startY = 0;
    }
    if(endX > my2DList.Count){
    endX = my2DList.Count;
    }
    if(endY > my2DList[0].Count){
    endY = my2DList[0].Count;
    }
    // create a for loop that checks to see if the distance(measured by absolute value of the difference) is within the radius and then only alters the class in that position in what ever way you want.
    for(int currentX = startX; currentX < endX; currentX++){
    for(int currentY = startY; currentY< endY; currentY++){
    int distanceFromFocalPoint = Math.Abs(originX - currentX) + Math.Abs(originY - currentY);
    if(distanceFromFocalPoint <= areaRadius){
    my2DList[currentX][currentY].someVariable = somevalue;
    }
    }
    }
    }



    thanks for the help.
     
    Basen1 likes this.