[ZScript] Tricky Conditions (Solved)

Mon Feb 07, 2022 9:38 am

1) I need to make friendly actor to summon stimpack/medkit based of how damaged player is, but can't find a proper condition to this.
Spoiler: "Garbage"

2) Also friendly actor may drop a light source (flare actor) if there is too dark to see. Again, how can i approach to light level of sector, where that actor is standing?
Spoiler: "Trash"

I just do not want to use default "Jump" function with usual probability.
I want to make a proper approach, where actor can give a stimpack/medkit based on player's health and drop a flare only in dark sectors.
Last edited by RastaManGames on Mon Feb 07, 2022 11:55 am, edited 1 time in total.

Re: [ZScript] Tricky Conditions

Mon Feb 07, 2022 10:42 am

Do you have player cast to something? Do remember, it's not a globally accessible value. Player is a pointer to the PlayerInfo-type struct that is attached to every player pawn. More importantly, this PlayerInfo struct doesn't actually contain health; health is part of the player pawn.

So, first you need to figure out how to get a pointer to the player. Naturally, there can be multiple players in the game.

You could access it via the global players array. For example, players[0].mo will return a pointer to the player pawn controlled by the first player. But this will make it incompatible with multiplayer.

So, doing this the proper way depends on the intended purpose. For example, if this is a companion mod, then I'd spawn a separate companion for every player, and I'd just give every companion a pointer to every player. For example, in an event handler:

Code:
class CompanionsForEveryoneHandler : EventHandler
{
   // This is called for every player that enters the game:
   override void PlayerSpawned (playerEvent e)
   {
      // Cast the player pawn for this player to ppawn:
      let ppawn = players[e.PlayerNumber].mo;
      // Null-check:
      if (!ppawn)
         return;
      // Spawn the companion at the location of the player pawn
      // and cast it to comp:
      let comp = Actor.Spawn("MyFriendlyCompanion", ppawn.pos);
      // If the monster spawned successfully, set the player pawn
      // as its master:
      if (comp)
         comp.master = ppawn;
   }
}

If you do the above, a separate MyFriendlyCompanion will be spawned for every player, and the corresponding player pawn will be accessible to MyFriendlyCompanion via the 'master' pointer. From there on, you can just check master.health from MyFriendlyCompanion whenever you want.

Also, I can't imagine a good reason to use A_SpawnProjectile for your case. First, the CMF_CHECKTARGETDEAD flag won't do anything sensible here because the player is not the target here—since the monster is friendly, it'll be targeting other monsters. Second, it's generally not a function for spawning anything but projectiles. If you want to spawn it with an offset (and perhaps even give it some velocity), A_SpawnItemEx is what you need.
Last edited by Jekyll Grim Payne on Mon Feb 07, 2022 1:16 pm, edited 1 time in total.

Re: [ZScript] Tricky Conditions

Mon Feb 07, 2022 10:49 am

RastaManGames wrote:2) Also friendly actor may drop a light source (flare actor) if there is too dark to see. Again, how can i approach to light level of sector, where that actor is standing?


This is easy. Just check cursector.lightlevel in your actor, it'll get you the light level of the sector they're int.

Re: [ZScript] Tricky Conditions (Solved)

Mon Feb 07, 2022 11:55 am

Woah, there is that thing with "Master". It helps me a lot (also i noticed few my stupid errors like incrementation inside of condition and "==" of counter instead of "=").
Also thanks for "CurSector" fix! You will be in credits of next thing I am about to release.