levelc omplete with global variable - construct-2

I have an issue with finishing a level using global variable when I make a global variable for enemy count is = 0 then add 1 to level variable, but when I start the game and destroy all enemies the global variable keep adding 1 to levels Continuously and all levels are unlocking along with this variable.
Now I want it should add only one level

2 Issues here:
Use add trigger once while true as a condition to the event that checks if count = 0. Without this, every tick that condition is true (before another enemy spawns) level will be incremented.
You don't wan't to start checking until the first enemy spawns. One possible solution is to add a global variable called isGameStarted initialized to 0. Then using an onCreated event for your enemies, set isGameStarted to 1. Finally, add another condition to your level increment event that checks to make sure isGameStarted = 1. Any time you are going to wipe all the enemies, make sure to set isGameStarted = 0 first to avoid triggering a level change.

Related

how to use jump to if I need to evaluate a condition of context variable for two different nodes at same time

I have one parent node ,based on the user input Iam setting a context variable at my application level eligibility:yes or no and passing back.And for my parent node I have two child nodes for conditions $eligibility=="yes" and $eligibility=="no".So once users input from parent node validation is done and context variable is passed back ,then I need to jump and look for condition of eligibility.If yes I need to go one node ,if no then to other.How can I do?
I tried putting true to node and added these two nodes to this and jump to true..But didnt worked..How can we achieve this?
what #data_henrik has mentioned is a good way to set context value and then switch to different flows depending upon the set value. But when you need to perform some logic before setting that value in the context from your application, it won't be a suitable way.
I had a requirement like this, so we used to send a dummy text from our application after we were done with setting the value in context after the parent node execution. Check out the images and explanation after that.
We didn't use Jump because we had to do some validation in the Conversation service after parent node before moving forward. Using jumps would've allowed the Conversation to move to next node before we could set value in context.
Use case flow - once user enters text for the parent node intent, for my case "#send-mail" intent, I show the parent response and do some functional validation in my app after that and add a value to the context. Now we send a dummy text "valid" which satisfies the intent "#Valid" and hence move to the next node in flow. In this node we check for the value in context (which is already set by now) and show appropriate response to user.
You can set within your first two test nodes, $testMe==true and $testMe==false a temp output variable within the output json packet, i.e. output{"temp":"true"} or "false". Then you can jump to a new set of nodes and test for the output.temp value, i.e. output.temp == 'true' then do something, or output.temp == 'false' then do something.
The nice side effect of this action is that the output.temp variable only has a life of that current conversation input. Unlike context variables which need to be removed / deleted.

Is it possible to track changes to a variable in MATLAB?

