Recreating ACS Vector Angle script to 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!)
Post Reply
andreboomer
Posts: 21
Joined: Wed Feb 16, 2022 9:04 pm
Graphics Processor: nVidia with Vulkan support

Recreating ACS Vector Angle script to Zscript

Post by andreboomer »

I'm using the link below:
https://zdoom.org/wiki/VectorAngle

I want to recreate the ACS script used in the example above, but in zscript. So Specifically I'm not relying on a TID but a pointer to the nearest actor/target.

For example with the line:
vang = VectorAngle (GetActorX (1) - GetActorX (0), GetActorY (1) - GetActorY (0));

I would like to replicate that in zscript, I was thinking maybe with DeltaAngle using pointers such as "Owner" to represent player and "Target" to represent a nearby actor.
Could anyone help me out, i'd greatly appreciate it.
:mrgreen:
User avatar
m8f
 
 
Posts: 1466
Joined: Fri Dec 29, 2017 4:15 am
Preferred Pronouns: He/Him
Location: Siberia (UTC+7)
Contact:

Re: Recreating ACS Vector Angle script to Zscript

Post by m8f »

There is a function for the Actor class:

Code: Select all

clearscope double AngleTo(Actor target, bool absolute = false) const
Isn't it what you want?
andreboomer
Posts: 21
Joined: Wed Feb 16, 2022 9:04 pm
Graphics Processor: nVidia with Vulkan support

Re: Recreating ACS Vector Angle script to Zscript

Post by andreboomer »

m8f wrote: Mon Aug 08, 2022 8:15 pm There is a function for the Actor class:

Code: Select all

clearscope double AngleTo(Actor target, bool absolute = false) const
Isn't it what you want?
this is what I came up with(not a coder by trade, so I'm probably way off)
vang = deltaangle(owner.angle, AngleTo(target));

but this line crashes the game. I'm stumped honestly.
User avatar
m8f
 
 
Posts: 1466
Joined: Fri Dec 29, 2017 4:15 am
Preferred Pronouns: He/Him
Location: Siberia (UTC+7)
Contact:

Re: Recreating ACS Vector Angle script to Zscript

Post by m8f »

Try

Code: Select all

vang = deltaangle(owner.angle, owner.AngleTo(target));
or maybe just

Code: Select all

vang = owner.AngleTo(target);
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49252
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: Recreating ACS Vector Angle script to Zscript

Post by Graf Zahl »

andreboomer wrote: Mon Aug 08, 2022 8:29 pm but this line crashes the game. I'm stumped honestly.
It crashes how? You got to be a bit more informative here if you need help.
User avatar
Rachael
Posts: 13965
Joined: Tue Jan 13, 2004 1:31 pm
Preferred Pronouns: She/Her
Contact:

Re: Recreating ACS Vector Angle script to Zscript

Post by Rachael »

Do make sure also to check the nullptrs for both owner and target - ZScript will not check them for you and will crash if you attempt to utilize a null actor object.
andreboomer
Posts: 21
Joined: Wed Feb 16, 2022 9:04 pm
Graphics Processor: nVidia with Vulkan support

Re: Recreating ACS Vector Angle script to Zscript

Post by andreboomer »

Graf Zahl wrote: Tue Aug 09, 2022 12:59 am
andreboomer wrote: Mon Aug 08, 2022 8:29 pm but this line crashes the game. I'm stumped honestly.
It crashes how? You got to be a bit more informative here if you need help.
Ok so this is basically what I wrote

Line495: Let TargetAngle = deltaangle(owner.angle, owner.AngleTo(target));
Let angle = (TargetAngle - owner.angle + 1.0) % 1.0;


Then as a conditional statement I wrote
if(owner.angle < 0.2 || owner.angle > 0.8) ;
int sx 320 = 320 - (320*Sin(angle)/Cos(angle);

All of this is within a ModifyDamage() function.

The error pops up with the game running as:
VM execution aborted:tried to read from address zero. In function parameter targ
the next line saying where it's called from which is line : 495

Rachael wrote: Tue Aug 09, 2022 1:22 am Do make sure also to check the nullptrs for both owner and target - ZScript will not check them for you and will crash if you attempt to utilize a null actor object.
I do think it is from the pointers that the error exists.
User avatar
Rachael
Posts: 13965
Joined: Tue Jan 13, 2004 1:31 pm
Preferred Pronouns: She/Her
Contact:

Re: Recreating ACS Vector Angle script to Zscript

Post by Rachael »

andreboomer wrote: Tue Aug 09, 2022 6:09 pm The error pops up with the game running as:
VM execution aborted:tried to read from address zero. In function parameter targ
the next line saying where it's called from which is line : 495

Rachael wrote: Tue Aug 09, 2022 1:22 am Do make sure also to check the nullptrs for both owner and target - ZScript will not check them for you and will crash if you attempt to utilize a null actor object.
I do think it is from the pointers that the error exists.
That is exactly what I was talking about.

You are not checking for the existence of target.

Try this:

Code: Select all

if (target != null && owner != null)
{
    Let TargetAngle = deltaangle(owner.angle, owner.AngleTo(target));
    Let angle = (TargetAngle - owner.angle + 1.0) % 1.0;
}
andreboomer
Posts: 21
Joined: Wed Feb 16, 2022 9:04 pm
Graphics Processor: nVidia with Vulkan support

Re: Recreating ACS Vector Angle script to Zscript

Post by andreboomer »

Rachael wrote: Tue Aug 09, 2022 6:18 pm
andreboomer wrote: Tue Aug 09, 2022 6:09 pm The error pops up with the game running as:
VM execution aborted:tried to read from address zero. In function parameter targ
the next line saying where it's called from which is line : 495

Rachael wrote: Tue Aug 09, 2022 1:22 am Do make sure also to check the nullptrs for both owner and target - ZScript will not check them for you and will crash if you attempt to utilize a null actor object.
I do think it is from the pointers that the error exists.
That is exactly what I was talking about.

You are not checking for the existence of target.

Try this:

Code: Select all

if (target != null && owner != null)
{
    Let TargetAngle = deltaangle(owner.angle, owner.AngleTo(target));
    Let angle = (TargetAngle - owner.angle + 1.0) % 1.0;
}
Where would it be best to check "if(owner.angle < 0.2 || owner.angle > 0.8)"

Could I call it from DoEffect()?

and if if(owner.angle < 0.2 || owner.angle > 0.8)" is true, could I create a bool to return true once the condition is met?
User avatar
Rachael
Posts: 13965
Joined: Tue Jan 13, 2004 1:31 pm
Preferred Pronouns: She/Her
Contact:

Re: Recreating ACS Vector Angle script to Zscript

Post by Rachael »

I'm trying to explain to you about null pointers and you're still going on about checking actor properties without even checking if they exist first.

Well - I did what I could to help you, I even provided you with some code. It's all in your hands, now.
andreboomer
Posts: 21
Joined: Wed Feb 16, 2022 9:04 pm
Graphics Processor: nVidia with Vulkan support

Re: Recreating ACS Vector Angle script to Zscript

Post by andreboomer »

Rachael wrote: Tue Aug 09, 2022 11:17 pm I'm trying to explain to you about null pointers and you're still going on about checking actor properties without even checking if they exist first.

Well - I did what I could to help you, I even provided you with some code. It's all in your hands, now.
Oh my bad for not clarifying, I did null check the pointers after you told me to. It runs now, but the script currently is doing nothing. Im assuming because it isn't being called correctly.

Bool SeeYou;

Override Void Doeffect()
{
if (target != null && owner != null)
{
Let TargetAngle = deltaangle(owner.angle, owner.AngleTo(target));
Let angle = (TargetAngle - owner.angle + 1.0) % 1.0;
if(owner.angle < 0.2 || owner.angle > 0.8)
{
int sx = 320 - (320 * Sin (angle) / Cos (angle));
Seeyou = true;
}
else
{
Seeyou = false;
}
}
super.DoEffect();
}

I think using DoEffect() to check if the Bool is true, and if it's true check for it in the modify damage function?

Override Void ModifyDamage (int damage, Name damageType, out int newdamage,
bool passive, Actor inflictor, Actor source, int flags)
if (passive && damage > 0)
{
if(seeyou == true)
{
DoStuff();
A_PrintBold("Works!");
}
}

I understand if you can't help me further, I still appreciate what you were able to provide so that I could progress forward.
User avatar
m8f
 
 
Posts: 1466
Joined: Fri Dec 29, 2017 4:15 am
Preferred Pronouns: He/Him
Location: Siberia (UTC+7)
Contact:

Re: Recreating ACS Vector Angle script to Zscript

Post by m8f »

Target! The item doesn't have a target, the owner has.

try

Code: Select all

if (owner != null && owner.target != null)
{
Let TargetAngle = deltaangle(owner.angle, owner.AngleTo(owner.target));
andreboomer
Posts: 21
Joined: Wed Feb 16, 2022 9:04 pm
Graphics Processor: nVidia with Vulkan support

Re: Recreating ACS Vector Angle script to Zscript

Post by andreboomer »

m8f wrote: Wed Aug 10, 2022 9:20 am Target! The item doesn't have a target, the owner has.

try

Code: Select all

if (owner != null && owner.target != null)
{
Let TargetAngle = deltaangle(owner.angle, owner.AngleTo(owner.target));
Yea it was the pointers, thanks to everyone for the help.
Post Reply

Return to “Scripting”