PDA

View Full Version : Has User.onIdentityActivation Been Removed/Changed?



UrsaTeddy
October 14th, 2022, 11:38
Greetings,

I have been overriding the User.onIdentityActivation function successfully, however after the last update, it is failing.

To test to see if something was going wrong, I simply tried to print out (Debug.chat) the User.onIdentityActivation function, without overriding and it is returning 'nil'.

So has the name of this function changed? I am going by https://fantasygroundsunity.atlassian.net/wiki/spaces/FGCP/pages/996644683/User#onIdentityActivation

Has anything changed?

Moon Wizard
October 14th, 2022, 17:03
Several global scripts in CoreRPG use the User.onIdentityActivation handler registration. You can not use Debug to get the value, because you are actually using that call to register a handler function. It's not a function definition. This handles things like adding the character portraits on the top left, which is still working for me.

You'll need to put Debug statements in your override function to see what is happening when it is called.

Regards,
JPG

UrsaTeddy
October 14th, 2022, 23:02
Okay ... I have debugged within my override as well ...

It simply does not work anymore ... I perform the following code to override ...



function OverrideOnIdentityActivation( )
Debug.chat("Original Function", User.onIdentityActivation)

self.PreviousOnIdentityActivation = User.onIdentityActivation
User.onIdentityActivation = self.OnIdentityActivation

Debug.chat("Overriding Activation", self.PreviousOnIdentityActivation, User.onIdentityActivation)
end

function OnIdentityActivation( identity , user , activated )
Debug.chat("We have an Activation",identity,user,activated)
Debug.chat("Previous is",self.OnIdentityActivation, self.PreviousOnIdentityActivation)
self.PreviousOnIdentityActivation( identity , user , activated )
end


This overrides the function, however it does not store the previous one.

As you can see I am attempting to store the previous version as good programming practice, and in addition I call the previous version (or attempt to) to keep any operations within it active since in the ruleset I am using (Savage Worlds) it overrides onIdentityActivation in four different locations in templates - not sure how that works.

How do I store/call the previous function?

Moon Wizard
October 15th, 2022, 00:03
You can't override a previous onIdentityActivation handler. Handlers may look like a simple assignment, but they are not. They are a way to register a function for an event; not setting/getting a function value.

It was never able to be referenced like that.

Regards,
JPG

UrsaTeddy
October 15th, 2022, 00:16
Okay ... so then how do I store the previous function ... or is this not possible?

This is something that is important if I just want to "add on" some functionality without disturbing previous onIdentityActivation code from, for example, another ruleset or another extension.

Moon Wizard
October 15th, 2022, 00:24
You would have to change the function in that global script before it assigns itself to the handler, if the script is written to allow it.

If the script assigns the event in the onInit, then you can't; because you have no guarantee of initialization order.
If the script assigns the event in a separate handler tied to onDesktopInit (or in an onTabletopInit function in v4.3), then you can override the function in a separate onInit global script.

For example, in the CoreRPG CharacterListManager and LanguageManager, the handler registrations are done in onDesktopInit, which means you could theoretically override those functions before they are registered.


function onInit()
PreviousOnIdentityActivation = CharacterListManager.onIdentityActivation;
CharacterListManager.onIdentityActivation = OnIdentityActivation;
end
function OnIdentityActivation(sIdentity, sUser, bActivated)
PreviousOnIdentityActivation(sIdentity, sUser, bActivated);
<Your New Code>
end


Regards,
JPG

UrsaTeddy
October 15th, 2022, 00:37
Okay, so I am obviously missing something.

You have overridden the function in multiple locations, so how would I ensure that all of those functions are executed as they are expected to be?

For example your CharacterList manager does not call a previous version of onIdentityActivation, so if I override the standard version with User.onIdentityActivation = MyFunction, it will not interfere with your CharacterList manager?

Moon Wizard
October 15th, 2022, 01:06
A handler is not the same as a function assignment. It may look similar, but under the hood, it is handled completely differently. These events can not be accessed to retrieve them. You have to capture in advance as in my example post above.

In fact, this particular part of the API is confusing enough that I've been working on replacement APIs for v4.3 to make this more obvious that you're registering a handler for an event.
(Ex: User.addEventHandler("onIdentityActivation", DiceSkinManager.onUserIdentityActivate);)

Regards,
JPG

UrsaTeddy
October 15th, 2022, 01:08
Okay so the addEventHandler function will queue up my onUserIdentityActivation for execution? Is that what you are stating?

Moon Wizard
October 15th, 2022, 01:10
Yes.

If you just need to be notified of the event and trigger independent actions, then just register your own event handler. (i.e. CoreRPG does this with LanguageManager and CharacterListManager)
If you need to specifically override another function that's being used as an event handler to change the behavior, then all the previous stuff we talked about applies.

Regards,
JPG

UrsaTeddy
October 15th, 2022, 01:12
Excellent ... I will give that a go and see if it handles what I want.

Once again, thank you for your guidance ... documentation would be updated when you release the new API versions?

Moon Wizard
October 15th, 2022, 01:14
It will be updated after the release over time, as I have time. :)

Regards,
JPG

UrsaTeddy
October 15th, 2022, 01:21
You need a co-coder who looks over your shoulder writing the documentation as you write the code :)

UrsaTeddy
October 15th, 2022, 08:25
Sorry to bother again ... however addEventHandler is throwing the error: attempt to call field 'addEventHandler' a nil field.

Moon Wizard
October 15th, 2022, 18:30
This only works in v4.3 which is in the beta Test channel.

If you want to get it working in v4.2.x right now, you'll have to follow the original code I gave you:
https://www.fantasygrounds.com/forums/showthread.php?75472-Has-User-onIdentityActivation-Been-Removed-Changed&p=665271&viewfull=1#post665271

Regards,
JPG

UrsaTeddy
October 16th, 2022, 00:59
Yes, I assumed it was the Test server, however it was not working on my Mac yesterday ... turns out that FGU requires a reboot of the machine when the server is changed.

So I joined the Test channel, updated, then ran the code and it finds the function now ... great ... however now it is claiming that

attempt to call field 'getDatabaseNode' is a nil value now

Has the DB code changed as well?

Is there a resource I can use where you list things that you are testing and changing in a quick summary way?

Moon Wizard
October 16th, 2022, 02:06
The changes are all in the beta test channel notes in the Laboratory forum.

However, nothing has changed for getDatabaseNode function. So, I'd have to see your code to give any input on what might be happening.

Regards,
JPG

UrsaTeddy
October 16th, 2022, 02:11
Unfortunately the error comes from another Extension by somebody else (WindowSaverX2).

I have simply disabled it and have been happy testing the addEventHandler.

The funny thing is that sometime ago when I was able to store/chain overrides I had essentially written my own Event Handler which can chain callbacks based on event type (currently based on connections/disconnections).

addEventHandler has simplified the code since I do not have to check the previous version for existence and then execute it in the order a user can set for when its execution occurs.