1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
|
//TODO: Add "retrigger if held" functionality to stick
// Player input callbacks
global function AddButtonPressedPlayerInputCallback
global function RemoveButtonPressedPlayerInputCallback
global function AddButtonReleasedPlayerInputCallback
global function RemoveButtonReleasedPlayerInputCallback
global function AddPlayerHeldButtonEventCallback
global function RemovePlayerHeldButtonEventCallback
global function AddPlayerPressedForwardCallback
global function RemovePlayerPressedForwardCallback
global function AddPlayerPressedBackCallback
global function RemovePlayerPressedBackCallback
global function AddPlayerPressedLeftCallback
global function RemovePlayerPressedLeftCallback
global function AddPlayerPressedRightCallback
global function RemovePlayerPressedRightCallback
global function DEBUG_AddSprintJumpHeldMeleePressed //Only for reference on how to do more complicated input events
global function CodeCallback_OnPlayerInputCommandChanged
global function CodeCallback_OnPlayerInputAxisChanged
void function CodeCallback_OnPlayerInputCommandChanged( entity player, int cmdsHeld, int cmdsPressed, int cmdsReleased )
{
foreach( callbackStruct in player.p.playerInputEventCallbacks )
{
if ( PlayerInputsMatchCallbackInputs( cmdsHeld, cmdsPressed, cmdsReleased, callbackStruct ) )
callbackStruct.callbackFunc( player )
}
foreach( callbackStruct in player.p.playerHeldButtonEventCallbacks )
{
if ( cmdsPressed & callbackStruct.buttonHeld )
thread RunHeldCallbackAfterTimePasses( player, callbackStruct )
}
foreach( callbackStruct in player.p.playerHeldButtonEventCallbacks )
{
if ( cmdsReleased & callbackStruct.buttonHeld )
{
string endSignalName = GetEndSignalNameForHeldButtonCallback( callbackStruct )
player.Signal( endSignalName ) //Send signal to kill corresponding RunHeldCallbackAfterTimePasses
}
}
}
void function CodeCallback_OnPlayerInputAxisChanged( entity player, float horizAxis, float vertAxis )
{
//printt( "Axis Changed: X: " + horizAxis + " Y: " + vertAxis )
foreach( callbackStruct in player.p.playerInputAxisEventCallbacks )
{
if ( ShouldRunPlayerInputAxisCallbackFunc( horizAxis, vertAxis, callbackStruct ) )
RunPlayerInputAxisCallbackFunc( player, callbackStruct )
}
}
void function AddPlayerInputEventCallback_Internal( entity player, PlayerInputEventCallbackStruct inputCallbackStruct ) //Not really meant to be used directly unless you know what you're doing! Use utility functions like AddButtonPressedPlayerInputCallback instead
{
if ( !player.GetSendInputCallbacks() )
player.SetSendInputCallbacks( true )
Assert( !InputEventCallbackAlreadyExists( player, inputCallbackStruct ), " Adding the same inputEventCallback " + string ( inputCallbackStruct.callbackFunc ) + " with the same inputs!" )
player.p.playerInputEventCallbacks.append( inputCallbackStruct )
}
void function RemovePlayerInputEventCallback_Internal( entity player, PlayerInputEventCallbackStruct inputCallbackStruct ) //Not really meant to be used directly unless you know what you're doing! Use utility functions like RemoveButtonPressedPlayerInputCallback instead
{
for( int i = player.p.playerInputEventCallbacks.len() - 1; i >= 0; --i ) //Removing from the end of an array, so it's fine to remove as we go along
{
if ( InputCallbackStructsAreTheSame( player.p.playerInputEventCallbacks[i], inputCallbackStruct ) )
{
player.p.playerInputEventCallbacks.remove( i )
break
}
}
TurnOffInputCallbacksIfNecessary( player )
}
bool function InputEventCallbackAlreadyExists( entity player, PlayerInputEventCallbackStruct inputCallbackStruct )
{
foreach( existingCallbackStruct in player.p.playerInputEventCallbacks )
{
if ( InputCallbackStructsAreTheSame( existingCallbackStruct, inputCallbackStruct ) )
return true
}
return false
}
void function AddPlayerHeldButtonEventCallback( entity player, int buttonEnum, void functionref( entity player ) callbackFunc, float buttonHeldTime = 1.0 )
{
if ( !player.GetSendInputCallbacks() )
player.SetSendInputCallbacks( true )
PlayerHeldButtonEventCallbackStruct callbackStruct
callbackStruct.buttonHeld = buttonEnum
callbackStruct.callbackFunc = callbackFunc
callbackStruct.timeHeld = buttonHeldTime
Assert( !HeldEventCallbackAlreadyExists( player, callbackStruct ), " Adding the same heldEventCallback " + string ( callbackStruct.callbackFunc ) + " with the same parameters!" )
string endSignalName = GetEndSignalNameForHeldButtonCallback( callbackStruct )
RegisterSignal( endSignalName )//Signal meant to kill the waiting thread if button is released. Note that registering the same signal multiple times seems to be ok.
player.p.playerHeldButtonEventCallbacks.append( callbackStruct )
}
void function RemovePlayerHeldButtonEventCallback( entity player, int buttonEnum, void functionref( entity player ) callbackFunc, float buttonHeldTime = 1.0 )
{
PlayerHeldButtonEventCallbackStruct callbackStruct
callbackStruct.buttonHeld = buttonEnum
callbackStruct.callbackFunc = callbackFunc
callbackStruct.timeHeld = buttonHeldTime
for( int i = player.p.playerHeldButtonEventCallbacks.len() - 1; i >= 0; --i ) //Removing from the end of an array, so it's fine to remove as we go along
{
if ( HeldButtonCallbackStructsAreTheSame( player.p.playerHeldButtonEventCallbacks[i], callbackStruct ) )
player.p.playerHeldButtonEventCallbacks.remove( i )
}
TurnOffInputCallbacksIfNecessary( player )
}
bool function HeldEventCallbackAlreadyExists( entity player, PlayerHeldButtonEventCallbackStruct callbackStruct )
{
foreach( existingCallbackStruct in player.p.playerHeldButtonEventCallbacks )
{
if ( HeldButtonCallbackStructsAreTheSame( existingCallbackStruct, callbackStruct ) )
return true
}
return false
}
void function DEBUG_PlayerHeldSprintJumpAndPressedMelee( entity player ) //Debug function, just an example on how to hook up more complicated InputEvents
{
PrintFunc()
}
void function DEBUG_AddSprintJumpHeldMeleePressed() //Debug function, just an example on how to hook up more complicated InputEvents
{
PlayerInputEventCallbackStruct callbackStruct
callbackStruct.cmdsHeldBitMask = IN_SPEED | IN_JUMP
callbackStruct.cmdsPressedBitMask = IN_MELEE
callbackStruct.callbackFunc = DEBUG_PlayerHeldSprintJumpAndPressedMelee
AddPlayerInputEventCallback_Internal( GetPlayerArray()[0], callbackStruct )
}
//List of valid inputs are in sh_constants for reference
void function AddButtonPressedPlayerInputCallback( entity player, int buttonEnum, void functionref( entity player ) callbackFunc )
{
PlayerInputEventCallbackStruct callbackStruct
callbackStruct.cmdsPressedBitMask = buttonEnum
callbackStruct.callbackFunc = callbackFunc
AddPlayerInputEventCallback_Internal( player, callbackStruct )
}
void function RemoveButtonPressedPlayerInputCallback( entity player, int buttonEnum, void functionref( entity player ) callbackFunc )
{
PlayerInputEventCallbackStruct callbackStruct
callbackStruct.cmdsPressedBitMask = buttonEnum
callbackStruct.callbackFunc = callbackFunc
RemovePlayerInputEventCallback_Internal( player, callbackStruct )
}
void function AddButtonReleasedPlayerInputCallback( entity player, int buttonEnum, void functionref( entity player ) callbackFunc )
{
PlayerInputEventCallbackStruct callbackStruct
callbackStruct.cmdsReleasedBitMask = buttonEnum
callbackStruct.callbackFunc = callbackFunc
AddPlayerInputEventCallback_Internal( player, callbackStruct )
}
void function RemoveButtonReleasedPlayerInputCallback( entity player, int buttonEnum, void functionref( entity player ) callbackFunc )
{
PlayerInputEventCallbackStruct callbackStruct
callbackStruct.cmdsReleasedBitMask = buttonEnum
callbackStruct.callbackFunc = callbackFunc
RemovePlayerInputEventCallback_Internal( player, callbackStruct )
}
void function RunHeldCallbackAfterTimePasses( entity player, PlayerHeldButtonEventCallbackStruct callbackStruct )
{
string endSignalName = GetEndSignalNameForHeldButtonCallback( callbackStruct )
player.EndSignal( endSignalName )
player.EndSignal( "OnDeath" )
/*OnThreadEnd(
function() : ( )
{
printt( "function ended at: " + Time() )
}
)
printt( "Pre wait time: " + Time() )*/
wait callbackStruct.timeHeld
//printt( "Post wait time: " + Time() )
if ( !IsValid( player ) )
return
callbackStruct.callbackFunc( player )
}
string function GetEndSignalNameForHeldButtonCallback( PlayerHeldButtonEventCallbackStruct callbackStruct )
{
return ( "Button" + callbackStruct.buttonHeld + "Released_EndSignal" )
}
bool function InputCallbackStructsAreTheSame( PlayerInputEventCallbackStruct callbackStruct1, PlayerInputEventCallbackStruct callbackStruct2 ) //Really just a comparison function because == does a compare by reference, not a compare by value
{
if ( callbackStruct1.cmdsPressedBitMask != callbackStruct2.cmdsPressedBitMask )
return false
if ( callbackStruct1.cmdsHeldBitMask != callbackStruct2.cmdsHeldBitMask )
return false
if ( callbackStruct1.cmdsReleasedBitMask != callbackStruct2.cmdsReleasedBitMask )
return false
if ( callbackStruct1.callbackFunc != callbackStruct2.callbackFunc )
return false
return true
}
bool function PlayerInputsMatchCallbackInputs( int cmdsHeld, int cmdsPressed, int cmdsReleased, PlayerInputEventCallbackStruct callbackStruct )
{
if ( !HasBitMask( cmdsHeld, callbackStruct.cmdsHeldBitMask ) )
return false
if ( !HasBitMask( cmdsPressed, callbackStruct.cmdsPressedBitMask ) )
return false
if ( !HasBitMask( cmdsReleased, callbackStruct.cmdsReleasedBitMask ) )
return false
return true
}
bool function HeldButtonCallbackStructsAreTheSame( PlayerHeldButtonEventCallbackStruct struct1, PlayerHeldButtonEventCallbackStruct struct2 ) //Really just a comparison function because == does a compare by reference, not a compare by value
{
if ( struct1.buttonHeld != struct2.buttonHeld )
return false
if ( struct1.callbackFunc != struct2.callbackFunc )
return false
if ( struct1.timeHeld != struct2.timeHeld )
return false
return true
}
void function TurnOffInputCallbacksIfNecessary( entity player )
{
if ( player.p.playerInputEventCallbacks.len() > 0 )
return
if ( player.p.playerHeldButtonEventCallbacks.len() > 0 )
return
if ( player.p.playerInputAxisEventCallbacks.len() > 0 )
return
//printt( "No more input callbacks, SetInputCallbacks to false" )
player.SetSendInputCallbacks( false )
}
PlayerInputAxisEventCallbackStruct function MakePressedForwardCallbackStruct()
{
PlayerInputAxisEventCallbackStruct callbackStruct
callbackStruct.horizAxisMinThreshold = -1.0
callbackStruct.horizAxisMaxThreshold = 1.0
callbackStruct.vertAxisMinThreshold = 0.4
callbackStruct.vertAxisMaxThreshold = 1.0
return callbackStruct
}
void function AddPlayerPressedForwardCallback( entity player, bool functionref( entity player ) callbackFunc, float debounceTime = 2.0 )
{
PlayerInputAxisEventCallbackStruct callbackStruct = MakePressedForwardCallbackStruct()
callbackStruct.debounceTime = debounceTime
callbackStruct.callbackFunc = callbackFunc
AddPlayerInputAxisEventCallback_Internal( player, callbackStruct )
}
void function RemovePlayerPressedForwardCallback( entity player, bool functionref( entity player ) callbackFunc, float debounceTime = 2.0 )
{
PlayerInputAxisEventCallbackStruct callbackStruct = MakePressedForwardCallbackStruct()
callbackStruct.debounceTime = debounceTime
callbackStruct.callbackFunc = callbackFunc
RemovePlayerInputAxisEventCallback_Internal( player, callbackStruct )
}
PlayerInputAxisEventCallbackStruct function MakePressedBackCallbackStruct()
{
PlayerInputAxisEventCallbackStruct callbackStruct
callbackStruct.horizAxisMinThreshold = -1.0
callbackStruct.horizAxisMaxThreshold = 1.0
callbackStruct.vertAxisMinThreshold = -1.0
callbackStruct.vertAxisMaxThreshold = -0.4
return callbackStruct
}
void function AddPlayerPressedBackCallback( entity player, bool functionref( entity player ) callbackFunc, float debounceTime = 2.0 )
{
PlayerInputAxisEventCallbackStruct callbackStruct = MakePressedBackCallbackStruct()
callbackStruct.debounceTime = debounceTime
callbackStruct.callbackFunc = callbackFunc
AddPlayerInputAxisEventCallback_Internal( player, callbackStruct )
}
void function RemovePlayerPressedBackCallback( entity player, bool functionref( entity player ) callbackFunc, float debounceTime = 2.0 )
{
PlayerInputAxisEventCallbackStruct callbackStruct = MakePressedBackCallbackStruct()
callbackStruct.debounceTime = debounceTime
callbackStruct.callbackFunc = callbackFunc
RemovePlayerInputAxisEventCallback_Internal( player, callbackStruct )
}
PlayerInputAxisEventCallbackStruct function MakePressedLeftCallbackStruct()
{
PlayerInputAxisEventCallbackStruct callbackStruct
callbackStruct.horizAxisMinThreshold = -1.0
callbackStruct.horizAxisMaxThreshold = -0.4
callbackStruct.vertAxisMinThreshold = -1.0
callbackStruct.vertAxisMaxThreshold = 1.0
return callbackStruct
}
void function AddPlayerPressedLeftCallback( entity player, bool functionref( entity player ) callbackFunc, float debounceTime = 2.0 )
{
PlayerInputAxisEventCallbackStruct callbackStruct = MakePressedLeftCallbackStruct()
callbackStruct.debounceTime = debounceTime
callbackStruct.callbackFunc = callbackFunc
AddPlayerInputAxisEventCallback_Internal( player, callbackStruct )
}
void function RemovePlayerPressedLeftCallback( entity player, bool functionref( entity player ) callbackFunc, float debounceTime = 2.0 )
{
PlayerInputAxisEventCallbackStruct callbackStruct = MakePressedLeftCallbackStruct()
callbackStruct.debounceTime = debounceTime
callbackStruct.callbackFunc = callbackFunc
RemovePlayerInputAxisEventCallback_Internal( player, callbackStruct )
}
PlayerInputAxisEventCallbackStruct function MakePressedRightCallbackStruct()
{
PlayerInputAxisEventCallbackStruct callbackStruct
callbackStruct.horizAxisMinThreshold = 0.4
callbackStruct.horizAxisMaxThreshold = 1.0
callbackStruct.vertAxisMinThreshold = -1.0
callbackStruct.vertAxisMaxThreshold = 1.0
return callbackStruct
}
void function AddPlayerPressedRightCallback( entity player, bool functionref( entity player ) callbackFunc, float debounceTime = 2.0 )
{
PlayerInputAxisEventCallbackStruct callbackStruct = MakePressedRightCallbackStruct()
callbackStruct.debounceTime = debounceTime
callbackStruct.callbackFunc = callbackFunc
AddPlayerInputAxisEventCallback_Internal( player, callbackStruct )
}
void function RemovePlayerPressedRightCallback( entity player, bool functionref( entity player ) callbackFunc, float debounceTime = 2.0 )
{
PlayerInputAxisEventCallbackStruct callbackStruct = MakePressedRightCallbackStruct()
callbackStruct.debounceTime = debounceTime
callbackStruct.callbackFunc = callbackFunc
RemovePlayerInputAxisEventCallback_Internal( player, callbackStruct )
}
void function AddPlayerInputAxisEventCallback_Internal( entity player, PlayerInputAxisEventCallbackStruct callbackStruct )
{
if ( !player.GetSendInputCallbacks() )
player.SetSendInputCallbacks( true )
Assert( IsValidPlayerInputAxisEventCallbackStruct( callbackStruct ) )
Assert( !InputAxisEventCallbackAlreadyExists( player, callbackStruct ), " Adding the same inputEventCallback " + string ( callbackStruct.callbackFunc ) + " with the same inputs!" )
player.p.playerInputAxisEventCallbacks.append( callbackStruct )
}
void function RemovePlayerInputAxisEventCallback_Internal( entity player, PlayerInputAxisEventCallbackStruct callbackStruct )
{
for( int i = player.p.playerInputAxisEventCallbacks.len() - 1; i >= 0; --i ) //Removing from the end of an array, so it's fine to remove as we go along
{
if ( InputAxisCallbackStructsAreTheSame( player.p.playerInputAxisEventCallbacks[i], callbackStruct ) )
{
player.p.playerInputAxisEventCallbacks.remove( i )
break //Can break since we shouldn't have more than one callbackstruct that's exactly the same
}
}
TurnOffInputCallbacksIfNecessary( player )
}
bool function InputAxisEventCallbackAlreadyExists( entity player, PlayerInputAxisEventCallbackStruct callbackStruct )
{
foreach( existingStruct in player.p.playerInputAxisEventCallbacks )
{
if ( InputAxisCallbackStructsAreTheSame( existingStruct, callbackStruct ) )
return true
}
return false
}
bool function InputAxisCallbackStructsAreTheSame( PlayerInputAxisEventCallbackStruct callbackStruct1, PlayerInputAxisEventCallbackStruct callbackStruct2 ) //Really just a comparison function because == does a compare by reference, not a compare by value
{
if ( callbackStruct1.horizAxisMinThreshold != callbackStruct2.horizAxisMinThreshold )
return false
if ( callbackStruct1.horizAxisMaxThreshold != callbackStruct2.horizAxisMaxThreshold )
return false
if ( callbackStruct1.vertAxisMinThreshold != callbackStruct2.vertAxisMinThreshold )
return false
if ( callbackStruct1.vertAxisMaxThreshold != callbackStruct2.vertAxisMaxThreshold )
return false
if ( callbackStruct1.debounceTime != callbackStruct2.debounceTime )
return false
if ( callbackStruct1.callbackFunc != callbackStruct2.callbackFunc )
return false
return true
}
bool function ShouldRunPlayerInputAxisCallbackFunc( float horizAxis, float vertAxis, PlayerInputAxisEventCallbackStruct callbackStruct )
{
if ( horizAxis < callbackStruct.horizAxisMinThreshold )
return false
if ( horizAxis > callbackStruct.horizAxisMaxThreshold )
return false
if ( vertAxis < callbackStruct.vertAxisMinThreshold )
return false
if ( vertAxis > callbackStruct.vertAxisMaxThreshold )
return false
if ( Time() < callbackStruct.lastTriggeredTime + callbackStruct.debounceTime )
return false
return true
}
bool function IsValidPlayerInputAxisEventCallbackStruct( PlayerInputAxisEventCallbackStruct callbackStruct )
{
//Make sure thresholds are within valid ranges
if ( callbackStruct.horizAxisMinThreshold < -1.0 )
return false
if ( callbackStruct.horizAxisMinThreshold > 1.0 )
return false
if ( callbackStruct.horizAxisMaxThreshold < -1.0 )
return false
if ( callbackStruct.horizAxisMaxThreshold > 1.0 )
return false
if ( callbackStruct.vertAxisMinThreshold < -1.0 )
return false
if ( callbackStruct.vertAxisMinThreshold > 1.0 )
return false
if ( callbackStruct.vertAxisMaxThreshold < 1.0 )
return false
if ( callbackStruct.vertAxisMaxThreshold > 1.0 )
return false
//Make sure min and maxes are correct relative to each other
if ( callbackStruct.horizAxisMinThreshold > callbackStruct.horizAxisMaxThreshold )
return false
if ( callbackStruct.vertAxisMinThreshold > callbackStruct.vertAxisMaxThreshold )
return false
return true
}
void function RunPlayerInputAxisCallbackFunc( entity player, PlayerInputAxisEventCallbackStruct callbackStruct )
{
bool callbackResult = callbackStruct.callbackFunc( player )
if ( callbackResult )
{
callbackStruct.lastTriggeredTime = Time()
if ( callbackStruct.debounceTime > 0 )
thread RunInputAxisCallbackAfterTimePasses( player, callbackStruct ) //Note that this has the potential to call RunPlayerInputAxisCallbackFunc again
}
}
void function RunInputAxisCallbackAfterTimePasses( entity player, PlayerInputAxisEventCallbackStruct callbackStruct )
{
player.EndSignal( "OnDeath" )
wait callbackStruct.debounceTime
WaitFrame() //Time to wait isn't exact due to floating point precision, so wait an extra frame.
if ( !IsValid( player ) )
return
float horizAxis = player.GetInputAxisRight()
float vertAxis = player.GetInputAxisForward()
if ( ShouldRunPlayerInputAxisCallbackFunc( horizAxis, vertAxis, callbackStruct ) )
RunPlayerInputAxisCallbackFunc( player, callbackStruct ) //Note that this has the potential to call RunInputAxisCallbackAfterTimePasses again
}
|