pyglet on_draw event occurs only when mouse moves - pyglet

I have a strange problem. When pyglet app starts it just draws 1-2 frames then freezes. on_draw event just stops occuring. But everytime I move mouse or press keys, on_draw event dispatches as well. In short I have to move mouse to make my pyglet application basically work.
This is actually happens in Windows. In Ubuntu with compiz I've to move mouse just once then application starts working normally.
This is my code example:
#!/usr/bin/env python
import pyglet
win = pyglet.window.Window(width=800, height=600)
label = pyglet.text.Label('Abc', x=5, y=5)
#win.event
def on_draw():
win.clear()
label.x += 1
label.draw()
pyglet.app.run()
Here's a video explaining things.

I came across this last night while trying to figure out the same problem. I figured out what causes this.
I had used a decorator and put my updates in the on_draw method, and it would run okay for a little while, then it would freeze, only to start working again when I moved the mouse or hit the keyboard. I tried all sorts of tricks to figure it out, I finally thought that maybe things were just running too fast, and that putting them in a batch and letting pyglet decide when to update them would be better. It worked.
I also scheduled things so that they would run about twice as fast as my refresh rate, but not so fast it would bog anything down. This is more than enough for smooth animations.
needles_list = [gauges.speedometer.Needle(speedometer_data, needle, batch=batch, group=needles),
gauges.tachometer.Needle(tachometer_data, needle, batch=batch, group=needles)]
def update(dt):
for needle in needles_list:
needle.update(dt)
pyglet.clock.schedule_interval(update, 1/120.0)
gauges.speedometer.Needle and gauges.tachometer.Needle are subclasses of pyglet.sprite.Sprite, and I wrote an update method for each of them. I then called their draw method in on_draw as normal.
#window.event()
def on_draw():
window.clear()
batch.draw()
I know this question has been up for a while, and the asker may have given up already, but hopefully it will help anyone else that's having this problem.

I had a similar problem of update events not getting called (using pyglet from Cygwin on Windows), and it turned out there was a bug:
https://groups.google.com/forum/#!msg/pyglet-users/Qp1HzPHcUEQ/A9AFddycLSAJ
I'm not certain it's the same problem you're having, but it seemed worth mentioning. I ended up hacking the file mentioned (pyglet/app/win32.py) by hand (setting self._polling = True around line 105, in Win32EventLoop._timer_func) and all my updates started flowing just fine.

Related

Anylogic: Running a model but stuck suddenly without any error

I'm new to Anylogic and created a simple traffic model. Only use 'carSource', 'CarMoveTo', 'Car Dispose' blocks to set the car routes. But After I ran the model, it worked for a while, then all the cars froze without any error occurring. ’Events‘ panel also stopped. How to solve it?
Most likely your model is running into an infinite loop somewhere in the logic. The first place to check would be all your loops that might become infinite,e.g Do loops, Do-while loops, iterator for loops where you perhaps change the counter variable manually...
If you have the professional version of AnyLogic the best option is to run the model in debug mode until you get this to the point where it freezes and then press pause. You will then see where in the code the model is getting stuck.
If this does not work you might need to start putting traceln in major functions and see ing you can spot the last traceln that gets printed and keep on adding more and more until you can find the point between two traceln where the model freezes
I had the same problem, that after a certain time, all cars froze and there wasn't a signle error.
The problem on my side was that the stop line was too close to the intersection, so I moved it a little bit farther.

Unity New Input System and Animations

I am creating a basic 2D fighter game and am trying to replace what was the old input system with the new input system. Because of this the old systems update would wait for buttons and then call appropriate functions when they were pressed. Movement was actually the easiest part, simply rigging a 1D Vector and then grabbing the float to use with movement, awesome. However the difficulty is in pressing keys to change things in the game. For example, I have an input 's' which should lead to the method Crouch(). This method changes the animation running and alters the hitbox to be shorter and tell the attack to use a smaller hitbox as well. A Stand() method is called on release of 's' to return everything to the way it is. I have my Player Input object set to 'Invoke Unity Events' which leads to the corresponding method needed. The problem is that even though these events are set to be push and release they are read as a toggle effect instead of press and release triggers(respectively). This does the same thing with my attack function because it forces the animation to play twice, one for pressing and one for releasing. Is there a fix for this or is this currently a bug? Edit: Here are some images to clarify and the code used to reflect everything happening associated with the Attack functionality. Let me know if anything else should be needed
public void Attack(){
anim.SetTrigger("attack");
StartCoroutine(wait());
}
IEnumerator wait(){
if(!isCrouched){
yield return new WaitForSeconds(1.3f);
SHitBox.enabled = true;
yield return new WaitForSeconds(.5f);
SHitBox.enabled = false;
}
if(isCrouched){
yield return new WaitForSeconds(1.3f);
CHitBox.enabled = true;
yield return new WaitForSeconds(.5f);
CHitBox.enabled = false;
}
}
Binding
Action
Think I figured it out. At least for the purposes of this specific thread. The answer was it was not my code. There is what I am going to call a bug in the new Unity Input System. Specifically there were three lines of code all simultaneously being hooked which caused three calls on my method. The fix was commenting out two lines of code. Here's the thread where this is solved, coincidentally found on GitHub help pages, heres the link:
https://github.com/Unity-Technologies/InputSystem/issues/959
the issue is listed as close but its still a problem for me, lol...
The only issue left is that the behavior of selecting the type of button press that I want to use is still acting funky. Mainly the inputs are still simply firing without listening to the type of input I want. I am basically just going to start searching through unity code to find where these choices have impact. If there are no comments/answers in 8 hours I'll accept my own answer because this has technically been answered, it just leads to another question.

