#Modular Trigger
1 messages · Page 1 of 1 (latest)
Alright-
The goal: Modular script which calls the function "Trigger" on a specified script when player collides with a trigger. (Layer 17 handles masking separate from scripts)
The code as of this message: https://paste.ofcode.org/tENYPa4bir2PRR8L6cNF2
Right, I did this recently myself as a fairly newer programmer, I'm happy to walk through how I approched it if you would like
Please do
I think there are some issues / misunderstandings that need some thought about first
No absolutely I'm gonna run it back from the start
@rapid ferry Have you used Events / UnityEvents before?
You've probably answered this before but I wanted to start from the top again to make sure I didn't miss anything 😄
No.
Alright, I'm gonna show you what they are. They're super easy to understand and once you know how they work you'll love em, I promise
Okay.
Modular Trigger
Just incase, Have you messed with much UI in unity before? Like how you can have buttons that run functions and all that jazz
All good if you haven't just might be easier to explain if you have
I've only directly accessed them in (Visual scripting) code, so I guess, no.
@rapid ferry I just saw the launcher in #archived-game-design. Is that what you are trying to code? You probably should’ve sent that since it would’ve helped a bit with all the confusion😅
Nah
No worries, just whipping up a tasty ms paint diagram
lol
I literally have the hatebin ready but I'm not sure if they deserve it.
?
Just dumping code on them isnt always the best approach
I was about to write 3 scripts
||it makes them go away though||
Don't be mean
You could go away instead 😄
baka
so lets start here, this is more or less what you have and you want to make ARadicalFunction tell ACoolFunction to run
Yes.
specifically OnTrigger related stuff but since we want these to be generic that doesnt actually matter rn
@rapid ferry try flipping it around, consider the trigger on SecondScript instead and it just checks if FirstScript is a valid thing that can trigger it
That's the opposite of what I'm trying to do.
This is exactly what Events are made to do (Other programmers don't roast this explanation for not being 100% accurate tyty)
Events are kind of like an Array or List of Functions, Except in code you can tell the Event to run every function that it has stored! This gives us the ability to be super generic with our code because we don't have to tell anything specific to run, we can basicially just say "hey if anyone is chilling in our Event you should run"
Correct, and it will likely make your life a lot easier
https://paste.ofcode.org/fxxPSc2t3z6DWfG8GyXBEn
Actually this might be better
Makes sense.
I feel like this is exactly what they want
an IRL equivalent would be instead of asking someone in your house if they want any dinner cuz your ordering, you can instead just say "Hey if anyone wants any food let me know because im ordering" because then you dont have to care about specifying anyone
Now just to clarify, Events themselves are a C# thing but UnityEvents are like an extended version of them with extra features, and this is where the fun happens
When you make a public UnityEvent, it shows up in your Inspector like this. This lets you drag in any script or gameobject you want, and specify a function you'd want to call if this UnityEvent gets told to run
Flow I thought you just wanted anything with the correct script to run if it enters the triggers?
So say you have "A = Player"
B = AScript
C = AnotherScript
if B or C goes into the player A... It runs the .Trigger() function in them?
so for example the way I do the thing your trying to do is this
@rapid ferry So do you need what they are doing or what I explained? Because I can explain the code I posted if that is what you need.
Otherwise I can leave it to batby
We can add SecondScript's OnTriggerEnter() to our FirstScript's UnityEvent, So when our FirstScript's OnTriggerEnter() is called, it tells our UnityEvent to run anything it has
FirstScript() doesn't really care what is in it's event, its just telling anything that happens to be inside it that its doing something
Here's my specific implementation of it (its not perfect but whatevs)
[System.Serializable]
public class UnityEventCollider : UnityEvent<Collider>
{
}
public class TriggerForwarder : MonoBehaviour
{
public UnityEventCollider onTriggerEnter;
public UnityEventCollider onTriggerStay;
public UnityEventCollider onTriggerExit;
public bool onTriggerEnterEnabled;
public bool onTriggerStayEnabled;
public bool onTriggerExitEnabled;
private void Start()
{
if (onTriggerEnter.GetPersistentEventCount() > 0)
onTriggerEnterEnabled = true;
if (onTriggerStay.GetPersistentEventCount() > 0)
onTriggerStayEnabled = true;
if (onTriggerExit.GetPersistentEventCount() > 0)
onTriggerExitEnabled = true;
}
void OnTriggerEnter(Collider other)
{
if (onTriggerEnterEnabled)
onTriggerEnter.Invoke(other);
}
void OnTriggerStay(Collider other)
{
if (onTriggerStayEnabled)
onTriggerStay.Invoke(other);
}
void OnTriggerExit(Collider other)
{
if (onTriggerExitEnabled)
onTriggerExit.Invoke(other);
}
}
What this does is forwards trigger calls to any object that we add to the UnityEvents. Theres two added features though that just lets us disable any trigger types we don't want to call (so we don't waste the triggers time trying to say anything) and on Start() we check if anyone is even listening, because no point wasting the triggers time if their isn't
We have to make an extended version of the UnityEvent at the top to use so it can send our Collider information aswell like you would expect
let me know if I need to explain anything 😄
Figured it out.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Trigger : MonoBehaviour
{
public GameObject TargetObj = null;
public string ScriptName = " ";
private void Start()
{
gameObject.layer = 17;
}
// Calls "Function" on trigger enter
void OnTriggerEnter()
{
Component TargetScript = TargetObj.GetComponent(ScriptName);
if (TargetScript != null)
{
TargetScript.SendMessage("Trigger");
}
}
}
It was simple.
Everyoone was like DoNt UsE SendMessage but it works perfectly fine.
Does it not throw an error for scripts that don't have the Trigger method?
The receiver script is expected to have a Trigger method
If it doesn't I made a mistake
It opperates a bit weirdly... It's basically an odd trigger chain where they set the next thing to be triggered
Well, imagine that it somehow sneaked into your production code. You'll probably have a crash at that point.
Or at the very least undefined behaviour. That's why you'd generally avoid relying on strings for this kind of stuff.
Btw that's one of the reasons people suggested against it Flow... It's not that it doesn't work but that it's not safe
If it works for now, it's fine but we weren't trying to just be difficult
As long as you're learning
Alright