Foreach loop logs ones constant input - c#

I have two logs in a foreach loop. One outputs the current element of the loop, and the other logs "1". The elements all get logged, but the "1" gets logged ones. I use Debug.Log for logging. I execute the script in a unity project.
The loop looks therefore something like this
foreach(CostumeClass A in B)
{
Debug.Log("1");
Debug.Log(A);
}
The Output is
>1
>FirstElement
>SecondElement
>...
I expected
>1
>FirstElement
>1
>SecondElement
>...

Apparently, Unity console packs identical logs together into one log and then writes on the right how often they appeared.
This just cost me one hour of my life, thanks unity.

This is intentional behavior in the Unity console that can manually be toggled on or off by using the Collapse button. When collapsing is on, the console will only display the first instance of recurring error messages, and increment a number for each recurring.
Also, unity only does this within loops. If you just write Debug.Log("!") four times, it'll appear four times... still don't really understand this.
This is because the messages are identical since they're called from the same location while in a loop, but different when you call them four times in different locations of your code.

Related

Is it possible to split page requests 50 / 50?

I have a landing page that plays a video. The client would like one video for half the users, and a different video for the second half.
I was thinking about generating doing a random number, if it's even then video 1 else video 2. I figure overtime this should end up being roughly 50 / 50. Another approach was setting a Application variable within Application_Start and using that. I was perhaps thinking I could configure something within IIS that I could evaluate when the page is requested.
The site is simple and will be served from a single source.
Is there a way to do this, before I start wasting time throwing things at the wall to see what sticks? I am not sure what to even search for.
You could generate a random number where the options are limited to 2 possible outcomes then use that:
Random random = new Random();
var randomNumber = random.Next(0, 2);
The above will give you either a 0 or a 1 as output which can be used to determine one of your two paths.
I ran this 10,000 times in a little program and it came out pretty even (4948/5052).
As long as your boss isn't worried about EXACTLY even numbers I think this should be OK.
Edit: In my test program I created a new randon in each iteration of the loop, because I felt this better simulated the use-case of an ASP.NET page.

Change Program Buffering Okuma OSP-300M

