How to avoid using Update for scriptable object data?
Hello all! I am making a word game which has letter tiles. The idea is that each time you take a turn, you tiles "age" which essentially means they lose one point of endurance. When their age reaches 0, they are replaced. These letter tiles can have skins for which I'm using a scriptable object. Each age has a different sprite (0-4). I have been trying to find a way to make the tile skin change only on click, however this doesn't seem to work and I am having to use Update
instead. This means I am running at least 10 Update
events simultaneously which I am sure is affecting performance. Am just wondering if there is an alternative to this that anyone can suggest?
public TileClass spawnedTile;
//used for Scriptable Object access
tileDisplayAccess = GetComponent<TileDisplay>();
private void Update()
{
switch (spawnedTile.age)
{
case 4:
//if the age is 4 then use the tiles from the scriptable object within the array
tileBGImage.sprite = tileDisplayAccess.tileAgeSprites[4];
break;
case 3:
tileBGImage.sprite = tileDisplayAccess.tileAgeSprites[3];
break;
case 2:
tileBGImage.sprite = tileDisplayAccess.tileAgeSprites[2];
break;
case 1:
tileBGImage.sprite = tileDisplayAccess.tileAgeSprites[1];
break;
case 0:
tileBGImage.sprite = tileDisplayAccess.tileAgeSprites[0];
break;
}
}
Answer by Wilomomo · Dec 12, 2019 at 03:13 PM
You can use get/set to make a change only when you set your value =)
private TileClass _spawnedTile;
public TileClass SpawnedTile
{
get { return _spawnedTile}
set
{
_spawnedTile = value;
switch (_spawnedTile.age)
{
case 4:
//if the age is 4 then use the tiles from the scriptable object within the array
tileBGImage.sprite = tileDisplayAccess.tileAgeSprites[4];
break;
case 3:
tileBGImage.sprite = tileDisplayAccess.tileAgeSprites[3];
break;
case 2:
tileBGImage.sprite = tileDisplayAccess.tileAgeSprites[2];
break;
case 1:
tileBGImage.sprite = tileDisplayAccess.tileAgeSprites[1];
break;
case 0:
tileBGImage.sprite = tileDisplayAccess.tileAgeSprites[0];
break;
}
}
}
With this code your change appear only when you change SpawnedTile value =)
I hope this will answer your question ^^
Thanks for your answer, but that doesn't seem to have worked. Each of the tile sprites don't change at all when they age. They just remain as per the default tile sprite. Any ideas why?
Well, how does your aging code looks like? If you want things event based of course you have to do your changes when that event happens. If you actually change your age variable somewhere you might want to replace the age variable by a property with backing field and do your logic inside the age setter. So change it when it needs to change. This should be pretty obvious, however we have no idea how your code looks like so we can not offer any alternatives. The first thing I would change is get rid of that switch statement and replace it with this line:
tileBGImage.sprite = tileDisplayAccess.tileAgeSprites[_spawnedTile.age];
If that age value can get outside the range of 0-4 you might want to add a single if statement around that line to avoid any errors.
However if you have only 10 tiles, running 10 Updates isn't really an issue at all. What could be an issue is the constant pointless re-assigning of the same sprite. However it might have no impact at all but that depends on the exact implementation. If you worry about the performance, measure it. Don't assume things you can not possible know anything about from the outside.