I got following situation
private volatile bool _inProgress = false;
public void DoSomethingStart()
{
if(_inProgress == false)
{
foo.BeginInvoke(null, null); // DoSomething
_inProgress = true;
}
}
[CatchAllExceptionsFromHere]
private void DoSomething()
{ }
The aspects works so far. All exceptions are handeled from CatchAllExceptionsFromHere. But i want to set "_inProgress = false" in the finally clause of the aspect - so if DoSomething has finished "_inProgress" should be set to false. Since Attributes cant take any object - is there a workaround?
Thanks michael
Aspects can take values on the declaration to set property values or just like a constructor. What you want is an OnMethodBoundaryAspect and in the OnExit method (which fires even if an error has occured, so it's just like a finally) you want to set the value of _inProgress.
However, the aspect needs to be an instance aspect and you'll need to either import the _inProgress member or just introduce it so that the aspect has access to it. It depends on if your class needs access to it. If it does the have the aspect import the member. Read up on how to do these things with these links
Aspect lifetime & scope part 1
Aspect lifetime & scope part 2
Introducing members part 1
Introducing members part 2
Related
I have a class where I want to call a static method to initialize some things. I tried calling it from the global scope, like in this simplified example:
class MyClass{
static bool initialized = false;
static void init(){
initialized = true;
}
}
MyClass.init();
void main() async {
// Do something useful
}
This results in the following errors:
Functions must have an explicit list of parameters
The name 'MyClass' is already defined
What I find a bit odd is that if I make the init() method return something and assign that result to a variable, it works:
class MyClass{
static bool initialized = false;
static bool init(){
initialized = true;
return true;
}
}
bool _dummy = MyClass.init();
void main() async {
// Do something useful
}
Why is this? And is there a better workaround to call a static void method from the global scope?
The reason is that static variables are lazy evaluated in Dart, so they will first get a value the first time they are accessed.
That means that MyClass.init() in:
bool _dummy = MyClass.init();
Is only being executed when something are trying to access the variable _dummy. It also means that the following in global scope:
MyClass.init();
Does not make any sense in Dart since this code are never going to be executed since there are no reference to the code.
The reason for this design can be found described in the Dart Language Specification:
Static variable declarations with an initializing expression are initialized lazily.
The lazy semantics are given because we do not want a language where one tends to define expensive initialization computations, causing long application startup times. This is especially crucial for Dart, which must support the coding of client applications.
https://dart.dev/guides/language/specifications/DartLangSpec-v2.10.pdf
new to unity so please ignore if I sound stupid.
I have to scripts references in a script. Example, script A and B are referenced in script C.
I need to store script A and B variable names in a another variable so that I can use that variable in conditioning.
private FemalePlayerAnimations femalePlayerAnimations;
private MalePlayerAnimations malePlayerAnimations;
private Variable variable; // Got Problem Here
void Awake()
{
femalePlayerAnimations = GetComponent<FemalePlayerAnimations>();
malePlayerAnimations = GetComponent<MalePlayerAnimations>();
}
void Start()
{
if(1 + 1 = 2) // Some Condition
{
variable = femalePlayerAnimations;
}
else if(1 + 2 = 3) // Some Another Condition
{
variable = malePlayerAnimation;
}
}
Thanks in advance.
If I understand your question correctly, you'll need to use inheritence and have your male/female animations inherit from the same base class.
i.e.
public abstract class BasePlayerAnimator : MonoBehavior {}
public class MalePlayerAnimator : BasePlayerAnimator {}
public class FemalePlayerAnimator : BasePlayerAnimator {}
Real question though is why do you need two different classes for male/female animations? wouldn't a single class with 2 different instances cover your needs?
I have a feeling you don't simply want the name of the variable as a string.
The logic you are trying to implement here won't work. You can't have a variable that holds the FemalePlayerAnimations class hold a MalePlayerAnimations class.
You should reconsider the design of your program as you can have two different instances (prefabs) of the same theoratical PlayerAnimations class. This is how Animation Controllers work in Unity.
Alternatively you could use a boolean field to store states, for example: bool useFemaleAnimations that is changed in the conditions and implement the correct "script" where applicable.
I'm sorry if this sounds like an extremely foolish question but it's really been bugging me.
What is the "this." that I see? Whenever I see the documentation in flutter I see it used in things like the following in the documentation:
this.initialRoute,
this.onGenerateRoute,
this.onGenerateInitialRoutes,
this.onUnknownRoute,
this.navigatorObservers
I'll be more than happy to also read up any links or documentation regarding it.
The 'this' keyword refers to the current instance.
You only need to use this when there is a name conflict. Otherwise, Dart style omits the this.
class Car {
String engine;
void newEngine({String engine}) {
if (engine!= null) {
this.engine= engine;
}
}
}
So you can be consistent with the name of your parameters, either in the constructor or in some function in the class.
class Car {
String engine;
void updateEngine({String someWeirdName}) {
engine = someWeirdName;
}
}
If you don't have a name conflict, you don't need to use this.
In other languages like Python and Swift, the word 'self' will do the same thing as 'this'.
Basically, this keyword is used to denotes the current instance. Check out the below example.
void main() {
Person mike = Person(21);
print(mike.height);
}
class Person {
double height;
Person(double height) {
height = height;
}
}
When we run this dart code, it outputs null as the height. Because we have used height = height inside the Person constructor, but the code doesn't know which height is the class property.
Therefore, we can use this keyword to denotes the current instance and it will help the code to understand which height belongs to the class. So, we can use it as below and we will get the correct output.
void main() {
Person mike = Person(21);
print(mike.height);
}
class Person {
double height;
Person(double height) {
this.height = height;
}
}
Use of this keyword
The this keyword is used to point the current class object.
It can be used to refer to the present class variables.
We can instantiate or invoke the current class constructor using this keyword.
We can pass this keyword as a parameter in the constructor call.
We can pass this keyword as a parameter in the method call.
It removes the ambiguity or naming conflict in the constructor or method of our instance/object.
It can be used to return the current class instance.
I have a motion-capture system that I use to animate a character (non-mecanim) so obviously I have to map the motion data onto the character's bones and segments.
Here is how I do it, but since the joints names are only used within the mapBones() method, I get quite a few amber warnings at the Unity console that warning CS0219: The variable JtHips is assigned but its value is never used.
I get one for each bone! It does not look professional, so I am wondering if there is any way I could get rid of them? All the posts I have checked out, of course recommend using them, but in this case, they are being used internally by the mapping function...
private Dictionary<MoCapSegment, ChrBones> jointsDict;
public enum MoCapSegment
{
Pelvis = 0,
// many more ...
Neck = 48,
Head = 49
}
public enum ChrBones
{
JtHips = 0,
// many more ...
JtNeckA = 48,
JtSkullA = 49
}
protected void mapBones()
{
jointsDict = new Dictionary<MoCapSegment, ChrBones>();
GameObject JtHips = transform.Find("CharacterRoot/JtJtHips").gameObject;
jointsDict.Add(MoCapSegment.Pelvis, ChrBones.JtHips);
// all the rest
}
You CAN do the following, but be careful
#pragma warning disable 0414
Specifically,
#pragma warning disable 0168// variable declared but not used.
#pragma warning disable 0219// variable assigned but not used.
#pragma warning disable 0414// private field assigned but not used.
I agree with you that this problem is infuriating.
Another thing you can do is this...
public void check<T>(T x){}
and then you can
private Whatever JtHips = whatever; // throws warning since never used
check(JtHips); // avoid warning like this
As always it's a great chance to use an extension
public static class YourExtensions
{
public static void Nothing<T>(this T x) {}
and then you can
JtHips.Nothing();
JtNeck.Nothing();
JtSkull.Nothing();
etc...
All that being said, it's a very bad idea to suppress warnings, of course. Take care.
I have a object property in my Class which is private and marked as read-only.
private var readOnlyObj:Object;
I can only access it with a get method:
public function get readOnly(){ return readOnlyObj }
I can access it by:
var objClass = new MyClass();
trace(objClass.readOnly)
And if i'll try to modify it:
objClass.readOnly = new Object();
I'll get an error:
Error# Property is read only.
Now my question is:
How do I set the properties of my readOnlyObj as read-only?
If I have set the object in the constructor:
readOnlyObj["property1"] = 0;
And modify that property by:
objClass.readOnly["property1"] = 2;
It is valid. I want set the property1 to a read-only property. Is this possible? Thank You!
You can do this by returning a duplicate of the original object and not the object itself.
The transform properties of DisplayObjects work like this: you can get the object property from a get function and can modify the object, but such modification has no effect until you pass the modified object back to the set function.
In your case, there's no way to give the object back (no setter) and by returning a copy (commonly called 'clone') from the getter, there is no way to modify the object property from outside, because the returned reference reference the newly created independent clone, essentially making the internal object constant.
What you are asking is not possible and only yield the answer "no" if on the other hand you asked about how to achieve that functionality then there would be a few answer possible.
First of all given your code and the problem at hand it is clear that you misunderstand the class scope. You set:
private var readOnlyObj:Object;
As read only while it's really not the object you want to protect, it's its properties. So readOnlyObj should really not even be visible and accessible.
Now that readOnlyObj is private and not accessible, you can put together a simple method to retrieve properties:
public function getProperty(name:String):*
{
if(readOnlyObj[name] != undefined)
{
return readOnlyObj[name];
}
return null;
}
It might also be useful to know how to put together a public setter that cannot be used externally.
Create an internal Boolean variable (only with true package), then internally set that variable to true before setting the property then set it back to false. Since externally that boolean cannot be set you end up with a public setter that cannot be used externally.
internal var allowSetter:Boolean;
public function set whatever(value:*):void
{
if(allowSetter)
{
//set property ect...
allowSetter = false;
}
}
You can't really do this, at least in the way you describe. You can of course make your readOnly object a custom class instance that only has read-only properties, but you can't freeze a dynamic Object instance.