What does "track" do? (ZScript)

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

What does "track" do? (ZScript)

Postby Agentbromsnor » Sun Feb 18, 2018 4:10 pm

Since I've been looking at Nash's old platformer ZScript thing, I've noticed that he uses "track" for the camera. I would like to know more about this function, since I would like to implement some more sophisticated camera controls in my game (for example: locking down the camera on a certain axis). However, I can't find any documentation on this function and I've looked everywhere.

I would greatly appreciate the documentation.
User avatar
Agentbromsnor
 
Joined: 28 Mar 2012

Re: What does "track" do? (ZScript)

Postby gwHero » Sun Feb 25, 2018 2:46 am

You mean track in 'if (track)' ? (line 9)
Code: Select allExpand view
class SideScrollerCamera : SecurityCamera
{
   // the player to track/scroll to
   SideScrollerPlayer track;

   override void Tick(void)
   {
      // position and point the camera to the player
      if (track)
      {
         double cx = track.Pos.X;
         double cy = track.Pos.Y + CAMERA_DISTANCE;
         double cz = track.Pos.Z + CAMERA_HEIGHT;
         Vector3 camPos = (cx, cy, cz);
         SetOrigin(camPos, true);
         Angle = CAMERA_ANGLE;
      }

      Super.Tick();
   }
}


It's the player object; it's defined as SideScrollerPlayer, which inherits DoomPlayer (you can check that in player\SideScrollerPlayer).

What Nash is doing, is checking if the player exists. Before you refer in code to an object, it's best to always check if an object is existing, otherwise you could get those nasty null errors. So, only in the if block, when it's certain that a player exists, the camera is synced to the player's position.
User avatar
gwHero
Be your own Siddhartha
 
Joined: 08 May 2017
Location: The Netherlands
Operating System: Windows 10/8.1/8/201x 64-bit
Graphics Processor: Intel with Vulkan Support

Re: What does "track" do? (ZScript)

Postby Nash » Sun Feb 25, 2018 8:36 am

I apologize for the overly-short or perhaps not-so-descriptive variable name but as gwHero already explained; it's a variable holding a reference (pointer) to a DoomPlayer-derived object called SideScrollerPlayer. The SideScrollerCamera needs to know which player to follow, hence why it's stored in that variable.

"track" is a local variable and it can be named anything, really. I could just as well name it "ThePlayerObjectThatThisCameraMustScrollAlongWith" but that would be annoying to type. :P

The "if (track)" part simply checks to make sure that the "track" variable is not empty before proceeding. If I had not added that check there, GZDoom would if the pointer to the player does not exist. And it may happen, depending on several factors not in my control.

People may write the same code as "if (track != NULL)" and I know some people are going to get anal about my overly-short null check but, meh, whatever... =P
User avatar
Nash
Twitter/Facebook/Youtube: nashmuhandes
 
 
 
Joined: 27 Oct 2003
Location: Kuala Lumpur, Malaysia
Twitch ID: nashmuhandes
Github ID: nashmuhandes


Return to Scripting

Who is online

Users browsing this forum: skeletronmk666 and 2 guests