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 sdgd · Feb 13, 2013 at 07:03 PM · c#arrayloadpointerstruct

array is pointer and float variable is not - why not? C#

I'm wandering why it guesses array is pointer and it automatically sets all KeyCodes in another script with load

while I must make public floats for mouse X Y Z like I'm trying with asdf example and I sucseed I can load all KeyCodes only with this in other script

 saveLoad.LoadControllsF(keyControllKeyCode, mouseSensitivityXf, mouseSensitivityYf, mouseSensitivityZf, MouseSmoothingb);
 
 mouseSensitivityYf = saveLoad.asdf;

and float needs to be adressed manually

main problem is LoadControllsF

     public void SaveControllsF(KeyCode[] key, float MouseX, float MouseY, float MouseZ, bool MouseSmoothb){
         string[] FilePaths = Directory.GetFiles(Application.dataPath + "/Save/User/Controlls/", "*.sav", SearchOption.AllDirectories);
         // If file exists we create backeup
         if (FilePaths.Length != 0){
             File.Copy(FilePaths[0],(FilePaths[0] + ".back"), true);
         }
         asdf = 50.5f;
         using(var writer = new BinaryWriter(File.OpenWrite(Application.dataPath + "/Save/User/Controlls/Controlls.sav"))){
             for (int i=0; i < 3; i++) {
                 writer.Write((int)key[i]);
             }
             
             writer.Write((decimal)MouseX);
             writer.Write((decimal)asdf);
             writer.Write((decimal)MouseZ);
             //writer.Write(MouseSmoothb);
             writer.Close();
         }
     }



     public void LoadControllsF(KeyCode[] key, float MouseX, float MouseY, float MouseZ, bool MouseSmoothb){
         string[] FilePaths = Directory.GetFiles(Application.dataPath + "/Save/User/Controlls/", "*.sav", SearchOption.AllDirectories);
         if (FilePaths.Length == 0){
             LoadDefaultControllsF(key, MouseX, MouseY, MouseZ, MouseSmoothb);
         }
         else {
             using(var reader = new BinaryReader(File.OpenRead(FilePaths[0]))){
                 for (int i=0; i < 3; i++) {
                     key[i] = (KeyCode)reader.ReadInt32();
                 }
                 MouseX = (float)reader.ReadDecimal();
                 asdf = (float)reader.ReadDecimal();
                 MouseZ = (float)reader.ReadDecimal();
                 reader.Close();
             }
         }
     }



Full Struct code

 using UnityEngine;
 using System.Collections;
 using System.IO;
 
 public struct SaveLoadS {
     public float asdf;
     
     /****** START DEFAULTS ******/
     public void SaveDefaultControllsF(KeyCode[] key, float MouseX, float MouseY, float MouseZ, bool MouseSmoothb){
         using(var writer = new BinaryWriter(File.OpenWrite(Application.dataPath + "/Save/Default/Controlls.sav"))){
             for (int i=0; i < key.Length; i++) {
                 writer.Write((int)key[i]);
             }
             writer.Close();
         }
     } // Development puropses ONLY
     public void LoadDefaultControllsF(KeyCode[] key, float MouseX, float MouseY, float MouseZ, bool MouseSmoothb){
         using(var reader = new BinaryReader(File.OpenRead(Application.dataPath + "/Save/Default/Controlls.sav"))){
             for (int i=0; i < key.Length; i++) {
                 key[i] = (KeyCode)reader.ReadInt32();
             }
             reader.Close();
         }
     }
     /****** END DEFAULTS *****/
     public void SaveControllsF(KeyCode[] key, float MouseX, float MouseY, float MouseZ, bool MouseSmoothb){
         string[] FilePaths = Directory.GetFiles(Application.dataPath + "/Save/User/Controlls/", "*.sav", SearchOption.AllDirectories);
         // If file exists we create backeup
         if (FilePaths.Length != 0){
             File.Copy(FilePaths[0],(FilePaths[0] + ".back"), true);
         }
         asdf = 50.5f;
         using(var writer = new BinaryWriter(File.OpenWrite(Application.dataPath + "/Save/User/Controlls/Controlls.sav"))){
             for (int i=0; i < 3; i++) {
                 writer.Write((int)key[i]);
             }
             
             writer.Write((decimal)MouseX);
             writer.Write((decimal)asdf);
             writer.Write((decimal)MouseZ);
             //writer.Write(MouseSmoothb);
             writer.Close();
         }
     }
     public void LoadControllsF(KeyCode[] key, float MouseX, float MouseY, float MouseZ, bool MouseSmoothb){
         string[] FilePaths = Directory.GetFiles(Application.dataPath + "/Save/User/Controlls/", "*.sav", SearchOption.AllDirectories);
         if (FilePaths.Length == 0){
             LoadDefaultControllsF(key, MouseX, MouseY, MouseZ, MouseSmoothb);
         }
         else {
             using(var reader = new BinaryReader(File.OpenRead(FilePaths[0]))){
                 for (int i=0; i < 3; i++) {
                     key[i] = (KeyCode)reader.ReadInt32();
                 }
                 MouseX = (float)reader.ReadDecimal();
                 asdf = (float)reader.ReadDecimal();
                 MouseZ = (float)reader.ReadDecimal();
                 reader.Close();
             }
         }
     }
 }
 

