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!)
Noxsis1408
Posts: 7
Joined: Wed Nov 22, 2017 2:32 pm

ACS math logic help

Post by Noxsis1408 »

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!!!
User avatar
ramon.dexter
Posts: 1255
Joined: Tue Oct 20, 2015 12:50 pm
Graphics Processor: nVidia with Vulkan support
Location: Kozolupy, Bohemia

Re: ACS math logic help

Post by ramon.dexter »

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
Arctangent
Posts: 1235
Joined: Thu Nov 06, 2014 1:53 pm
Discord: SquawkyAtan#2371

Re: ACS math logic help

Post by Arctangent »

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 [wiki]scope[/wiki]. 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 all

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 all

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, [wiki]operators[/wiki]. 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 [wiki]script types[/wiki] 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 [wiki]loops[/wiki] for something like that. In this situation, you'd want to set up a while loop, such as

Code: Select all

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 [wiki]delay[/wiki]ing 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 all

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!
	}
}
Noxsis1408
Posts: 7
Joined: Wed Nov 22, 2017 2:32 pm

Re: ACS math logic help

Post by Noxsis1408 »

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?
User avatar
Arctangent
Posts: 1235
Joined: Thu Nov 06, 2014 1:53 pm
Discord: SquawkyAtan#2371

Re: ACS math logic help

Post by Arctangent »

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.
Noxsis1408
Posts: 7
Joined: Wed Nov 22, 2017 2:32 pm

Re: ACS math logic help

Post by Noxsis1408 »

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);
}
User avatar
Arctangent
Posts: 1235
Joined: Thu Nov 06, 2014 1:53 pm
Discord: SquawkyAtan#2371

Re: ACS math logic help

Post by Arctangent »

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.
Noxsis1408
Posts: 7
Joined: Wed Nov 22, 2017 2:32 pm

Re: ACS math logic help

Post by Noxsis1408 »

I tried it and it worked. Thanks for all your help!!!

Return to “Scripting”