Lua: How do you update a variable that's in an event? - if-statement

The issue I'm running into is that my variable isn't updating after an event. Here is my code:
local Players = game:GetService("Players")
local intermission = 15 -- time in seconds
local AmountOfPlayers = #Players:GetPlayers() -- starts at zero
local minPlayers = 1
Players.PlayerAdded:Connect(function(Player) -- updates amount of players in server
AmountOfPlayers = AmountOfPlayers + 1
end)
Players.PlayerRemoving:Connect(function(Player) -- updates amount of players in server
AmountOfPlayers = AmountOfPlayers - 1
end)
Below, this if-statement is not running since AmountOfPlayers is not equal to minPlayers.
while intermission > 0 do
if AmountOfPlayers >= minPlayers then
print("SUCCESS")
end
end
I have a scope issue of the variable AmountOfPlayers. I'm not sure how to fix this, so any suggestions will be appreciated. Thank you for your help!

You forgot to add a wait() in your while loop. This is most likely what leads to the script crashing and not working. So it should look like:
while intermission > 0 do
wait()
if AmountOfPlayers >= minPlayers then
print("SUCCESS")
end
end
OR
If that isn't the problem, instead of making:
local AmountOfPlayers = #Players:GetPlayers()
Instead:
local AmountOfPlayers = Players:GetChildren()
And Remove the two functions.

Related

How to get a total number of occurrences in an IF statement

After looking at a bunch of the other questions similar to mine, I found one that partially works but not exactly as I need it to. I am trying to get a total for the number of times an occurrence is found in my IF statement. The code I have tried is below:
for i in data['resources']:
url = server+ "/api/3/assets/" +str(i)
response = requests.request("GET", url, headers=headers, verify=False)
data = response.json()
Total = 0
if data['red'] and data['blue'] > 0:
Total += 1
print('Total:', Total)
Rather than giving me an output like:
Total: 3
I get this:
Total: 1
Total: 1
Total: 1
Total=0 inside the for loop and getting reset for each iteration. So you have to put Total=0 outside the for loop. And also if you want it to print only once then put the Print statement outside and at the end of the for loop
I am sure this should solve your problem

MPI master/slave code ending before completing all tasks

I am trying to run serial tasks in parallel using MPI. I had some very nice help from Sigismondo with this. However, I have run into an issue. When I run the following code, only 8 of the 16 tasks end up getting complete, one task completed per core. I am not sure why the master processor doesn't send another job after one of the slave processors completes a task. I was hoping to get some help. Everything else works perfectly fine though.
if(myid.eq.0) then
pending_tasks = 0
sent_tasks = 0
open(100,file="s2p_commands.txt")
do i=1,(numprocs-1)
read(100,'(A)') command
call MPI_SEND(command,200,MPI_CHAR,i,0,MPI_COMM_WORLD,ierr)
pending_tasks = pending_tasks + 1
sent_tasks = sent_tasks + 1
enddo
! all procs have one task to work on.
do
! wait for results - from any source
call MPI_RECV(result,200,MPI_CHAR,MPI_ANY_SOURCE,0,MPI_COMM_WORLD,istatus,ierr)
free_proc = istatus(MPI_SOURCE)
if (sent_tasks < nlines) then
read(100,'(A)') command
call MPI_SEND(command,200,MPI_CHAR,free_proc,0,MPI_COMM_WORLD,ierr)
sent_tasks = sent_tasks + 1
else
! all tasks sent, but wait all the results
pending_tasks = pending_tasks - 1
endif
#ifdef debug
write(6,*) "Processor ",myid," executes: ",trim(command)
#endif
if (pending_tasks == 0) EXIT
enddo
! in this point the master can send out the 'QUIT' command to all the slaves
else
do
call MPI_RECV(command,200,MPI_CHAR,0,0,MPI_COMM_WORLD,istatus,ierr)
! that's a suggestion for a clean exit policy - pseudocode, don't forget
if (command=='QUIT') EXIT
call MPI_SEND(result, 200,MPI_CHAR,0,0,MPI_COMM_WORLD,ierr)
enddo
endif
time2 = MPI_Wtime()
call system (trim(command))
time3 = MPI_Wtime()
write(6,*) "System call on myid=",myid," took ",time3-time2," seconds"
if(myid.eq.0) write(6,*) "Total time was ",time3-time1," seconds"
call MPI_FINALIZE(ierr)

How to redraw a curve to Photoshop with Autoit?

