ACS math logic help

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!)

ACS math logic help

Postby Noxsis1408 » Wed Nov 22, 2017 3:14 pm

Im trying to make a script when you kill a monster it adds 1 to a compiler and when it reaches a certain amount it will activate a script. this is kinda what i was trying to do:
Bare with me I'm a Noob when it comes to scripting
script 1 (void)
{
int a;
(a + 1 = a);
}

Script 2 open

{
//when int a = x (do such and such);

then terminate script 2
}

so you kill a monster you can set its special to script 2 and it will add 1 to a. then run a open script and it will activate as soon as you have killed a certain amount of monsters. then terminate its self so it run away.

any help would be most appreciated thanks!!!
Noxsis1408
 
Joined: 22 Nov 2017

Re: ACS math logic help

Postby ramon.dexter » Wed Nov 22, 2017 11:22 pm

since it's wrong, it's deleted...
Last edited by ramon.dexter on Thu Nov 23, 2017 4:16 am, edited 1 time in total.
User avatar
ramon.dexter
rudebwoy
 
Joined: 20 Oct 2015
Location: Kozolupy, Bohemia
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: nVidia with Vulkan support

Re: ACS math logic help

Postby Arctangent » Thu Nov 23, 2017 1:58 am

That ... example is probably less than helpful, especially since it's outright wrong.

Alright, so let's just get to the nitty gritty of your actual mistakes.

First, you need to understand a thing called scope. There's more to it than this, but the basics of it is that a variable define in a script only exists to that script and any functions it calls, while a variable defined outside of any script exists for all of them. Or, to put it into actual code, this

Code: Select allExpand view
script 1 OPEN {
   int a = 6;
}

script 2 ( void ) {
   int b = a + 5;
}


will error out, because script 2 has no access to a variable called a, while

Code: Select allExpand view
int a;

script 1 OPEN {
   a = 6;
}

script 2 ( void ) {
   int b = a + 5;
}


will compile, as a is in the proper scope for script 2 to access it.

Next, operators. While your usage of the + operator is correct, that = is utter nonsense to ZDoom and ... every programming language I can think of. This is because it's specifically the variable to the left side of the = operator is what gets set to whatever's on the right side of it. Which means a + 1 = a is incorrect in that it's trying to set a non-variable to a value, but a = a + 1 is correct because it's setting a variable to a value. That said, ramon.dexter was correct that the ++ operator will work instead; in fact, that's its only purpose.

Finally, you'll need to take a moment to consider what you're doing. From what I can tell, you have actors set up so that they can call script 1 via their special ( which is executed when they're picked up if they're items, or when they die if they're monsters ), and then use script 2 to check if that values reaches a certain threshold. You've almost got the framework for that, but let's explain why via script types real quick. script 1 has no type, while script 2 has the OPEN type - meaning that script 1 won't call itself on its own, while script 2 will do so when the map starts up. That said, that means it starts up, goes through its code, then ends - that's because scripts don't repeat by default, and you need something like loops for something like that. In this situation, you'd want to set up a while loop, such as

Code: Select allExpand view
script 2 OPEN {
   while( a < x ) {
      Delay( 5 );
   }
   
   // place the rest of the code here
}


This is a script that runs at the start of the map, then immediately enters into a neverending loop until a is equal or greater than x ( or, rather, it continuously loops while a is less than x - as the name implies, while loops don't stop until their condition is no longer met ). During this loop, the script does absolutely nothing aside from delaying itself from doing anything else, which is important because otherwise the loop would go on forever at infinite speed - or at least, it would if not for the fact that ZDoom cancels ACS infinite loops automatically by terminating the script, meaning that it'd instead cause the script to break instantly. Either way, you don't want to leave our a Delay() call in a loop that won't end itself without time passing. Anyway,

Once that condition is no long met, the while loop will automatically end, leading to the rest of the script - this is why we're using a while loop, as it allow us the basically park a script in place until that condition is met, then continue on where it left off without a hitch. Then, once it's done all that code, script 2 will automatically terminate - for the same reason that you don't need to put terminate in script 1.

One foreword: when and then don't exist in ACS, it's an C-based language. Instead, you have if statements, which are structured like this:

Code: Select allExpand view
script 1 void {
   if( x == y ) { // the condition
      // the stuff you want to put after "then" goes here
   } // these curly braces are essential, though you can place them like this ...
   
   if( x == z )
   {
   } // or like this; spacing and stuff is very flexible in acs, you just need to make sure that the right symbols follow the right symbols
   else
   {
      // also, using the else statement, you run code should the if statement fail!
   }
}
User avatar
Arctangent
squawky
 
Joined: 06 Nov 2014
Discord: SquawkyAtan#2371

Re: ACS math logic help

Postby Noxsis1408 » Thu Nov 23, 2017 7:02 pm

So could you show me a complete script for this situation. I get the while loop I've used a couple but not in this format. How would I go about creating the action special part to add to A? That's kinda where I got lost. I learn mostly by seeing the end result and working my way backwards to see the prosces. This is what I get now:

Int A;
Int X;

script 2 OPEN {
while( A < X ) {
Delay( 5 );
}

Floor_RaiseToHighest (1, 5);
}

But how do I go about adding to X to get the loop to end. So right now I have 6 Imps all with different tids. I want to have them call script 1 which will add one to X until A is less than X. Do I use:

Script 1 (void)
{
(X++1);
}

Maybe it's the sleep deprivation or I'm just not getting it. But how do I add to X?
Noxsis1408
 
Joined: 22 Nov 2017

Re: ACS math logic help

Postby Arctangent » Thu Nov 23, 2017 8:12 pm

You don't actually need to use a variable in that circumstance - you can just compare A to a static number.

That said, you can initialize variables to a certain value by using the = operator during their creation i.e. instead of just int X;, you can use int X = 8; to start x off.

Also, you have your while logic backwards - it won't break out of the loop until the condition is no longer being met, i.e. while( A < X ) loops while A is less than X, and therefore won't stop looping until A is at least equal to X. You'd want to increment A instead.

Also also, the ++ operator doesn't use a second value; you just do var++. Though for reference, there is the similar += operator that is basically a mix of + and =; it takes the variable on the left, then adds the value on the right to it. Similarly, there's the -- and -= operators, for decrementing.
User avatar
Arctangent
squawky
 
Joined: 06 Nov 2014
Discord: SquawkyAtan#2371

Re: ACS math logic help

Postby Noxsis1408 » Thu Nov 23, 2017 9:14 pm

So something like this?

Int A=5;
Int X;

Script 1 (void)
{
(X++);
}

script 2 OPEN {
while( A > X ) {
Delay( 5 );
}

Floor_RaiseToHighest (1, 5);
}
Noxsis1408
 
Joined: 22 Nov 2017

Re: ACS math logic help

Postby Arctangent » Thu Nov 23, 2017 11:37 pm

You don't need to enclose the X++ in parenthesis, though I don't think it'll hurt. As far as I can see, it should be ready to go.
User avatar
Arctangent
squawky
 
Joined: 06 Nov 2014
Discord: SquawkyAtan#2371

Re: ACS math logic help

Postby Noxsis1408 » Fri Nov 24, 2017 1:35 am

I tried it and it worked. Thanks for all your help!!!
Noxsis1408
 
Joined: 22 Nov 2017


Return to Scripting

Who is online

Users browsing this forum: No registered users and 2 guests