Post by anl515 on Mar 16, 2020 13:49:07 GMT -5
Hello , I started to write some python scripts and i just wondered how the loops actually work in a python script. I tried the code below. It works but it actually freezes the game scriptvise until the script is force-stopped.
Can someone help me about how to make loops without freezing the game scriptvise (If i need to give a example you get 10 simoleons every 1 minutes or etc. ). I think there is a another way instead doing it in commandline but i can't find it. Thanks in advance
(Sorry for bad grammar...)
The things i tried:
1-Making "while True" to "while 1=1" or defined some x to make it
2-Tried to make another function that runs the actual function
EDIT:
Using thread function in python library with "continue" and "breakout" functions are used to control the while loop without making other scripts stop or distrubted.
The code i achieved after doing it:
import sims.sim_info
import sims4.commands
from objects.components.consumable_component import ConsumableComponent
import services
import re
import time
@sims4.commands.Command('updatebodytoggle', command_type=sims4.commands.CommandType.Live)
def updatebodytoggle(_connection=None):
output = sims4.commands.CheatOutput(_connection)
output("Trying...")
client = services.client_manager().get_first_client()
sim_info = client.active_sim.sim_info
while True:
sim_info._sim_ref().needs_fitness_update=True
sim_info.update_fitness_state()
output("Update Succesful")
time.sleep(1)
# I tried to cooldown first before restarting the loop but it didn't worked.
The code intends to update the active sim's body shape every 1 second (I may change this i'm just trying to understand how loops work)Can someone help me about how to make loops without freezing the game scriptvise (If i need to give a example you get 10 simoleons every 1 minutes or etc. ). I think there is a another way instead doing it in commandline but i can't find it. Thanks in advance
(Sorry for bad grammar...)
The things i tried:
1-Making "while True" to "while 1=1" or defined some x to make it
2-Tried to make another function that runs the actual function
EDIT:
Using thread function in python library with "continue" and "breakout" functions are used to control the while loop without making other scripts stop or distrubted.
The code i achieved after doing it:
import sims.sim_info
import sims4.commands
from objects.components.consumable_component import ConsumableComponent
import services
import re
import time
import threading
@sims4.commands.Command('updatetoggle', command_type=sims4.commands.CommandType.Live)
def updateopen(_connection=None):
class ThreadingExample(object):
def __init__(self, interval=1):
self.interval = interval
thread = threading.Thread(target=self.run, args=())
thread.daemon = True # Daemonize thread
thread.start() # Start the execution
def run(self):
while True:
client = services.client_manager().get_first_client()
sim_info = client.active_sim.sim_info
sim_info._sim_ref().needs_fitness_update=True
sim_info.update_fitness_state()
time.sleep(1)
example = ThreadingExample()