- Home /
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?
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.
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
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
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.
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
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
Follow this Question
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