Constant Integer manipulation?

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
XASSASSINX
Posts: 376
Joined: Tue Dec 20, 2016 4:53 pm
Location: MURICAA BROTHER! Just kidding, Brazil.

Constant Integer manipulation?

Post by XASSASSINX »

Hello, quick question, but how do you alter constant integers in Zscript? i'm a bit lost at this and i've read every topic on the wiki and a few from here, but nothing has helped me so far. This is what i'm talking about:

Code: Select all

DamageFunction ( (Cvar.FindCvar('cl_tracerbasespeed').GetInt() ) * Random(1, 3)); 
This works perfectly, hovewer, this:

Code: Select all

Speed ( (Cvar.FindCvar('cl_tracerbasespeed').GetInt() ) * Random(1, 3));
(Ignore the extra spaces, i was just trying to debug it)

Gives a crash with the following message: "Unable to call GetInt from constant declaration"

I'm 90% sure there is a workaround here or maybe something i'm missing, any help?
User avatar
Player701
 
 
Posts: 1636
Joined: Wed May 13, 2009 3:15 am
Graphics Processor: nVidia with Vulkan support
Contact:

Re: Constant Integer manipulation?

Post by Player701 »

Constants are, well, constants, the meaning of the word being, among others, "something that does not change over time". Most of actor properties expect a constant value for their definition, DamageFunction being an exception. So it's simply not possible to declare Speed the way you want it. However, you can change the value of the Speed backing field at runtime, for example, if you override Tick:

Code: Select all

override void Tick()
{
    Super.Tick();
    Speed = Cvar.FindCvar('cl_tracerbasespeed').GetInt() * Random(1, 3);
}
Note that the above will work for monsters but not for projectiles. This is because a projectile's velocity is set only once when it gets spawned. You will have to change the value of Vel (a 3D vector) instead:

Code: Select all

override void Tick()
{
    Super.Tick();
    double mySpeed = Cvar.FindCvar('cl_tracerbasespeed').GetInt() * Random(1, 3);
    Vel = Vel.Unit() * mySpeed;
}
If you actually intend to set the speed only once and keep it at that, then overriding BeginPlay should do it. Then, you can manipulate Speed and not Vel because at this time, the velocity has not yet been set by the engine:

Code: Select all

override void BeginPlay()
{
    Super.BeginPlay();
    Speed = Cvar.FindCvar('cl_tracerbasespeed').GetInt() * Random(1, 3);
}
XASSASSINX
Posts: 376
Joined: Tue Dec 20, 2016 4:53 pm
Location: MURICAA BROTHER! Just kidding, Brazil.

Re: Constant Integer manipulation?

Post by XASSASSINX »

Constants are, well, constants, the meaning of the word being, among others, "something that does not change over time"
Oh yeah, i'd figured that would be the problem, but still, that's the point of modding!

Anyhow, i've would never figured that one out on my own, thanks!! Definetly gotta check out more of this part of Zscript, giving credit if this ever get posted, thanks!
Post Reply

Return to “Scripting”