Say you have a recursive function that changes part of a global variable.
Eg. Global variable is a 3D array from 1 to 20 (i.e (:,:,20), and in one recursion (:,:,3) and (:,:,5) are changed, and in the next recursion (:,:,7) and (:,:,8) are changed. Is it possible to see when each value for one of the elements is created, so see that in the first recursion the 3rd and 5th element were assigned a value, and in the second recursion the 7th and 8th were?
There are a couple of ways I can think of.
Pass an extra argument to your function that acts as an accumulator and return this at the end. This is standard recursion practice if you're dealing with recursion so I won't explain further (though happy to if you ask). If you haven't dealt with accumulators before, then I'd avoid this as it's not the simplest concept to wrap your head around.
Append to a global variable from inside your function that acts as a counter and keeps track of what you want it to.
Alternatively, create a persistent variable inside your function to act as the counter instead. You can either return this all the way back to the first call (as you would with an accumulator), or use it to update a global variable outside your function.
You can avoid globals if you're happy to make your recursive function a closure returned by a generator function, such that it has state relating to your counter variable.
Happy to expand on any of the above.

Remove a term from an array during a for loop

I've been creating a system for storing objects that persist for a specified duration, then remove themselves. However, I'm having some trouble figuring out how to remove expired items from the array while the for loop is running (to minimize extra iterations through the array)
Here is what I've made so far. Terms in the list are flagged with a Boolean value indicating that they're done.
For i = 0 To VisualEffects.Count - 1 Step 1
VisualEffects(i).Update(gt)
VisualEffects(i).Draw(sb, Pos, Cam)
If VisualEffects(i).CD.isExpired() Then
VisualEffects.RemoveAt(i)
i = -1
End If
Next
Why does this produce an error? How can I remove a term from an array and continue iterating through the remainder of the loop?
Regards
Ares
The line
i = -1
sets i to -1, causing the For loop to terminate.
If you can, reverse the order of the loop so that you start with the last array element and count down. That makes the remove logic much more straightforward.
.NET arrays are fixed-size so you can't actually remove an element from an array at all. You can change the value of an element, so you can set an element to Nothing to remove the object that it referred to, but the element is still there. If you read the MSDN documentation for the Array class, you'll see that the RemoveAt method throws a NotSupportedException for this reason.
If you're actually using a collection rather than an array then you can call RemoveAt but you need to loop from the last index to the first rather than first to last. That way, removing an item will not affect the indexes of those items that you are yet to visit.

For Loop iterated twice even when size of NodeList is 1

I am facing some kind of a weird(as it appears to me) problem. I have a NodeList and need to remove an element from the NodeList while iterating through it. The NodeList has only one child element , so after removing that element that NodeList does not have any child element. Ideally the for loop should have stopped after removal of that element, but this is not happening and as the for loop runs for the second time, even when there are no child elements available, I am getting a NullPointerException.
Sample XML :
<Order OrderNo="1">
<Lines>
<Line LineNo="1"/>
</Lines>
</Order>
Sample Code :
NodeList nlLine = inDoc.getElementsByTagName("Line");
for(int cntLn = 0 ; cntLn < nlLine.getLength() ; cntLn++){
Element elLn = (Element) nlLine.item(cntLn);
if(//some condition){
elLn.getParentNode().removeChild(elLn);
cntLn--;
}
}
I am getting NullPointer exception on the linewhere i am getting the Parent Node and then removing the child. Any clue/lead/help on this?
You are decrementing the loop variable cntLn--.
I am pretty sure you meant to increment it cntLn++. And since you are already incrementing, just omit the line cntLn--;
TIP: As a healthy coding practice, always use the prefix increment operator ++cntLn in case of loops (unless you explicitly need the postfix behavior).
I think this behavior is caused by the last line of your loop (cntLn--;).
Assume, the list has one child, so you start your processing with cntln=0 and length=1 which means: execute the loop. Then you remove the node since the condition is evaluated to true and you decrease the counter to -1. After that your for-loop increases cntln again to 0 and you start again but now the node list of the Lines-element is empty!
Simply omit the last line and try again ;)
Nevertheless, I think this is a critical processing since you cannot foresee side-effects when removing nodes while looping over the node list. Maybe it would be better to store the index of each node to be removed in this loop and remove them later in another loop.
Let's see what your program does.
Getting Line nodes (nlLine)
Setting the cycle variable to 0, since it's bigger than nlLine's size (1), we are going in the cycle.
Supposing the condition is true
Removing a node from XML (nlLine not updated!)
decrement the cycle variable
Cycle variable is still 0 (because of step 5.) and the nlLine hasn't been updated so we are going in to the cycle again (0 < 1)
Since elLn did not have a parent at this time NPE will be thrown (it has been removed from the XML)
Solution: leave cntLn--; out from your code. You have to step to the next element of the nodelist, since elLn.getParentNode().removeChild(elLn); won't remove your actual node from the nodeList.
Tip: usually it's a bad practice to modify the list during for cycling over it. Although it's a bit different situation, but still try to avoid. You should do it AFTER the main loop. (e.g. create a removeThese list to store the elements you want to delete from the list you are looping, and you can remove them once you are done with the main loop. It's safe, it's easy to understand and easy to manage.)
Simply increment and not decrement cntLn by one when removing the child - the upper bound of the for loop is static, so your only chance to manage the state of the for loop is to modify the loop variable.

JMeter - loop controller with variable loop count

I am using JMeter and I want to define one loop controller. In this loop I want to define the loop count with a Jmeter variable. Is this posible?
Thanks a lot
I know is very late to help you, but it can help the others.
One good way to do this is to define a counter inside the loop controller.
Example, if you want a loop with 20 iterations, you put 20 in the loop controller and in the counter you put from 0 - 19. In the counter you can also define an increment. The output variable is named in the Reference Name field.
Yes, it's possible.
Define your user variable using the User Defined Variables component, or use the CSV component.
Then, in the Loop Controller, define loop count to: ${nameOfVar}
Since version 4.0 of JMeter, there is an easier way:
https://jmeter.apache.org/usermanual/component_reference.html#Loop_Controller
JMeter will expose the looping index as a variable named
__jm__Name of your element__idx
So for example, if your Loop Controller is named LC, then you can access the looping index through ${__jm__LC__idx}. Index starts at 0
If you're looking to learn jmeter correctly, this book will help you.
You can use the following inside the "Loop Count" field:
${myVar}
e.g. for a random count:
${__groovy(java.util.concurrent.ThreadLocalRandom.current().nextLong(128L),)}
You can access the current loop index (starts at 0) via ${__jm__Loop Controller__idx} where Loop Controller is the name of your loop.

Resources