✅ Acces delegate type from another script
Im using unity with firebase realtime database. I need my static ReadFromDatabase function to be able to Callback on the process (from another script) that called it. Would really appreciate your help, but would understand if you cant help me.
19 Replies
Do you just want to access the delegate type itself or an instance event?
If you want to access the callback/event then you'll need a reference to the instance, probably by adding it from the editor
Well i basically got a static script called FirebaseManager with a static funtion ReadFromDatabase. Since that function needs to wait for the data to arive, i need it to callback with the data to the function that called it. Therefore, i need to give the function to call later (when the data is received) as an argument to said ReadFromDatabase function. I having issues doing that. Could you tell me how im supposed to do that?
If you need more explaining, we could talk in a call.
can you show your ReadFromDatabase code?
so can you not just pass the function as a parameter when you call ReadFromDatabase? how is the method being called?
Well that is what im trying to do. I am just not very good at this and cant seem to make it work. The function is beeing called from another script that is not static. Both relegate types are identical but arent defined in the same script, so it just gives an error
Go to go. It's getting late. Thank you for trying to help me
You can create a function wherever ReadFromDatabase is being called and pass it is as the readCallback parameter. I don't have your full code, but something like:
or you could an Action/Func instead of declaring a delegate since they are delegates themselves:
I made a little diagram of what i am trying to do. Could you tell me, which code sshould be put in which script?
You call FirebaseManager with the random function you have in another script for the callback
How are you trying to call it currently?
i have this delegate type:
public delegate void ReadCallback(object data);
ReadCallback readCallback = LoadInventory;
FirebaseManager.ReadFromDatabase("/data/", readCallback);
i would be ok with going into voice chat and talking about this vocaly so its easier for the both of us
Does that give you an error?
I probably won't be in any one place long enough today to voice chat sorry
You can tell me when you'll have time. (I live at GMT+1 btw)
also, here is the error:
cannot convert from 'InventoryManagerScript.ReadCallback' to 'FirebaseManager.ReadCallback'
oh you are declaring it as the wrong type
You have two delegates with the same type name in different namespaces and you imported the wrong one
and really you don't even have to assign it to a variable, you should just be able to say
FirebaseManager.ReadFromDatabase("/data/", LoadInventory)
Thank you so much, i kinda wasted your time there... I works like a charm now. the ticket close automatically or do i need to do something?
no problem
$close
Use the /close command to mark a forum thread as answered
and totally a preference thing, but again I prefer Action/Func as they are equivalent to delegates but imo more readable
ok, thanks again!
/close