- Home /
How to tell an operand "Once" or "Started to"
This may make a few people mad, but I'm trying to convert Project Spark Logic to Unity 5.6 C#...
I am beginner to intermediate coder, and I'm trying to make this function as clean as possible, for my knowledge, and others as well. I will do my best to write my desired effect in C#:
void Update()
{
if (intA > (intB * intC))
{
"Started To" intC++;
"Started To" intD++;
}
}
It should result in, when intA "Started to" be greater than intB * intC, intC and intD increment by 1 only one time. But when intA is less, reset. Then when intA is greater than again, increment those int's again only one time.
NOTE: I am aware of doing this ugly, and unclean with multiple Booleans, the question is, though, Is there a more simple way?
Answer by UnityCoach · Jul 10, 2017 at 06:54 PM
You should use properties (variables and accessors)
private int _A;
public int A
{
get { return _A; }
set
{
if (_A != value) // only triggered when value actually changes
{
_A = value; // updates value
C = _A < B*C ? 0 : C+1; // use ternary operator to assign new value to C, based on condition
D = _A < B*C ? 0 : D+1;
}
}
}
@UnityCoach I will mark this as the answer, hopefully when my knowledge gets up there, I'll look back at this and go: ohhhh so that's what the _A is used for...... I still have no clue, lol.... Do you return _A to use it? What if intA is a main part of code? I'll never know...
If you get the chance check out my new question, you'll actually see where this equation was used. :)
Yes, you return _A to use it, anywhere. _A is marked private so that no other class can access it (OOP encapsulation principle), yet you implement a public 'get' method so that other module can read the value. Usually I would also add 'private' before the 'set' method, so that no other module can change it. It really forces you to respect the SOLID design and OOP good practices. The nice thing about this is that you know when a value is 'read' or 'written to' and can take action.
Something like this for example, will automatically count down every time you access it:
private int _remaining = 10;
public int Remaining
{
get
{
_remaining --;
return _remaining;
}
}
You can check out my youtube channel for some free video tutorials and my website for more.
Answer by Cynikal · Jul 10, 2017 at 06:49 PM
Add a bool outside of the Update function..
bool startedTo = false;
void Update()
{
if ((intA > (intB * intC)) && !startedTo)
{
startedTo = true;
"Started To" intC++;
"Started To" intD++;
} else if (intA > (intB * intC))
{
// Started to is now true... do something else
}
}
Not sure if this is what you're wanting.
Are you saying:
void Update()
{
if(intA > ( intB * intC)) {
startedTo = true;
} else {
startedTo = false;
}
}
private bool startedTo(true) {
intC++;
intD++;
}
I know that's not written correctly, but is it what you're referring to? using a struct outside the update function? Because anything with Update constantly gets called, and with the bool always true while the condition is running, still makes intC and intD go nuts...
Answer by Jinkata · Jul 10, 2017 at 06:51 PM
There are a bunch of different ways to accomplish this. But you really only need one boolean and simply switch to either checking greater than or less than based on the value of the boolean. You could also put this in a C# property so it only checks when intA value is updated. Or you could use delegates with anonymous functions to update the method used to check whether or not you need to take action.
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/using-delegates
If you want any clarification on any of this just let me know.
Your answer
Follow this Question
Related Questions
Using AirBrakes on mobile device with Standard Assets AircraftController 0 Answers
Built project, now scripts are missing. 2 Answers
rigidbody.Use gravity not working from script 0 Answers
How to make the calculated angle respective of the players rotation? 1 Answer
Coroutines randomly stop working 0 Answers