[SOLVED] Calling methods every n seconds in scene.update()
Hi, I want to know if it is possible to call a method every n seconds (example: every 1 second) in scene.update.
If this is possible, can you please provide the right way of going about this?
Thank you, and God bless you all abundantly in Jesus Christ!!!
So @Splefix - I have done something similar previously, where I needed to run different tasks at different intervals. Specifically, it was to upload IoT data from a pressure/temp/humidity sensor I have here, to an online MQTT server - but that is by the by.
This script shows how to set up 3 separate timers for task1, task2 and task3 (obviously amend the number of variables for your specific requirements)
# you will need the time module, so... import time # Then simply set up timers for each task you wish to run last_task1=time.time() last_task2=time.time() last_task3=time.time() # Execute specific tasks within each variable you have set up above # to execute task1 every 5 seconds if time.time()-last_task1>=5: # run whatever code you need to execute every 5 seconds here # put your code for task1 here # then reset the timer for this task1 last_task1=time.time() # to execute task2 every 300 seconds if time.time()-last_task2>300: # run whatever code you need to execute every 300 seconds here # put your code for task2 here # then reset the timer for this task2 last_task2=time.time() # to execute task3 every 3 seconds if time.time()-last_task3>3: # run whatever code you need to execute every 300 seconds here # put your code for task2 here # then reset the timer for this task3 last_task3=time.time()
Hope this helps.
@Splefix - specifically for your example...
# you will need the time module, so... import time # set the variable for your single timer last_task1=time.time() # then execute task1 every 1 second if 'your_statement' is true if your_statement == True: if time.time()-last_task1>=1: # run whatever code you need to execute every 1 second here # put your code for task1 here # then reset the timer for this task1 last_task1=time.time()
dont use time module in scene. use the scene t parameter.
here is an example showing three methods i mentioned
@musicman305 Haha brother, God just helped me figure that out myself! Thank you very much though for your answer! God bless you abundantly and may peace be with you!!!
Nice meeting you brother!
why are you deleting you goal/score label every frame! instead of just updating the text?
@JonB Hey, can I ask you to show me an example of calling a method containing args using the example you gave me above?
use lambda, or functools.partial.
i.e Action.call(lambda arg1:self.some_method(arg1) )
@themusicman Haha brother, God just helped me figure that out myself! Thank you very much though for your answer! God bless you abundantly and may peace be with you!!!
Nice meeting you brother!
You're very welcome matey...
@JonB Concerning your question above: Can you show me how to update the text? This is my first time using the scene module.
Also, say I have two args, and the function is out of the MyScene class: How would I tackle this?
see my example, the .text attribute is used to update text.
note, you probably want to set the anchor_point=(0,0) for left justification, unless you want the text centered.
Lambdas accept multiple args
lambda arg1,arg2:some_function(arg1, arg2)
If you do a lot of these types of things, I (once again) promote the scripter, which works with views and scenes, for example like this:
from scene import * from scripter import * class MyScene (Scene): def setup(self): self.ship = SpriteNode('spc:PlayerShip1Orange') self.ship.position = self.size / 2 self.add_child(self.ship) self.do_a_thing_every_second() @script def do_a_thing_every_second(self): while(True): print('do the thing') yield 1.0 run(MyScene())
@mikael Hi friend, I am having trouble with importing scripter? It says that this is invalid syntax:
from scripter import *
@Splefix, as an unfortunate barrier to entry, you need to download
scripter.pyfrom the link and place it in your site-packages.
@mikael Hey sorry man, I am new to Pythonista, especially concerning the installation you are talking about. Can you please help me install scripter? If it isn’t too much trouble. I haven’t worked with github before either.
@Splefix, do you have stash installed? If not, recommend that you install it anyway. (Google for ”pythonista stash” for instructions).
When you have it, start stash, then:
StaSh v0.7.0 on python 3.6.1 Warning: you are running StaSh in python3. Some commands may not work correctly in python3. Please help us improving StaSh by reporting bugs on github. Tip: Bring a background job to foreground with fg JOB_ID [~/Documents]$ cd site-packages [site-packages]$ wget https://raw.githubusercontent.com/mikaelho/scripter/master/scripter.py
@mikael Hey man, I did that, but now I get the following error:
- Name 'script' is not defined
This concerns the following code:
@script def do_a_thing_every_second(self): if self.auto_amount >= 1: while(True): spawn_bubble(self, False) yield 1.0
@Splefix, do you have the file in site-packages and still have the import statement at the top? Import statement with the asterisk and not just
Yes, but it is okay. I will just use the time module instead. Thank you all for your help. God bless you all in Jesus Christ’s MIGHTY NAME!!!!