FowardElement Posted October 5, 2015 Share Posted October 5, 2015 Basically what I have: 1. List of coordinates 2. When player enters any of these coordinates -UI.Notify Now I have this working for each coordinate, but I need to make the coordinates into a list that I can update to make the code much cleaner. For some reason I cant wrap my head around this. I've searched the forums (List of coordinates, vector3 list, etc) and I cant come up with anything. Please be nice to this newbie and give me an example of creating a list of coordinates that I can use with Game.Player.Character. IsInRangeOf. Thanks. Link to comment Share on other sites More sharing options...
Jitnaught Posted October 5, 2015 Share Posted October 5, 2015 C#: Vector3[] coords = new Vector3[] { new Vector3(0, 0, 0), new Vector3(1, 1, 1) }; Link to comment Share on other sites More sharing options...
FowardElement Posted October 6, 2015 Author Share Posted October 6, 2015 Thanks for the info, but how would I use this for if (Game.Player.Character.IsInRangeOf(?, 1)) { UI.Notify("Is in range"); } Link to comment Share on other sites More sharing options...
Jitnaught Posted October 6, 2015 Share Posted October 6, 2015 foreach (Vector3 vec3 in coords) { if (Game.Player.Character.IsInRangeOf(vec3, 1)) { UI.Notify("Is in range"); break; } } Link to comment Share on other sites More sharing options...
FowardElement Posted October 6, 2015 Author Share Posted October 6, 2015 Yes, yes ,yes! Thank you so much! Jitnaught 1 Link to comment Share on other sites More sharing options...
bilago Posted October 6, 2015 Share Posted October 6, 2015 (edited) foreach (Vector3 vec3 in coords){ if (Game.Player.Character.IsInRangeOf(vec3, 1)) { UI.Notify("Is in range"); break; }} foreach is a lot more costly than a for() loop, I would avoid foreach like the plague when it comes to scripting for games. I would do one of these: List<Vector3> Coordinates = new List<Vector3>() { LocationA, LocationB, LocationC }; int index = Coordinates.FindIndex(x => Game.Player.Character.IsInRangeOf(x, 1)); if (index > -1) UI.Notify(string.Format("Location {0} is in range!", Coordinates[index])); or do this: for (int i = 0; i < Coordinates.Count; i++) { if (Game.Player.Character.IsInRangeOf(Coordinates[i], 1)) { UI.Notify("Is in range"); break; } } Edited October 6, 2015 by bilago Link to comment Share on other sites More sharing options...
Jitnaught Posted October 6, 2015 Share Posted October 6, 2015 foreach is a lot more costly than a for() loop, I would avoid foreach like the plague when it comes to scripting for games. www.dotnetperls.com/for-foreach Link to comment Share on other sites More sharing options...
GeorgeZhang Posted October 6, 2015 Share Posted October 6, 2015 You can edit(add/remove) elements in a list, so I prefer using Lists. Vector3 Loc1 = new Vector3(0, 0, 0);List<Vector3> Locs = new List<Vector3> { Loc1, new Vector3(0, 0, 0) };Locs.Add(Loc1);Locs.RemoveAt(0); Link to comment Share on other sites More sharing options...
bilago Posted October 6, 2015 Share Posted October 6, 2015 (edited) You can edit(add/remove) elements in a list, so I prefer using Lists. Vector3 Loc1 = new Vector3(0, 0, 0);List<Vector3> Locs = new List<Vector3> { Loc1, new Vector3(0, 0, 0) };Locs.Add(Loc1);Locs.RemoveAt(0); I agree, but if you have data that doesn't need to change, arrays perform faster than lists. Edited October 6, 2015 by bilago leftas 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now