and maybe as partionally solution how do I make a pointer?

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
2
Best Answer

Answer by nullstar · Feb 14, 2013 at 12:47 PM

C# determines whether parameters are passed by value or passed by reference based on the type of that parameter. Roughly speaking all structs and simple types are passed by value and all classes and arrays are passed by reference. This is just the way things work in C#. Follow these links for more info on value types and reference types.

Although its possible to use pointers in C#, doing so requires that you specify the unsafe keyword which Unity doesn't support. It is however possible to specify the 'ref' keyword when declaring a function parameter in order to pass a value type parameter as a reference. This is effectively like having a pointer function parameter. Follow this link for more information on passing by value and reference.

Comment
Add comment · Show 5 · 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 sdgd · Feb 14, 2013 at 01:34 PM 0
Share

thank you very much on $$anonymous$$icrosoft pages I'm almost always lost

can you show me basic code how do I make a pointer and how do I destroy it afaik pointers needs to be destroyed IF we want there's no memory leaks

avatar image sdgd · Feb 14, 2013 at 01:40 PM 0
Share

I mean how do do it with ref keyword

and yes I saw both comments and in 1/2 of your comment I started reading this answer

witch seem more important since users put more effort in putting answer thanks for deleting your comments I can't get your knoledge

avatar image nullstar · Feb 14, 2013 at 01:49 PM 1
Share

This is how you would create a pointer:

 class PointerExample
 {
     unsafe void PointerFunction()
     {
         int myInt = 1234;
         int* p$$anonymous$$yInt = &myInt;    // this is a pointer to my int
     }
 }

Although note that as I stated, Unity doesn't support the unsafe keyword so you cant use them with Unity code. As for the memory part of your question, you dont create and delete memory manualy in C# the same you would in say C++. C# is a managed memory language, that means it automaticaly takes care of memory allocation / deallocation for you by internaly keeping track of references to allocated memory and periodically releasing sections of memory which have a zero reference count.

avatar image sdgd · Feb 14, 2013 at 01:57 PM 0
Share

thank you

well in C++ I've saw that we have to know when to destroy pointers for not heaving undefined behaviours and memory leakes

avatar image nullstar · Feb 14, 2013 at 02:00 PM 1
Share

An example of using ref:

 class RefExample
 {
     public void RefTest()
     {
         int i = 1;        // int equals 1
         IncrementInt(ref i);
         Debug.Log(i.ToString());   // int equals 2 because it was passed by reference to IncrementInt()
     }
     
     public void IncrementInt(ref int i)
     {
         ++i;        // int equals 2
     }
 }

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

How do I delete or empty an array of custom structs during edit mode? 1 Answer

Selecting Structs from dropdown menu in inspector. 1 Answer

Multidimensional array of structs 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers


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