Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Polymo · Nov 01, 2012 at 08:55 PM · classstaticorderinitialization

class constructor/order/initialization confusion

Hi, sorry if this sounds noobish (i'm not a professional programmer), but i have the following problem: I'm about to implement a recipe system. I have two classes:

Item {
int id,
string name
and so on }

then

recipe {
int id,
item[] ingredients
}

then i have a static array:
item[] itemPresets = {
new item(X, xx,...),
new item(Y, yy,...)
}

now i need something like these itempresets for the recipes too. But the constructor for the recipe class is:
Recipe(int _id, int item1, int item2){
id = _id;
ingredients[0] = itemPresets.GetItemByID(item1);
and so on..
}

i need these as ints because i have to be able to send them via RPC too. But:
how can i be sure, that "itemPresets" is initialized first so "GetItemByID" returns the right item?
Or am i doing something really wrong here? Are there some "best practices" for this?

Thank you for your time. (and sorry, if i misspelled something, english is not my native tongue)

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 Michael CMS · Nov 01, 2012 at 09:45 PM

You can be sure that statics arrays are initialized before the constructor code.

Blockquote 10.5.5.1 Static field initialization The static field variable initializers of a class correspond to a sequence of assignments that are executed in the textual order in which they appear in the class declaration. Blockquote

Hope this helps

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 Polymo · Nov 01, 2012 at 10:37 PM 0
Share

sorry, i'm still confused. So the compiler can figure out which array needs to be filled first by analyzing the code, even though i initialize it with help of a function which in return needs access to an already initialized array? (i googled a bit for static field initialization after your comment, but i'm not really sure if this is what i mean). I have to say i dont initialize it in Start() or Awake but varray = {Xobject,Xobject}. Should i do it in Start to have more control?

avatar image Michael CMS · Nov 02, 2012 at 09:12 AM 0
Share

The compiler doesn't figure out which array needs to be filled first. It just initializes them in the order you declare them in your code.

If you have for example : A[] = {xObj(),xObj()}; B[] = {yObj(),yObj()};

Then A will be initialized by the time B starts initializing.

xObj's constructor won't have either A or B initialized, while yObj's constructor will have only A initialized

Switching the order of the declaration of A and B will reverse their initialization order also.

Ofc if you feel more comfortable you can move the code in the Start /Awake functions.

avatar image Polymo · Nov 04, 2012 at 01:30 PM 0
Share

Thank you. Good to know. And, now I did move it to Start(); that felt right and it works.

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

11 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

Related Questions

What the best way to initialize constant static data in a MonoBehavior? 0 Answers

Static GameObject List gives error when calling Add or Count from another script 1 Answer

Using static constants from an outside script 2 Answers

Error Assigning GameObject To Var Inside A Class 3 Answers

Auto Commenting lines 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