creating new SKSpriteNode vs using a copy of a SKSpriteNode - sprite-kit

I'm looking for ways to improve the performance of my game. In my game, the boss will cast 16 fireballs at once (at different angles). My current code is to create a SKSpriteNode for each fireball, and repeat the process 16 times. I can improve the performance by dispatching the creation process to a different thread and then add the sprite back to main thread once it's created. This way I don't see a noticeable lag in the gameplay.
However, I'm thinking that will it improve the performance even more if I cache the first fireball sprite after it's being created and then use .copy() 15 times to create the rest of the fireballs?
ultimately, I'm trying understand is using .copy() on a SKSpriteNode as expensive as creating new one?
Thanks for any insight.

Copy is a little less expensive, but not much. It doesn't need to access any IO to grab things like the texture since you already have it in memory.
Now the way you are designing your game, you should not be constantly recreating fireballs. You should create a pool of them, and constantly recycle them. This way you only have to create them once during the loading portion of your game, and you are not wasting precious cycles on redundant work.

Related

Will Disabled GameObjects take up performance in Unity?

Hi I wanted to know if I have a GameObject in Unity, but have no components attached to it except for the Transform and it will be SetActive(false). Does this take up performance. I know that this question have been already asked over here :
Will Disabled and invisible Gameobject take up performance?
But I am still confused with the answer and its is 3 years old and all of the answers I researched are kinda old, so I do not know if enabling and disabling objects will still take up performance as it might be updated, I don't know. I need this info cause I am making a Procedural Generation in Unity.
Thanks :)
As far as the GameObjects present in the scene are concerned, they will take up a bit of CPU and Memory because though inactive, they are still present in the scene, and you can still access them in the scene.
But this is negligible in most cases. Because all the components on the gameObject itself will be Inactive (Transform, Renderers, Scripts).
However, When you have scripts attached to the GameObjects, if they are Rendering something, etc, Keeping them Inactive will save your CPU and GPU when it comes to those components, because there is no computing kicking in until the GameObject is actually active in the scene.
But having inactive GameObjects in the scene in general is not an issue at all. It takes up negligible performance and memory in an average use case.

Unity best practices for unloading unused areas of a map

I'm wondering about the best way of reducing my CPU and memory overhead for areas of the game world that do not need to be updated every frame.
I have just started to consider this issue as I'm currently implementing a shadow detection system using Raycasts.
My problem is this:
I can have about 100 lights in my level that on every frame send a Raycast to nearby characters to determine if these characters are in shadows.
My game is a low poly PC game, and I understand the overhead from Raycasting isn't that drastic. So its not a major concern. But I'm still not sure of the best approach to optimising this.
I have been thinking of a few soltions, but am unsure if there is "standard" per say.
1. exit update loop if player is too far away
void Update() {
if (Vector3.Distance(playerPos, transform.position) > someRadius) {
return
}
}
This is the most glaringly obvious solution, with even more obvious concerns.
This update loop will still be hogging CPU cycles , performing 2 calculations on every frame, for every light point.
2. Disable Light gameObjects when the player is too far away
This method is more efficient in terms of CPU overhead, as those will be negated. However I'm still hogging uneccesary memory.
In order to make this solution more scalable I would have to design some kind of "enabler" that keeps track of game objects that should be enabled/disabled based on the player position.
But at this point I know I'm re-inventing the wheel, and feel very sure that there is an industry standard for this.
Is there an alternative to enabling/disabling?
I see a lot of game developers talking about physically unloading areas of their game from memory and writing those areas to disk, when the player is not nearby.
I wonder is this achieved by simply destroying and re-instantiating the objects.
Question
Is Unity opionated about this?
The page here lists an example, similar to my first solution. But they are talking about 100s of thousands of updates per frame here.
Maybe I don't need to worry as much as I think
Thanks!
If you want not render objects, that are not in your camera area, Occlusion culling is what you exactly need. Watch this link:
https://docs.unity3d.com/Manual/OcclusionCulling.html
If You want change how your object looks based on range between camera and your object, You have to use LOD(Level Of Detail). Here is documentation:
https://docs.unity3d.com/Manual/class-LODGroup.html

High CPU use in SpriteKit game

I'm making my first game with Swift and SpriteKit and have noticed that my game quickly approaches 100% CPU use. I have been posting a lot about this recently, but here I'm now I'm now attempting to pull it apart to determine where my problems are. I originally thought my problem was with instantiating new enemies, so I removed everything but the small user-controlled spaceship and its shooting functionality. However, I'm still hitting 100% after a couple minutes of "play". I'm using the time-profiler instrument to try and figure out whats going on but I'm having trouble. I've broken up the time line:
Here is the first subgraph when the user clicks a homeScene button to enter the GameScene.
I'm not entirely sure what the red and yellow "Heaviest backtraces" with 3256x and 861x respectively mean, but from tutorials I've seen I would imagine that they are abnormally high. Here is the Call Tree as well:
I'm also not entirely sure why the controller textures would be using so much as they are simply setting two textures in the file right before the GameScene class:
let controllerBaseTexture = SKTextureAtlas(named:"Sprites").textureNamed("controllerBase")
let controllerHandleTexture = SKTextureAtlas(named:"Sprites").textureNamed("controllerHandle")
class GameScene: SKScene, SKPhysicsContactDelegate {
Then here is the Call Tree when the game starts and when the CPU reaches 100%:
I'm also confused about what "UnsaveMutableAddressors" are, as I see them very commonly when trying to debug this code.
I know this is a lot, but I'm not sure where to begin. Any suggestions would be awesome. I would also like to know if there are good resources on how to interpret this better because I've gone through a handful and I'm still very stuck.
Thanks for any help!
The profiler shows it's something in your update method for your gameScene. Keep in mind this gets ran around 60 times per second so you don't want to do anything extremely heavy there.

Game Slow down issue

I am developing a game in which i have to move the ball and power up image simultaneously.
For move a power up image view, I create a separate thread and call move method of power by using [self performSelectorOnMainThread:..], this is required to show the updated position on view. But my Game slowdown after creating this thread.
please guide me in improving the performance of game.
You probably don't want to be creating your own thread here; that's probably slower than doing this work on the main thread unless your calculations are incredibly complicated.
Instead of creating your own threads, investigate CoreAnimation. It's designed to handled moving things around smoothly, manages its own threads transparently, and provides a "fire-and-forget" interface. For most 2D games, the first approach you should follow is CALayers for each element moved around using CoreAnimation.
We didn't design or write your game so how can we be expected to know how to magically fix it for you? Do some research of your own, use Apple's excellent "Instruments" and "Shark" tools for tracking down the problem(s).
I strongly suggest you look at cocos2d instead of manually managing sprites and graphics. Cocos2d will abstract all of that stuff away from you.

jerky effect in the movement of objects

im trying to create a game in which few objects(UIImageViews) traverse the screen...
these objects are generated at an interval of 1 to 0.45 seconds...when they r generating in the range of 1-0.60 seconds they traverse smoothly..but when they generate at a rate less than 0.6 seconds their movement is not smooth they start jerking..also i have a UIImageView which changes the position depending on the the touchesmoved event...
please do help
hey guys I was able to solve the problem by creating 3 different threads ie one for generating the objects, one for traversing the objects and another for moving the object on the touches move event. I was previously using timers for it...
but using the threads solved the problem
Anyways thanks for all your help :)
Obviously your game uses too much cpu-power. Try optimizing it using a profiler (Shark for example) and make your object-creation method less time consuming. (Create a bunch of objects at one time and then just reuse them instead of making new ones?)
What's profiling?
I you don't know profiling at all, read this introduction written by Mike Ash. (Excellent blog btw.)
Here is Apple's Documentation of Shark.