- Home /
More elegant use of struct and class
I love to program in more simple ways, using either less code or writing things more literally, but it all stops when I'm making things that can comunicate to each other...
I have a struct inside a class, and its vars should be accessible to other scripts inside the level. Should I allow the variable inside the struct to be directly accessible by external scripts or should I create methods in the class that handle the value required by the other scripts? Or the struct handles all the methods by itself and use methods to allow reading and/or writing the variables without ever being a need for a specific method?
If I go for the last one, would "not use struct", but just unwrap all the vars in the class be a more adequated solution?
Answer by Jeff-Kesselman · Jun 05, 2014 at 01:53 AM
There is no one newer to this, sorry.
This is like asking "should I always hold the screwdriver in my left or my right hand?"
Struct is a tool, one of many that you can use to structure your code.
The one meta-rule is that it is always a good practice to separate interface (API) from implementation (code details.)
There are many ways to do that and which re best really depends on the situation.
So there is no kind of thing as a safer implementation, or more efficient? Less error prone, etc?
As separating API from the implementation, thans, I'm actually doing it for the first time. I'm somewhat usued to microcontrollers, where the stack were limited to just 9 calls and very limited memory for basically anything, so program$$anonymous$$g for PC and smartphones is very new to me.
Your answer
Follow this Question
Related Questions
Transform var between scripts 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Variable declaration. Public vs indirect public? 3 Answers
Random selection 1 Answer