I want to draw a specific curve line to Photoshop or to mspaint. This drawing action should be saved for the possibility to redraw that curve in the exact same way. How can I do it with Autoit? Is there a recording and play mechanism? As far as I read, the AU3 recorder is not available anymore.
Photoshop is just an example. I want to be able to do that kind of drawing record for different purposes and programs. Maybe also for online image editors or something.
I am not that familiar with Autoit yet. I do not expect a full code example, maybe you can give me an idea - that would be very helpful.
Currently I tried a bit with mouse functions like MouseDown, MouseMove etc. and it is quite funny, but i do not really have a concept to record and redraw these mouse actions.
If I have to clarify more please let me know - i will do my best to be precise.
I recommend using two scripts, one for recording and the second to replay recorded actions.
Code for the recording:
; declaration
Global $sFileCoordinates = #ScriptDir & '\RecordedMouseMoveCoordinates.txt'
Global $iRecordingDurationInSeconds = 10
Global $iXSave, $iYSave
; functions
Func _recordMouseMoveCoordinatesToFile()
Local $aPos = MouseGetPos()
If $aPos[0] <> $iXSave Or $aPos[1] <> $iYSave Then
FileWrite($hFile, $aPos[0] & ',' & $aPos[1] & #CRLF)
Local $aPos = MouseGetPos()
$iXSave = $aPos[0]
$iYSave = $aPos[1]
EndIf
Sleep(80)
EndFunc
; processing
Sleep(4000) ; wait 4 seconds to place your mouse to the start position
Global $hFile = FileOpen($sFileCoordinates, 1 + 256)
Global $hTimer = TimerInit()
While Round((TimerDiff($hTimer) / 1000), 1) <= $iRecordingDurationInSeconds
ToolTip(Round((TimerDiff($hTimer) / 1000), 1))
_recordMouseMoveCoordinatesToFile()
WEnd
FileClose($hFile)
Recording will start after a 4 second delay. This should allow to move your mouse to the start point of your drawing action.
Global $iRecordingDurationInSeconds = 10 means your drawing action should be finished in 10 seconds (a tooltip displays remaining seconds). And here the seconds script.
Code to redraw curve:
; declaration
Global $sFileCoordinates = #ScriptDir & '\RecordedMouseMoveCoordinates.txt'
; functions
Func _getFileContent($sFile)
Local $hFile = FileOpen($sFile, 256)
Local $sFileContent = FileRead($hFile)
FileClose($hFile)
Return $sFileContent
EndFunc
Func _drawRecordedMouseMoveCoordinatesFromFile($sContent)
Local $aFileContent = StringSplit($sContent, #CRLF, 1)
Local $iX = StringSplit($aFileContent[1], ',')[1]
Local $iY = StringSplit($aFileContent[1], ',')[2]
MouseMove($iX, $iY, 4)
MouseDown('left')
For $i = 1 To $aFileContent[0] Step 1
If $aFileContent[$i] <> '' Then
Local $iX = StringSplit($aFileContent[$i], ',')[1]
Local $iY = StringSplit($aFileContent[$i], ',')[2]
MouseMove($iX, $iY, 4)
EndIf
Next
MouseUp('left')
EndFunc
; processing
Sleep(2000) ; wait 2 seconds till start
Global $sFileContent = _getFileContent($sFileCoordinates)
_drawRecordedMouseMoveCoordinatesFromFile($sFileContent)
There is a start delay of 2 seconds. All saved coordinates will be executed in the same way recorded. It starts with MouseDown('left'), then the mouse movements to MouseUp('left').
Notice:
This approach isn't really robust because of the coordinates which aren't relative to your window. Please see Opt('MouseCoordMode', 0|1|2) in the help file for more information. If you want to draw more than just one line or curve this approach isn't the best. But as your question describes only that requirement it should be fine.

How can I fix the timer to avoid values below 0 seconds?

while gamerunning:
timerstart = time.time()
posy2 += 3
if posy2 >= window_height:
posy2 = -50
randomx = random.randrange(0, window_width - 50)
if health == 0:
gameover = True
while gameover:
timerend = time.time()
timer = timerend - timerstart
print timer
screentext("Game over, press esc to quit or press R to retry", black)
pygame.display.update()
I'm trying to figure out how to correctly implement a timer in python but I get values below 0 seconds when I print it out so I'm assuming that the timer variable is only printing out the time it takes for the python interpreter to run the code inside my gameover loop. My question is how can I fix this problem and make a proper timer that runs while the gamerunning loop is going and end once it's game over. I'm rather new to Python so I'm not sure what other modules I can really use to accomplish this other than time.
Try moving timerstart = timer.time() outside the loop that runs your game. In your example that is before while gamerunning:. The problem was that in every loop the variable was being reset, thus the results you got.
We can't see the rest of your code in that image, but in this case in order to update the start time between games you have to update timerstart after/if the player selects 'R' to retry the game. Otherwise your game ends.

Struggling with syntax with If....Then statements in model, Rails 3.2.13

I'm trying to write an equation that calculates how long an employee has been hired to determine how much Vacation time they are eligible for. New hires get 10, after six years of employment they get an extra day a year, capping off at 10 extra days (on their 16th year). Some of these equations worked individually, but they don't work all together. So I'm having a syntax problem I think.
undefined method `-' for nil:NilClass
The vacation_days section is what is breaking my app.
class Employee < ActiveRecord::Base
def years_employed
(DateTime.now - hire_date).round / 365
end
def vacation_days
if years_employed <= 6
10
end
if years_employed > 6
(years_employed.to_i - 6) + 10
end
if years_employed > 16
(years_employed * 0) + 20
end
end
end
Also, if you have any advice on a better way to go about this, please instruct me!
You don't want ends, you want elses, otherwise it's going to keep evaluating–so you were returning nil sometimes. Roughly:
def vacation_days
if years_employed <= 6
10
elsif years_employed <= 16
years_employed + 4
else
20
end
end
just as an alternative you can use a case statement along with ranges
def vacation_days
case years_employed
when 0..6 then 10
when 7..16 then years_employed+4
else 20
end
end