Jump to content
  • Advertisement
ethancodes

C# assigning a property from an object in a list to a variable

Recommended Posts

Maybe I didn't explain it clear enough in my last post. I have mixed my lists up. 

 

currentPosition = gridPositions[randomIndex].Id;

Here I am assigning the Id to currentPosition. What you all are saying is correct. Id is equal to the index. However, the code SHOULD have been like this:

currentPosition = gridOptions[randomIndex].Id;

Here I am assigning the Id of the index of gridOptions to currentPosition. This is what I actually meant to do. The Id here will not be equal to the index because gridOptions can only ever have up to 4 elements. But I need to track the where those 4 elements came from in the gridPositions list. So instead of comparing vector3's till I find a match, I just created an Id.

Share this post


Link to post
Share on other sites
Advertisement

You should really work on your variable naming. A variable named currentPosition shouldn't point to an identifier. A position is a position.

Share this post


Link to post
Share on other sites
12 minutes ago, Lactose said:

You should really work on your variable naming. A variable named currentPosition shouldn't point to an identifier. A position is a position.

I guess I can understand from an outside point of view, but what would you suggest I name it? currentPositionId? It may be an identifier but it's an identifier for a position. It made perfect sense to me. But I can see where it could be confusing if you don't know everything that's going on.

Share this post


Link to post
Share on other sites
6 minutes ago, ethancodes said:

suggest I name it? currentPositionId?

currentGridId because it will in fact get the position from the grid. That would be my choice.

3 hours ago, ethancodes said:

The Id here will not be equal to the index because gridOptions can only ever have up to 4 elements.

It is still possible, if less likely, to have the same index. Maybe use some kind of ID structure to avoid confusion example 10001 followed by 10002 etc. Or a string ID like Bethesda uses "0000000f" etc.

 

Is your code doing what you want it to do now?

Share this post


Link to post
Share on other sites

It seems to have fixed part of the problem but it's still doing some weird stuff. Earlier someone had said that how I was getting my random was not correct. This part here:

randomIndex = Random.Range (1, gridOptions.Count);

How should I be doing it if not like this?

Share this post


Link to post
Share on other sites
15 minutes ago, ethancodes said:

How should I be doing it if not like this?

You are starting from 1. So if there was 4 values you would get: 1, 2 and 3. However a array starts from 0 so you need 0,1,2,3; for the index. By starting from 1 you can never get the first item in the array unless you subtract 1, but then the last item can't be reached.

Share this post


Link to post
Share on other sites

Just as an update. I did find 2 more issues. One was a simple mathematical mistake. Now it is working the way I want mostof the time (except maybe the randomIndex part, but I'm not sure if that's an actual issue yet or not?) However, in edge cases, where I wouldn't assign something to the gridOptions list, I'm getting an error. The reason for this error is kind of confusing to me. When I debug, it shows that this list is already predefined with 4 elements all set to null. I'm confused because I thought lists should be dynamic? Maybe there's something I'm not familiar with. But this is a problem because if I have a null, then I get an error. Any ideas on why this would happen?

Just now, Scouting Ninja said:

You are starting from 1. So if there was 4 values you would get: 1, 2 and 3. However a array starts from 0 so you need 0,1,2,3; for the index. By starting from 1 you can never get the first item in the array unless you subtract 1, but then the last item can't be reached.

Yea I caught that and changed it, thanks. Not sure how I did that. Other than that it's ok though?

Share this post


Link to post
Share on other sites
11 minutes ago, ethancodes said:

Yea I caught that and changed it, thanks. Not sure how I did that. Other than that it's ok though?

Yes. The rest is correct. If you are wondering why my random is done differently it is because as a Trading Card Game(Yes real trading so they count as gambling games) developer I use a true random library that includes the last number.

I think with the C# random if you did Random.Range(0:1) it only returns 0.

18 minutes ago, ethancodes said:

it shows that this list is already predefined with 4 elements all set to null

Isn't that what you want? If there wasn't a null value it would have given a "Index out of range" error, with null objects you can check if the object is null. You can set a list to be empty by creating a empty one: 

List<Object> MyList = new List<Object>(); //This list has no values

MyList.add();//Use this to add object to the list
MyList.RemoveAt(Index);//Removes the item at index
MyList.Remove(Object);//Removes object from list

//If the null objects remain:
MyList.RemoveAll(Object => Object == null); //Look just realize today that c# supports Lambda and already my bad habits are back :P

 

Share this post


Link to post
Share on other sites

Yea you're right I can just check to see if it's null and then adjust if it is. Maybe I don't fully understand how lists work, but I thought they were supposed to be dynamic, so I was confused why I would have null elements? The way I had the code set up is so it would just pick any element in the list. So it didn't matter how long it was. If there was only one option, then it picks that one. However, now if there is only one option, it may pick a null, which means I need to do more work to make it check the next, then the next, and so on until it finds one that has something. My main question here is why is it making null elements? Why isn't it acting dynamically? From what I found online, the only thing I saw was maybe because its an object that's going in there? Any clarification on this would be great.

Share this post


Link to post
Share on other sites
5 hours ago, ethancodes said:

My main question here is why is it making null elements? Why isn't it acting dynamically?

It is dynamic. You have to use Add and Remove to keep it dynamic. Also don't give it a defined size at start:

List<int> MyIntList = new List<int>(0);//Make a empty list
print (MyIntList.Count); //returns 0

MyIntList.Add (1);
print (MyIntList.Count); //returns 1

MyIntList.RemoveAt (0);  //remove the first item
print (MyIntList.Count); //returns 0

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!