I'm currently running a small loop in g-code that has to wait for a common variable to change values. With Program Buffering ON, my g-code program does not see changes to the variables!
What is the best way to turn Program Buffering OFF while I am in this g-code loop?
If I manually set Program Buffering (NC Optional Parameter Bit No.2 Bit 7 to "DOES NOT". Then my loop behaves appropriately and the controller properly checks the value of the common variable each loop.
NLOOP G04 F1
IF[VC890 EQ 0] GOTO NRTS
GOTO NLOOP
NRTS RTS
Very straight forward loop. Maybe it needs to be more complex.
Perhaps if it was longer the buffer wouldn't matter?
I expect my customer's will want Program Buffering turned on.
Can I turn it off temporarily with the THINC API?
Because if it works, this would be great:
public void SetNCOptionalParameterBit(
int intBitIndex,
int intBitNo,
OnOffStateEnum enValue);
If this function will let me set param bit no 2 bit 7 on and off then this would probably be a valid work around.
Okuma.CMDATAPI.DataAPI.COptionalParameter myCOPtionalParameter;
myCOptionalParameter = new Okuma.CMDATAPI.DataAPI.COptionalParameter();
myCOPtionalParameter.SetNCOptionalParameterBit(2, 7,
Okuma.CMDATAPI.Enumerations.OnOffStateEnum.On);
What about M331 to prevent read ahead? (I won’t be at a control for a few days to verify usage, I’m holding my newborn and it’s 4am right now but I think it can go either on the line where you read variable on or the line before.)
NLOOP G04 F1 M331 (buffering prohibit)
IF[VC890 EQ 0] GOTO NRTS
GOTO NLOOP
NRTS RTS
The SetNCOptionalParameterBit() function is capable of setting NO. 2, BIT 7.
However, depending on what version of API you have, the THINC API test application might fail to do so. I confirmed there is a bug in the test app for API 1.17.2.0. And it was fixed by the time 1.18.0.0 was released.
So just be aware of that. Even if your machine has an older API such as 1.17.2.0, you should still be able to write code that uses this function successfully. Just ignore the test app results.
The best solution for my scenario was saving the current value of NC Optional Parameter 2 into a common variable, then changing it to Does Not buffer then running my code, then putting it back to whatever it was before.
in Gcode:
VC892 = VOPRB[2] (save current NC Optional Parameter bit 2 value)
VOPRB[2] = [VOPRB[2] OR 128] (bit magic to flip bit 7 to a 1 if its not)
(insert code to be run without buffering)
VOPRB[2] = VC892 (put back saved NC Optional Parameter bit 2 value)

C# - locking if statements - moving variable

I have a simple code with one moving variable and a few ranges. The variable either increases or decreases at pseudo-random and I have no control over it. The ranges are my if statements.
Each if statement has two commands and I need one of the two commands to be met and executed, before the code can move on.
The problem is the variable can move into a different range before one of the commands is executed, which forces the code to expect two different commands.
How can I keep the code from doing anything else until one of the commands is executed, despite where the variable goes?
Note: It is guaranteed that one of the commands will execute given enough time.
While loops have not worked as they are seen as infinite loops.
Bools such as "trigger one or two was executed == true/false" have also not worked, and trapping the code in a bool statement yields the same result as the while loop. I have also tried a switch statement, but it was no different that the collection of ifs below.
I have looked into using state machine and recursive methods, but at this time they are a little beyond me and don't know if they will work for me now.
The below code is a generic example:
int MP = moving variable; //updated every iteration of the code
int R3, R2, R1, S1, S2, S3; //static variables input by user- in descending order
//MP usually starts between S1 and R1
if(R3 < MP < R2)
{
command one; //an if statement that gives command when triggered
command two; //another if statement
}
if(R2 < MP < R1)
{
command one;
command two;
}
if(R1 < MP < S1)
{
command one;
command two;
}
if(S1 < MP < S2)
{
command one;
command two;
}
if(S2 < MP < S3)
{
command one;
command two;
}
If it helps, I can bring in the actual code, but I do believe I have narrowed down my problem to this and am portraying it as simple as I know how. I can also go into more detail about any part of this.
I am hoping this is as simple as me overlooking an option, or perhaps there is a something I have not learned about yet.
Thank you for your time
Edit- This is an automated trading algorithm. MP is a live feed from the broker for a specific commodity price and the ranges are price points that the user thinks the commodity price will reach. The two actions are placing buy or sell orders, which are executed when the price hits certain levels. Trouble arises because, one of the price points is inside a different range(the stop order for those who know about trading). So there is overlapping of if statements briefly. What is happening, is that the above or below range's orders are being executed before the stop order from a neighboring range. That is why I need one of the initial orders to execute, before new ones are submitted.
I am sure this only made things more confusing, that is why I am trying to keep it at a very conceptual level.
-end edit
Assign MP to a temporary variable and then do a while loop around your if statements. Re-assign MP to the temp variable on every iteration of the loop. That way you are guaranteed that the variable will not change in flight.
The code you posted, the value of MP will not change between when it is initialized and the bottom of the last if statement.
If you are concerned with whatever calling code you have running this method of yours in multiple threads, where by the time the second one is executed, the first one hasn't finished. Then you must put your values in a Queue(ConcurrentQueue more likely) and process them from there.
That is why I need one of the initial orders to execute, before new
ones are submitted
Your statement here, screams "I need a processing Queue".

Why can't this read integers from my text file? Sytem.FormatException

Okay, so I am making a game that reads data from a text file to create events. What happens is as each year advances if something exciting happens a popup box with three options appears, clicking them affects the game and so on.
I have created a function which can take various arguments and make the form automatically - so far so good- but writing large event descriptions in the code is messy and disorganized. Instead I decided to create another function which takes values from a text file, organizes them and then calls the second function to create the 'event'.
Now, as I said each event comes with three choices (See below) and each choice has a consequence on one of three factors (approval, prestige, power), I haven't quite worked out the mechanics properly but all in good time, everything runs wonderfully until I need to load this integers from the text file.
It keeps having trouble converting the string to an integer, why is this and how can I fix it?
Line 11 of the text file: 10 (yes I checked and it is the right line to read)
The code:
List<int> affecta = new List<int>();
affecta.Add(Int32.Parse(System.IO.File.ReadLines(filename).Take(11).First()))
I can load the other things such as the picture file's location perfectly, so 'filename' points to the correct .txt
It might be something really obvious to someone with more experience, but I just can't see why.
I don't think Take does what you think - It grabs the first 11 items and returns all of them, so you get an IEnumerable of 11 items. When you do First on that, you get item at position 0, not position 10. I think you want Skip and then First, which will skip the first 10 items and return the next (11th) item:
affecta.Add(Int31.Parse(System.IO.File.ReadLines(filename).Skip(10).First()))
If you use Take(11) this means "get 11 rows from the source". After that you have First(), so you'll get first of them. You're essentially trying to convert the first line into integer.
I assume you want to use Skip(10) since you want the 11th line.
Take(11).First() returns the First element from the IEnumerable containing the 11 elements.
Instead, Skip the first 10 and select the First from the IEnumerable.
affecta.Add(Int32.Parse(System.IO.File.ReadLines(filename).Skip(10).First()))
Alternatively, Take the first 11 and select the Last from the IEnumerable.
affecta.Add(Int32.Parse(System.IO.File.ReadLines(filename).Take(11).Last()))

error provider is not blinking uniformly

I have an error provider providing error for 4 controls..
when I set all the four errors, only two of them blink together at a time and all four settle down after certain time..
even if I set two errors, both blink alternatively..
but I want all of them blink together...How can I do this? (I don't prefer using more than one errorProvider)
You've hinted to an (IMO) acceptable solution, with your last statement: use 2 error providers, one dedicated to blinking exactly one control at a time (the latest one with an invalid input, or the one you'd expect to be corrected ASAP, or whatever criteria you deem most important), and a second one that "silently" displays the icon on all controls with invalid input.

Categories