C
C#16mo ago
Stein0705

✅ 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
Thinker
Thinker16mo ago
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
Stein0705
Stein0705OP16mo ago
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.
dan in a can
dan in a can16mo ago
can you show your ReadFromDatabase code?
Stein0705
Stein0705OP16mo ago
dan in a can
dan in a can16mo ago
so can you not just pass the function as a parameter when you call ReadFromDatabase? how is the method being called?
Stein0705
Stein0705OP16mo ago
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
dan in a can
dan in a can16mo ago
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:
FirebaseManager.ReadFromDatabase("blah", SatisfiesCallback);

private void SatisfiesCallback(int value)
{

}
FirebaseManager.ReadFromDatabase("blah", SatisfiesCallback);

private void SatisfiesCallback(int value)
{

}
or you could an Action/Func instead of declaring a delegate since they are delegates themselves:
// Signature would look like this then; I'm assumine snapshot.Value is an int here. Actions return void and Functions return something non-void
public static void ReadFromDatabase(string path, Action<int> callback)

// You can call it the same way as the previous example, but I thought I'd note the option of an anonymous function, too
FirebaseManager.ReadFromDatabase("blah", (int value) =>
{
// Do some stuff in an anonymous function here
});
// Signature would look like this then; I'm assumine snapshot.Value is an int here. Actions return void and Functions return something non-void
public static void ReadFromDatabase(string path, Action<int> callback)

// You can call it the same way as the previous example, but I thought I'd note the option of an anonymous function, too
FirebaseManager.ReadFromDatabase("blah", (int value) =>
{
// Do some stuff in an anonymous function here
});
Stein0705
Stein0705OP16mo ago
I made a little diagram of what i am trying to do. Could you tell me, which code sshould be put in which script?
dan in a can
dan in a can16mo ago
You call FirebaseManager with the random function you have in another script for the callback How are you trying to call it currently?
Stein0705
Stein0705OP16mo ago
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
dan in a can
dan in a can16mo ago
Does that give you an error? I probably won't be in any one place long enough today to voice chat sorry
Stein0705
Stein0705OP16mo ago
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'
dan in a can
dan in a can16mo ago
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)
Stein0705
Stein0705OP16mo ago
Thank you so much, i kinda wasted your time there... I works like a charm now. peepoNoted the ticket close automatically or do i need to do something?
dan in a can
dan in a can16mo ago
no problem
SinFluxx
SinFluxx16mo ago
$close
MODiX
MODiX16mo ago
Use the /close command to mark a forum thread as answered
dan in a can
dan in a can16mo ago
and totally a preference thing, but again I prefer Action/Func as they are equivalent to delegates but imo more readable
Stein0705
Stein0705OP16mo ago
ok, thanks again! /close
Want results from more Discord servers?
Add your server