Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Taxen0 · Jan 17, 2017 at 09:44 AM · optimizationmemorymemory managementmemory usageoptimize

Basic question about optimization (store variable vs accessing)

Hi,

I have a minor question about optimization, and I know that in my case it probably won't matter but I'm interested in learning more about these kind of things for the future.

I have a Vector3 array that i store the waypoints or "path" that the enemies in my tower defense game should take, the array is created at the start of the game and stored in a gameHandler script. Every time an enemy is spawned they get assigned a path to follow.

Should I copy the array created in my gameHandler script to each enemy when created, or should the enemy script simply call the gameHandler whenever they need to fetch the next waypoint. I guess the choice is between efficiency (many calls to gameHandler for each enemy) vs memory usage (storing the array).

In my case I guess I will just copy the array, but lets imagine that they grow really large, is there a point where I should approach this differently. Also it is a mobile game if that change anything.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Inaetaru · Jan 17, 2017 at 10:03 AM

I'm assuming that the path is single path shared between all enemies (for given level) and the path doesn't change during play.

My answer is written under these assumptions;

Simple answer is no. If you copy the array (I mean real copy - allocate new array, copy values), you will have unnecessary redundancy of data in your code. There is lots of articles describing why we're trying to avoid data redundancy - in short, it will be hard in future to keep all redundant data consistent and updated.

If you do not copy the array, but just take reference (sharing same data), it's still not good, because a) you're exposing inner working of class working with the path and b) some code might accidentally change it breaking it for everyone. Read about encapsulation.

So the best solution is not to provide path at all (unless really needed), but provide functions which do work on the path. For example GetNextDestination(currentPosition).

Now, about optimizations:

  • General rule is: don't do it.

  • Second rule is: don't do it - yet.

Before you do an optimization, you should always measure what the slowest part of the game is and start there. In this particular case, the overhead done by calling function can be rounded to zero when you compare it with overhead caused by rendering the unit. When you add - let's say - thousands enemies and game slows down, avoiding one method call will save you like less then 1% of time needed for one frame. If you optimize meshes of enemies (less polygons, single batchable material, etc.), you'll save much more. I already saw performance gains reaching 90% (but that's extreme). In other words, premature optimization is waste of time (unless you're really experienced). I suggest reading articles/books about that. (On the other hand "avoiding premature optimization" is NOT "wasting resources all the time". Of course you should try to write fast code, but readability, good design like encapsulation, etc. are top priority. Optimizations comes after "good designed code" is too slow, which is actually bit rare.)

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Taxen0 · Jan 17, 2017 at 11:35 AM 0
Share

Thank you for a great reply! You are correct that the path won't change. Im not sure how C# works when it comes to variable referencing, but if i have the following code:

 public class Enemy: $$anonymous$$onoBehaviour {
     private Vector3[] path;
 
     public void UpdatePath(Vector3[] newPath){
         path = newPath;
     }
 } 
 

I guess path would be its own copy of newPath (the one saved in gameHandler), right? So in that case you would recommend that I ins$$anonymous$$d get the enemies to call a method in the gameHandler script whenever they reach their current waypoint to fetch the next one?

avatar image Inaetaru Taxen0 · Jan 17, 2017 at 04:25 PM 0
Share

Variables in C# are bit tricky. When you're using classes and arrays, assignment does NOT copy the data. Both variables will point to same object! However if you assign structures or primitive values (int, float, etc.), value will be copied.

So no, your variable path will not be a copy.

For example if you do this:

 Enemy enemy = GetSomehowEnemy();
 Vector3[] newPath = CreateNewPath();
 enemy.UpdatePath(newPath);

now if enemy changes it's own path, newPath will be affected too!

Anyway yes, I would recommend to call method in gameHandler ins$$anonymous$$d of keeping own path.

Note: see http://stackoverflow.com/a/13275 for more info about class/array and structure differences in C#.

avatar image Taxen0 Inaetaru · Jan 17, 2017 at 04:33 PM 0
Share

Ah i see, Thanks again! $$anonymous$$akes sense to still call the gamehandler for the sake of encapsylation.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

64 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to optimize memory usage in webgl applications 0 Answers

Unity Sprite Memory Usage Is Huge 1 Answer

Unity memory thirst 0 Answers

Reduce GPU Memory usage from textures 1 Answer

Is this memory usage too much for a mobile game? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges