-
Notifications
You must be signed in to change notification settings - Fork 57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding TempVisualEffectAPI #313
Conversation
Allows for adding custom TemporaryVisualEffects (TVEs) to every CharacterBody spawned that functions the same as vanilla TVEs. Use `TempVisualEffectAPI.AddTemporaryVisualEffect()` before `WhenContentPackLoaded` to add a custom TVE to a static List and Dictionary. Custom TVE list is copied into `R2APITVEController` components which are added on `CharacterBody.onBodyStartGlobal`. Hooks onto CharacterBody's `UpdateAllTemporaryVisualEffects()` and `UpdateSingleTemporaryVisualEffect` to update custom TVEs just like vanilla ones. Compares the latter method's `resourceString` input with the dictionary's string key in order to instantiate the correct TVE GameObject. `AddTemporaryVisualEffect()` requires a CharacterBody input, bool output delegate parameter. This delegate is used for the 'active' of the TVE - if it returns true, the TVE will be created/activated for the instance.
R2API/TempVisualEffectAPI.cs
Outdated
if (controller) { | ||
for (int i = 0; i < controller.localTVEs.Count; i++) { | ||
TempVisualEffectAPI.TemporaryVisualEffectStruct temp = controller.localTVEs[i]; | ||
self.UpdateSingleTemporaryVisualEffect(ref temp.effect, moddedString + temp.effectPrefab.name, temp.useBestFitRadius ? self.radius : self.bestFitRadius, temp.condition.Invoke(self), temp.childLocatorOverride); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use effectName not .name so that garbage isnt generated each frame
R2API/TempVisualEffectAPI.cs
Outdated
private static void UpdateSingleHook(ILContext il) { | ||
var cursor = new ILCursor(il); | ||
|
||
GameObject GetCustomTVE(GameObject vanillaLoaded, string ResourceString) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
resourceString not ResourceString
R2API/TempVisualEffectAPI.cs
Outdated
var newTVE = new TemporaryVisualEffectStruct(); | ||
|
||
if (effectPrefab == null) { | ||
R2API.Logger.LogError($"Tried to add TVE: {effectPrefab.name} GameObject is null"); return false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.name will fail here since ref is null
R2API/TempVisualEffectAPI.cs
Outdated
newTVE.effectName = effectPrefab.name; | ||
|
||
tves.Add(newTVE); | ||
tveDict.Add(moddedString + effectPrefab.name, newTVE); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
effectName not .name
R2API/TempVisualEffectAPI.cs
Outdated
|
||
internal static List<TemporaryVisualEffectStruct> tves = new List<TemporaryVisualEffectStruct>(); | ||
internal static Dictionary<string, TemporaryVisualEffectStruct> tveDict = new Dictionary<string, TemporaryVisualEffectStruct>(); | ||
internal static readonly string moddedString = "R2APIModded:"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
internal const string
Fixed based on the Pull Request review from `xiaoxiao921`. Thank you for the review and suggesting changes! I've implemented the suggested changes and some extras: - Reorganised the checks, such as checking if the effectPrefab is null first. - `GameObject == null` now throws NRE instead of returns false. - Removed `effect.effectPrefab == effectPrefab` check. Matching GameObjects can be added, as long as the name is different (For using the same effect but in a different radius, condition, or child locator). - Changed 'Tried' to 'Failed'. More accurate.
Updated `useBestFitRadius` to default to false, making `effectPrefab` and `condition` the only required parameters.
Whoops, had the API named wrong.
Allows for adding custom TemporaryVisualEffects (TVEs) to every CharacterBody spawned that functions the same as vanilla TVEs.
Use
TempVisualEffectAPI.AddTemporaryVisualEffect()
beforeWhenContentPackLoaded
(usually in mod'sAwake()
) to add a custom TVE to a static List and Dictionary. Custom TVE list is copied intoR2APITVEController
components which are added onCharacterBody.onBodyStartGlobal
. Hooks onto CharacterBody'sUpdateAllTemporaryVisualEffects()
andUpdateSingleTemporaryVisualEffect()
to update custom TVEs just like vanilla ones. Compares the latter method'sresourceString
input with the dictionary's string key in order to instantiate the correct TVE GameObject.AddTemporaryVisualEffect()
requires a CharacterBody input, bool output delegate parameter. This delegate is used for the 'active' of the TVE - if it returns true, the TVE will be created/activated for the instance.Example use:
