"How do I 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.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
User avatar
Gutawer
Posts: 469
Joined: Sat Apr 16, 2016 6:01 am
Preferred Pronouns: She/Her

Re: "How do I ZScript?"

Post by Gutawer »

Vaecrius wrote:someone else will have to reply with how to get the angle (which I'd like to know as well).
Well, once you have the line, you can access the line's delta vector and get the vector perpendicular to it:

Code: Select all

Vector2 perpVec = (-line.delta.y, line.delta.x).unit();
Once you've got that, you can use the formula r = d - 2(d dot n)n (where n must be normalised; this is the point of the .unit() call) to generate the reflected vector:

Code: Select all

Vector2 reflected = vel.xy - 2 * (vel.xy dot perpVec) * perpVec;
to get the Vector2 of reflection (you can upgrade this to 3d by making reflected a Vector3, removing all calls to .xy in the formula, and constructing a 3d perpVec (where Z will always be 0 since a linedef cannot be sloped)). Then, if you want the angle of reflection:

Code: Select all

double rAngle = atan2(reflected.y, reflected.x);
Provided all my maths is correct, that should work.
User avatar
phantombeta
Posts: 2113
Joined: Thu May 02, 2013 1:27 am
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia with Vulkan support
Location: Brazil

Re: "How do I ZScript?"

Post by phantombeta »

@Gutawer
How would I use RenderOverlay for this? I couldn't find anything on using it.
Also, I'm not entirely sure RenderOverlay would be the best way to do this... This is meant to simply display a message telling the player that they levelled up, then fade out.
User avatar
Agentbromsnor
Posts: 265
Joined: Wed Mar 28, 2012 2:27 am

Re: "How do I ZScript?"

Post by Agentbromsnor »

I'm trying to get the player's sprite to change depending on what direction the analogue input is providing for my 2D platform-game.

I currently have it set up like this, using a StateLabel array:

Code: Select all

void PlayerControls(void)
	{
		//All the animation states are placed in this array
		//The actual animation is called at the end of this function using SetStateLabel
		StateLabel animState[5];
		animState[0] = "Spawn";
		animState[1] = "Walking_right";
		animState[2] = "Walking_left";
		animState[3] = "Jumping_right";
		animState[4] = "Jumping_left";
		
		//This variable is used to call the animation states as presented above
		int myAnim = 0;
		
		double PlayerMovement = GetPlayerInput(INPUT_SIDEMOVE);
		
		if(PlayerMovement > 0)
		{
			Console.Printf("Right");
			int myAnim = 1;
			//if(PlayerAttacking)
			//{
				//Correct animation here
			//}
		}
		else if(PlayerMovement < 0)
		{
			Console.Printf("Left");
			int myAnim = 2;
			//if(PlayerAttacking)
			//{
				//Go to the right facing attack animation
			//}
		}
		SetStateLabel(animState[myAnim]);
	}
	
	override void Tick()
    {
		PlayerAngle();
		PlayerControls();
		Super.Tick();
    }
}
For some reason, the player will only display the right-facing sprite (only the first frame) and nothing else no matter what movement I try. What am I doing wrong here?
User avatar
Nash
 
 
Posts: 17454
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia

Re: "How do I ZScript?"

Post by Nash »

What I did for my side scroller starter kit was just use non-rotating sprites (0 angle suffix) and scale the sprite's X negatively to make it flip left. This has the benefit of you only needing to provide art once instead of twice.
User avatar
gwHero
Posts: 360
Joined: Mon May 08, 2017 3:23 am
Graphics Processor: Intel with Vulkan/Metal Support
Location: The Netherlands

Re: "How do I ZScript?"

Post by gwHero »

True, and instead of making X negative, this might even work now since GZDoom 3.1.0

Code: Select all

bSPRITEFLIP = (PlayerMovement < 0);
User avatar
Agentbromsnor
Posts: 265
Joined: Wed Mar 28, 2012 2:27 am

Re: "How do I ZScript?"

Post by Agentbromsnor »

Oh, that's definitely something I'll consider adding. But... My code isn't working. :p Or is my current method somehow causing the code not to work properly?
User avatar
gwHero
Posts: 360
Joined: Mon May 08, 2017 3:23 am
Graphics Processor: Intel with Vulkan/Metal Support
Location: The Netherlands

Re: "How do I ZScript?"

Post by gwHero »

Don't know if this is the error for sure, but what I do see is that you declare myAnim 3 times; (I think the compiler actually should reject this).
Anyway, where you refer to myAnim in the IF this should be without the 'int' keyword:

Code: Select all

      int myAnim = 0;
      
      double PlayerMovement = GetPlayerInput(INPUT_SIDEMOVE);
      
      if(PlayerMovement > 0)
      {
         Console.Printf("Right");
         myAnim = 1;
      }
      else if(PlayerMovement < 0)
      {
         Console.Printf("Left");
         myAnim = 2;
      }
      SetStateLabel(animState[myAnim]);

User avatar
Agentbromsnor
Posts: 265
Joined: Wed Mar 28, 2012 2:27 am

Re: "How do I ZScript?"

Post by Agentbromsnor »

Oh wow, that's a good catch. I'll change that right away. Thanks!
User avatar
gwHero
Posts: 360
Joined: Mon May 08, 2017 3:23 am
Graphics Processor: Intel with Vulkan/Metal Support
Location: The Netherlands

Re: "How do I ZScript?"

Post by gwHero »

yw!
Hopefully it solved the issue :)
User avatar
Agentbromsnor
Posts: 265
Joined: Wed Mar 28, 2012 2:27 am

Re: "How do I ZScript?"

Post by Agentbromsnor »

Well, it certainly made things better, haha! :D It changes the player-sprite on left and right input accordingly, but it's still not animating. It should be animating...

I'm going to delve into the code some more, and if I go full derp again I might leave another comment here. :p
ZzZombo
Posts: 317
Joined: Mon Jul 16, 2012 2:02 am

Re: "How do I ZScript?"

Post by ZzZombo »

It's obviously not gonna animate a thing if you keep resetting the state right back each tick.
User avatar
Agentbromsnor
Posts: 265
Joined: Wed Mar 28, 2012 2:27 am

Re: "How do I ZScript?"

Post by Agentbromsnor »

Oh yeah... I'm going to have to write a thing for that. Thanks! I still have a lot to learn in regards to ZScripting, obviously. :)
User avatar
vidumec
Posts: 61
Joined: Sat Apr 15, 2017 2:38 am

Re: "How do I ZScript?"

Post by vidumec »

is there any way to draw something on screen ( drawimage ) without having to redo the whole HUD in zscript from scratch?
User avatar
Nash
 
 
Posts: 17454
Joined: Mon Oct 27, 2003 12:07 am
Location: Kuala Lumpur, Malaysia

Re: "How do I ZScript?"

Post by Nash »

Yes, make an EventHandler and draw it in RenderOverlay.
User avatar
vidumec
Posts: 61
Joined: Sat Apr 15, 2017 2:38 am

Re: "How do I ZScript?"

Post by vidumec »

ooh shiny, that's nice, thanks

edit: why does "DrawImage("sprites/SpriteName.png",(0,0))" work, but "DrawImage("SpriteName.png",(0,0))" doesn't? Assuming i have the same sprite in both "root" and "sprites" directories

Return to “Scripting”