Are events global or specific to each player?

For AI and campaign script related discussions and questions
Post Reply
MIH-XTC
Trained
Trained
Posts: 368
Joined: 31 Jan 2014, 07:06

Are events global or specific to each player?

Post by MIH-XTC »

Are WZ events specific to each player when they are called or are they global?

For example, if we have AI1.js, AI2.js, AI3.js, etc... and all of the AI's have an eventDroidBuilt() call, is that code specific to the AI that called it or is it global for all players? I'm thinking in campaign there is no concept of having multiple AI1.js, AI2.js etc... as all the code belongs to the same interpreter. However, in multiplayer, there are multiple AI instances and no checks need to be done. Is this correct?

A similar question is, how is it possible for cam_eventDroidBuilt() to only apply to the AI players and not humans if no checks are done on who the droid belongs to?

I basically want to know, do I need to check the player number of who owns structures/droids in all the event logic?
User avatar
Berserk Cyborg
Code contributor
Code contributor
Posts: 938
Joined: 26 Sep 2016, 19:56

Re: Are events global or specific to each player?

Post by Berserk Cyborg »

MIH-XTC wrote: 29 Jul 2019, 16:59 Are WZ events specific to each player when they are called or are they global?

For example, if we have AI1.js, AI2.js, AI3.js, etc... and all of the AI's have an eventDroidBuilt() call, is that code specific to the AI that called it or is it global for all players? I'm thinking in campaign there is no concept of having multiple AI1.js, AI2.js etc... as all the code belongs to the same interpreter. However, in multiplayer, there are multiple AI instances and no checks need to be done. Is this correct?
Events are by default only received by the players they are meant for, per loaded script. This is checked for by way of the "me" variable which is the player number of the thing using a script. A script that wishes to catch all events can use receiveAllEvents(true) or isReceivingAllEvents = true, which is what rules.js does and libcampaign. In most circumstances one doesn't need to catch all events.

In campaign the scripts are all run and owned by the player. So player checking is a must here.

MIH-XTC wrote: 29 Jul 2019, 16:59 A similar question is, how is it possible for cam_eventDroidBuilt() to only apply to the AI players and not humans if no checks are done on who the droid belongs to?

I basically want to know, do I need to check the player number of who owns structures/droids in all the event logic?
cam_eventDroidBuilt() uses camPlayerMatchesFilter(player, filter) to check the player. It would be best practice to check player numbers regardless of receiving all events.
MIH-XTC
Trained
Trained
Posts: 368
Joined: 31 Jan 2014, 07:06

Re: Are events global or specific to each player?

Post by MIH-XTC »

Gotcha, this is what I suspected but just wanted to make sure.

At the end of cam_eventDroidBuilt() it calls __camAddDroidToFactoryGroup() but that function assumes all enemies belong to the same enemy player.

I would like to check if a droid belongs to UltScav before adding it to an AI group but UltScav player # will change depending on the map so it's not a global constant.

Ironically, I see your PR for refactoring libcampaign.js and that might make it easier to check the player# in __camAddDroidToFactoryGroup() :):):)

Let me know when I should start using that version of libcampaign and I'll switch over.
User avatar
Berserk Cyborg
Code contributor
Code contributor
Posts: 938
Joined: 26 Sep 2016, 19:56

Re: Are events global or specific to each player?

Post by Berserk Cyborg »

MIH-XTC wrote: 29 Jul 2019, 23:04 At the end of cam_eventDroidBuilt() it calls __camAddDroidToFactoryGroup() but that function assumes all enemies belong to the same enemy player.

I would like to check if a droid belongs to UltScav before adding it to an AI group but UltScav player # will change depending on the map so it's not a global constant.
Units are assigned to the factory's group if it has one that it manages. Otherwise it contains newly produced units as a new group via a defensive order and then sends them on to do something when enough get built by that factory. Thus the player number is irrelevant (which is how it handles players 6 and 7 on the first two Alpha missions).

Since you want to also use a custom timers based AI, though, and thus more freedom away from the library, you can simply return out of libcampaign's eventDroidBuilt() by checking the player numbers and assigning a new global variable in the library to hold whatever player number you want to ignore per mission script.
MIH-XTC wrote: 29 Jul 2019, 23:04 Ironically, I see your PR for refactoring libcampaign.js and that might make it easier to check the player# in __camAddDroidToFactoryGroup() :):):)

Let me know when I should start using that version of libcampaign and I'll switch over.
I still have one more PR to reveal at a later time that depends on the split PR. It has a few fixes to better support mods that include more stats, like EB-Mod. It's safe to my knowledge (I did some extensive testing a few days ago) so feel free to start using the changes in this branch.
Post Reply