#Modular Trigger

1 messages · Page 1 of 1 (latest)

lean niche
#

Lets have this in a thread

rapid ferry
#

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

pallid sand
#

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

rapid ferry
#

Please do

lean niche
#

I think there are some issues / misunderstandings that need some thought about first

pallid sand
#

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 😄

rapid ferry
#

No.

pallid sand
#

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

rapid ferry
#

Okay.

lean niche
#

Modular Trigger

pallid sand
#

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

rapid ferry
#

I've only directly accessed them in (Visual scripting) code, so I guess, no.

warm coral
#

@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😅

pallid sand
#

No worries, just whipping up a tasty ms paint diagram

rapid ferry
#

lol

dense marten
#

I literally have the hatebin ready but I'm not sure if they deserve it.

rapid ferry
#

?

pallid sand
#

Just dumping code on them isnt always the best approach

lean niche
dense marten
lean niche
pallid sand
#

You could go away instead 😄

lean niche
#

baka

pallid sand
#

so lets start here, this is more or less what you have and you want to make ARadicalFunction tell ACoolFunction to run

rapid ferry
#

Yes.

pallid sand
#

specifically OnTrigger related stuff but since we want these to be generic that doesnt actually matter rn

mossy nova
#

@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

rapid ferry
lean niche
#

Is this what you want?

pallid sand
#

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"

mossy nova
lean niche
lean niche
#

I feel like this is exactly what they want

pallid sand
#

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

lean niche
#

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?

pallid sand
#

so for example the way I do the thing your trying to do is this

lean niche
#

@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

pallid sand
#

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 😄

rapid ferry
#

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.

sharp sky
rapid ferry
#

The receiver script is expected to have a Trigger method

#

If it doesn't I made a mistake

lean niche
sharp sky
#

Or at the very least undefined behaviour. That's why you'd generally avoid relying on strings for this kind of stuff.

lean niche
#

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

rapid ferry
#

Alright