PDA

View Full Version : Does onInit for global scripts get called on the client?



darrenan
February 15th, 2022, 16:54
Title pretty much says it all. I have a global LUA script defined in base.xml and it looks as though its onInit() function is only getting called on the host side, and not on the client, even though I know for a fact that other functions in that script are executing on the client side. Is the logic for when onInit() is called different for clients?

Moon Wizard
February 15th, 2022, 21:35
Yes, the onInit is fired on the client as well as the host.

One big difference to note between client and host is that any public database nodes will not be available on the client during the onInit process, since the database is streamed in after the tabletop initializes. So, if you are depending on database access, you'll probably need to refactor your client-specific code to wait for the database node to be created.

Regards,
JPG

SilentRuin
February 15th, 2022, 21:51
Title pretty much says it all. I have a global LUA script defined in base.xml and it looks as though its onInit() function is only getting called on the host side, and not on the client, even though I know for a fact that other functions in that script are executing on the client side. Is the logic for when onInit() is called different for clients?

If you want the DB access for client init you do the following:



function onInit()
Interface.onDesktopInit = onDesktopInit;
... your stuff...
end
function onDesktopInit()
... your client DB stuff...
end

darrenan
February 15th, 2022, 22:41
Thanks, that helps. I'm assuming that the Interface.onDesktopInit assignment line can be wrapped in a "not Session.IsHost" check, since the host should be able to just directly call onDesktopInit and do that stuff there? Or is it better for some reason to just do that unconditionally?

Moon Wizard
February 15th, 2022, 23:55
If it needs to run in both, I usually just call it in the same way that works for both, since that is simpler code wise.

If there is an script initialization call ordering consideration, then you might break it up, but that is rarely needed. (Since there is no predefined order that scripts get called for onInit and onDesktopInit.)

Regards,
JPG