Can you really "rewind/reverse" the debugger in Swift/Xcode?

When you are stepping through Swift code in Xcode (9/10?), there is a green bar on the right with something like:
You are supposed to be able to drag the partial-hamburger-menu upwards to rewind the statement pointer to re-run code. However, every time I try it it moves back as expected, but then 100% of the time I step from that point I get:
Is there a trick to this?
You can move the pointer to the next statement to be executed to either a previously executed statement or a not-yet-executed statement, but in order for that to work the stack needs to be in the correct state, and so do the variables in memory.
In my experience, the outcome is usually a crash.
You'd need to drop down to the assembler code and examine it in order to figure out what's really going on, and might need to patch variables and/or the contents of the stack in order for your code to survive the change of program counter. I've never invested the time to try to do that, however. As a result I find the feature pretty much useless, and have given up on it. (I've worked in assembler a LOT in years past, but never learned enough about ARM assembler to be able to read it well, much less hack registers, memory, and the stack to make moving the program counter work.)

Method for Back button not working when Time.TimeScale=0

I have written below code for Back button event.
void Update()
{
if (Input.GetKey(KeyCode.Escape))
{
SceneManager.LoadScene("PreviousLevel");
}
}
In almost all cases this is fine. But I have found a small issue. When the user pauses the game, this doesn't work. When the user pauses the game, I do Time.timeScale=0. Initially, I gave a thought of modifying pause method and instead of doing Time.timeScale=0, use a bool variable and modify other pause logic accordingly. But then I also realized that I have over 14 co-routines whose logic is heavily dependent on Time.timeScale and modifying those will take a lot of time. They are heavily dependent on time.timeScale.
I wanted to know, is there any other method where I can write back button logic and which is not dependent on Time.timeScale.
It should work, input polling in Unity is not dependent on the time scale.
Try by inserting a Debug.Log inside the condition, and you should see it in the console.
Watch out if you put the if inside FixedUpdate and not Update: in that case it won't work since FixedUpdate is completely skipped when time scale is 0.
However, if you want a "dirty" trick, you can slow the timescale to a very low number, without using 0, i.e.: 10e-8. But use it with a lot of care, since it can lead to unwanted behaviour.
Input is dependent on time scale: GetButton() works normally but GetAxis() works inconsistently, for example getting mouse movement with GetAxis() works as expected, but getting Horizontal and Vertical returns 0 when timeScale is 0. You can get around this by using Input.GetAxisRaw().
It should also be noted that any axis also counts as a button, so you can also use GetButton() on those but the return value will be bool instead of float.

What do nested "touching" blocks do?

Making a Scratch platformer, I discovered that this did not work how I wanted:
...because it just didn't seem to run at the right rhythm for my program.
However, this did work:
...and I notice it being used in platformers such as Sushi Platformer.
Does anyone know what these nested touching blocks actually do?
Update:
#towerofnix is right... I made an experimental project, at https://scratch.mit.edu/projects/118349555/#player
...with the following experimental options:
Results:
Control: Platform detection works, but jitters up and down too much
Experiment1 (per #towerofnix): works, no jittering!
Experiment2: doesn't work, penguin goes down through floor sometimes
Experiment3: works, because it's equivalent to Experiment1!
Scratch isn't magic. It's a normal code interpreter (as far as I know).
Now that that's out of the way, let's be the interpreter™ and figure this out.
touching-color will return true or false.
touching then takes the input true or false.
touching will go through all sprites named true or false.
since there are no sprites with those names (at least not in sushi platformer), just continue.
next is return false, so return false.
therefore the nest will always return false.
not takes the input false, so it returns true.
thus this - - is unnecessary and can be replaced with this -
So if doing that last step doesn't result the same as not doing the last step, we really need to see your project!
You need to do something special here...
You want to stop the jittering, you'll have to stop the gravity. Set a condition on the gravity so that it stops whenever your sprite touches the platform. Then, have gravity reactivate on your jump button. The way this is now, it touches the platform, jumps up (a tiny bit), and then touches the platform again. You need to change the way your gravity works.
If you want to avoid jittering, you can make a block and select "run without screen refresh" and move the entire if