Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Help with Events and args

Discussion in 'Scripting' started by KrazyWulf1983, Mar 18, 2021.

  1. KrazyWulf1983

    KrazyWulf1983

    Joined:
    May 5, 2014
    Posts:
    12
    so im trying to trigger an event with args..

    trigger event
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class ItemDropped: MonoBehaviour
    4. {
    5.     private void OnTriggerEnter2D(Collider2D collision)
    6.      
    7.     {
    8.         collision.GetComponent<Item> = arg1;
    9.         collision.GetComponent<Amount> = arg2;
    10.         GameEvents.current.OnItemDroppedEvent(arg1,arg2);
    11.     }
    12.  
    13. }
    14.  
    15.  
    subvscriber
    Code (CSharp):
    1. public void awake()
    2. {
    3.         GameEvents.current.OnItemDroppedEvent += Current_OnItemDroppedEvent;
    4. }
    5.     private void Current_OnItemDroppedEvent(Item arg1, int arg2)
    6.     {
    7.         inventory.AddItemAmount(arg1, arg2);
    8.     }
    event handler
    Code (CSharp):
    1. using System;
    2.     using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class GameEvents : MonoBehaviour
    7. {
    8.  
    9.     public static GameEvents current;
    10.     private void Awake()
    11.     {
    12.         current = this;
    13.     }
    14. public event Action<Item,int> OnItemDroppedEvent;
    15.     public void OnItemDropMethod(Item _item, int _amount)
    16.     {
    17.         if (OnItemDroppedEvent != null)
    18.         {
    19.             OnItemDroppedEvent(_item,_amount);
    20.         }
    21.     }
     
  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,051
    Please always post the full error you get and/or clearly describe what doesn't work they way you expect it to.

    The event's second argument is
    int
    but you try to pass it a script (
    Amount
    ) when trying to trigger the event. You also try to trigger the event directly, which is not allowed, you need to call
    GameEvents.current.OnItemDropMethod(arg1,arg2);
    instead.
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    I'll try to repeat Adrian in clearer terms.

    Here:
    Code (csharp):
    1.  
    2.         collision.GetComponent<Item> = arg1;
    3.         collision.GetComponent<Amount> = arg2;
    4.         GameEvents.current.OnItemDroppedEvent(arg1,arg2);
    5.  
    arg2 is typed as 'Amount'... see how you say 'GetComponent<Amount>'? Yeah, that means arg2 is of type 'Amount'. Not of type 'int'.

    See here:
    Code (csharp):
    1.  
    2.     public event Action<Item,int> OnItemDroppedEvent;
    3.     public void OnItemDropMethod(Item _item, int _amount)
    4.     {
    5.  
    Both of these expect 'int' as the second argument. NOT 'Amount'. See <Item,int> and (Item _item, int _amount)

    Lastly... you say:
    Code (csharp):
    1. GameEvents.current.OnItemDroppedEvent(arg1,arg2);
    That points to this:
    Code (csharp):
    1. public event Action<Item,int> OnItemDroppedEvent;
    Thing is... the 'event' modifier in 'public event Action...' tells the compiler that the only thing that can access it is the declaring type... which is GameEvents. This is why you get an error saying:
    Note how I gave the actual error and not some vague rendition of it like:
    something about? really?

    This is why wherever you got this code from (may you have copy pasted it, or based it on existing code, or whatever... I'm assuming you based this on someone else's code since you don't know how to use it)... wherever you got this code from they added this method:
    Code (csharp):
    1.  
    2.     public void OnItemDropMethod(Item _item, int _amount)
    3.     {
    4.         if (OnItemDroppedEvent != null)
    5.         {
    6.             OnItemDroppedEvent(_item,_amount);
    7.         }
    8.     }
    9.  
    Note how all this method does is call the event on your behalf from within the type GameEvents... because only GameEvents can call its own events.

    So you should be saying:
    Code (csharp):
    1. GameEvents.current.OnItemDroppedMethod(arg1,arg2.SomeVariableOnAmountThatIsOfTypeInt);
    Noting that I have NO IDEA what 'int' you actually want to pass along because we don't know your code. But I have to guess that the type 'Amount' must have some variable on it that is of type int that you had intended to pass along.

    I apologize if I sound dismissive, but your original post had little to no information describing your problem which would help to facilitate us helping you. And your follow up message criticizes the person who attempted to help you by telling them:
    And yeah.. you DO try to trigger it directly. That's the problem. Adrian was telling you that you should not call it directly. Because that's what the event modifier blocks you from doing.

    To repeat my signature:
    Because may anyone think what I have to say here is rude... I think it's rude of OP to not include information that helps us help them. And instead expecting us to read all of their code and do the leg work to figure out the problem which the compiler already did on their behalf and all they had to do was literally copy-paste it... or you know... READ IT. And then following that lack of giving us information by telling us WE should READ the code. And if that 1983 is indicative of their birth year, then they're the same age as me, a grown adult!
     
    Last edited: Mar 18, 2021
    mopthrow likes this.
  4. KrazyWulf1983

    KrazyWulf1983

    Joined:
    May 5, 2014
    Posts:
    12
    thank you and i appolagies for my sillyness. im new to coding.. i learned from youtube videos cretaed the code myself an yes i barly understand it as is.. guess i shoul watch and read more im not sure spent the last week trying to figure this out.. your rioght on every account.. i also agree i typed the code for amount wrong.. probably should have beenb a get component <itemdropped>.amount
     
    lordofduct likes this.
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    well, I'm happy you resolved the issue
     
  6. KrazyWulf1983

    KrazyWulf1983

    Joined:
    May 5, 2014
    Posts:
    12
    so i went back and did a rethink like it was suggested i do and relized i was doing it all wrong i fixed my issue by putting the itempickup code on my playercontroller which has the inventory linked to it then runing the get components on the script attached to the item to pickup getting the 2 variables i needed and runing them into the inventory.. i been trying to do it all wrong this whole time