/// <summary> /// Sets a timer to call the given native function at a set interval. If a timer is already set /// for this delegate, it will update the current timer to the new parameters and reset its /// elapsed time to 0. /// </summary> /// <param name="obj">Object to call the timer function on.</param> /// <param name="function">Method to call when timer fires (must be a UFunction method).</param> /// <param name="time">The amount of time between set and firing. If <= 0.f, clears existing timers.</param> /// <param name="looping">true to keep firing at Rate intervals, false to fire only once.</param> /// <param name="firstDelay">The time for the first iteration of a looping timer. If < 0.f inRate will be used.</param> /// <returns>The handle for the timer.</returns> public FTimerHandle SetTimer(UObject obj, FSimpleDelegate function, float time, bool looping = false, float firstDelay = -1.0f) { FTimerHandle handle = default(FTimerHandle); SetTimer(ref handle, obj, GetFunctionName(obj, function), time, looping, firstDelay); return(handle); }
public static void Init(IntPtr addTickerAddr, IntPtr isInGameThreadAddr, FSimpleDelegate onRuntimeChanged) { isInGameThread = (Del_IsInGameThread)Marshal.GetDelegateForFunctionPointer(isInGameThreadAddr, typeof(Del_IsInGameThread)); Debug.Assert(IsInGameThread(), "USharp should only be loaded from the game thread"); addStaticTicker = (Del_AddStaticTicker)Marshal.GetDelegateForFunctionPointer(addTickerAddr, typeof(Del_AddStaticTicker)); ticker = Tick; addStaticTicker(ticker, 0.0f); OnRuntimeChanged = onRuntimeChanged; }
private static void Run(FSimpleDelegate func, EAsyncThreadType threadType, bool waitForComplete) { switch (threadType) { case EAsyncThreadType.GameThreadUnloadIgnore: case EAsyncThreadType.GameThread: if (IsInGameThread()) { func(); return; } break; case EAsyncThreadType.RHIThread: if (IsInRHIThread()) { func(); return; } break; case EAsyncThreadType.RenderingThread: if (IsInRenderingThread()) { func(); return; } break; } if (HotReload.IsUnloading) { return; } AsyncCallback callback = new AsyncCallback(func, threadType); lock (callbacks) { callbacks.Add(callback); } Native_FAsync.AsyncTask(callback.Run, threadType); if (waitForComplete) { callback.WaitForComplete(); } else { callback.Dispose(); } }
private static FName GetFunctionName(UObject obj, FSimpleDelegate function) { Delegate del = function as Delegate; if (del != null) { UObject target = del.Target as UObject; if (target != null)// Also check against obj? { IntPtr functionAddress = NativeReflection.LookupTable.FindFunction(target, del.Method); FName name; Native_UObjectBase.GetFName(functionAddress, out name); return(name); } } return(FName.None); }
public static void Run(FSimpleDelegate callback) { if (IsInGameThread()) { callback(); } else { using (AutoResetEvent waitHandle = new AutoResetEvent(false)) { asyncTask(delegate { callback(); waitHandle.Set(); }, 0); waitHandle.WaitOne(Timeout.Infinite); } } }
public static void Run(FSimpleDelegate callback) { if (IsInGameThread()) { callback(); } else { CallbackInfo callbackInfo = new CallbackInfo() { Callback = callback, WaitHandle = new AutoResetEvent(false) }; lock (callbacks) { callbacks.Enqueue(callbackInfo); } callbackInfo.WaitHandle.WaitOne(Timeout.Infinite); callbackInfo.WaitHandle.Close(); } }
/// <summary> /// Runs a function on the game thread synchronously /// </summary> public static void RunOnGameThread(FSimpleDelegate func) { Run(func, EAsyncThreadType.GameThread, true); }
public AsyncCallback(FSimpleDelegate callback, EAsyncThreadType threadType) { this.callback = callback; ThreadType = threadType; }
internal static void RunUnloader(FSimpleDelegate func) { Run(func, EAsyncThreadType.GameThreadUnloadIgnore, true); }
/// <summary> /// Runs a function on any thread asynchronously (no real reason to use this - just use regular C# threading) /// </summary> internal static void RunOnAnyThreadAsync(FSimpleDelegate func) { Run(func, EAsyncThreadType.AnyThread, false); }
/// <summary> /// Runs a function on the rendering thread asynchronously /// </summary> public static void RunOnRenderingThreadAsync(FSimpleDelegate func) { Run(func, EAsyncThreadType.RenderingThread, false); }
public FTimerHandle FindTimerHandle(UObject obj, FSimpleDelegate function) { return(FindTimerHandle(obj, GetFunctionName(obj, function))); }
/// <summary> /// Sets a timer to call the given native function on the next tick /// (this could be called RunFunctionOnNextTick as it isn't really timer related (aside from being done by FTimerManager)). /// </summary> /// <param name="obj">Object to call the timer function on.</param> /// <param name="function">Method to call when timer fires.</param> public void SetTimerForNextTick(UObject obj, FSimpleDelegate function) { SetTimerForNextTick(obj, GetFunctionName(obj, function)); }
/// <summary> /// Sets a timer to call the given native function at a set interval. If a timer is already set /// for this delegate, it will update the current timer to the new parameters and reset its /// elapsed time to 0. /// </summary> /// <param name="inOutHandle">Handle to identify this timer. If it is invalid when passed in it will be made into a valid handle.</param> /// <param name="obj">Object to call the timer function on.</param> /// <param name="function">Method to call when timer fires (must be a UFunction method).</param> /// <param name="time">The amount of time between set and firing. If <= 0.f, clears existing timers.</param> /// <param name="looping">true to keep firing at Rate intervals, false to fire only once.</param> /// <param name="firstDelay">The time for the first iteration of a looping timer. If < 0.f inRate will be used.</param> public void SetTimer(ref FTimerHandle inOutHandle, UObject obj, FSimpleDelegate function, float time, bool looping = false, float firstDelay = -1.0f) { SetTimer(ref inOutHandle, obj, GetFunctionName(obj, function), time, looping, firstDelay); }