Less code and avoiding unecessary If-statements
Hello everyone.
For example, let's say I've this code:
public void SetSpotsSprites () {
for(int i = 0; i< AvailableVehicles.Count; i++){
if(activeVehicle.name == AvailableVehicles[i].vehicleName){
if(AvailableVehicles[i].spot01Weapon != null)
spotsSprites[0].GetComponent<Image>().sprite = Resources.Load <Sprite> ("StoreSprites/Weapons/"+AvailableVehicles[i].spot01Weapon);
if(AvailableVehicles[i].spot02Weapon != null)
spotsSprites[1].GetComponent<Image>().sprite = Resources.Load <Sprite> ("StoreSprites/Weapons/"+AvailableVehicles[i].spot02Weapon);
if(AvailableVehicles[i].spot03Weapon != null)
spotsSprites[2].GetComponent<Image>().sprite = Resources.Load <Sprite> ("StoreSprites/Weapons/"+AvailableVehicles[i].spot03Weapon);
}
}
Is there a way to make that code more compact? Something like this:
public void SetSpotsSprites () {
for(int i = 0; i< AvailableVehicles.Count; i++){
if(activeVehicle.name == AvailableVehicles[i].vehicleName){
if(AvailableVehicles[i].spot0[i]Weapon != null)
spotsSprites[0].GetComponent<Image>().sprite = Resources.Load <Sprite> ("StoreSprites/Weapons/"+AvailableVehicles[i].spot0[i]Weapon);
}
}
Thanks!
Ins$$anonymous$$d of having individual methods for each weapon, add a method that returns the weapon.
Vehicles.spot01Weapon -> Vehicles.GetWeapon(0)
Vehicles.spot02Weapon -> Vehicles.GetWeapon(1)
Vehicles.spot03Weapon -> Vehicles.GetWeapon(2)
That actually may come in handy in a very near future. Thanks!
Answer by AlwaysSunny · Dec 06, 2016 at 11:56 AM
Always a good idea to be thinking about ways to reduce code. There's never a one-size-fits-all answer to questions like this, though. It's about developing good practices for structuring your data. For instance, as you hinted at, you could have an array of whatever these spot01Weapon objects are. This is an extremely common and clever way to handle logical groups of objects, and having logically grouped objects in collections is almost always a smart thing to do. Then you'd be addressing them by index instead of by their variable name. Steps like this are a good habit, but you can also overdo it. Have a look at C# Generic Lists too. You'll get the hang of it!
Yes I'm already familiarized with Generic Lists. I was just trying to know if when sometimes, by mistake, we come to the situation i pointed above and if there was a simple solution. But apparently no. So I guess I'll have to change a couple of things in my game.
Thanks anyway!
Answer by UnityCoach · Dec 06, 2016 at 12:18 PM
You would have to put your spotXXWeapon in a List, then you could do another loop through it.
public void SetSpotsSprites ()
{
for(int i = 0; i< AvailableVehicles.Count; i++)
{
if(activeVehicle.name == AvailableVehicles[i].vehicleName)
{
for (int w = 0 ; w<AvailableVehicles[i].spotWeapons.Count ; w++)
{
// TODO : add your stuff here
{
}
}
}
Not quite what i had in $$anonymous$$d. $$anonymous$$ore loops isn't a valid solution at this moment.
But thanks anyway.
The idea here is the standard "use an array ins$$anonymous$$d" one. You can turn spot01, spot02 and spot03 into an array. Inside the vehicle struct, replace Weapon spot01, spot02, spot03; with Weapon[] Spot;.
That gives all the usual benefits: can easily add more weapons, a loop with one body is less error-prone than multiple identical bodies, can treat it as a single object for functions.
Nested loops are good here because they show what you really have: many vehicles, many weapons for each vehicle. But don't push yourself to learn something new if you're on a deadline, or just want to get something finished. $$anonymous$$nowing this stuff will be worth it, but plan for several days at least, and a "try new ideas, that won't work at first" project. I've even got a chapter on array in struct in array ... in taxesforcatses.
That's actually what I end up doing. I put all the weaponsSlot in a Weapon[] Slot, and iterate trough them. But I thought the answer from AlwaysSunny was more $$anonymous$$d opening, and for future wanderers they may also find it quite useful. As well as the example given by you and jikkou.
Thanks!