Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Kyka

Yes. It's time for another nublet ACS question.

Recommended Posts

Ok, this is truly a bit of an abomination of coding, but it is the best I could come up with... I am trying to make a sector (tag 132) where the values of red, green and blue scroll from 0 to 255 and then back down to 0 again, and continue this cycle repeatedly.

My idea was to have the values of x,y and z count up to 255 in one script, terminate that script when they got to 255, and execute another script that would start the count back down to zero, etc etc.

It seemed like a good idea to me.

But it doesn't work at all. I am sure there is at least a dozen easier ways to do this, using something like FOR or WHILE, but I can't figure out how to do the cycling up and down thing with those.

#include "zcommon.acs"

int x = 255;
int y = 255;
int z = 255;

Script 11 OPEN
{
	Sector_SetColor(132, x, y, z);
}

Script 12 (void)
{
	
	x=x+1;
	delay(2);
	restart;
	
}

Script 13 open
{
	x=x-1;
	delay(2);
	restart;

}

Script 14 OPEN
{
If (x == 0)
	{
	ACS_Terminate(13, 0);
	ACS_Execute(12, 0, 0, 0, 0);
	}
}

Script 15 OPEN
{
If (x == 255)
	{
	ACS_Terminate(12, 0);
	ACS_Execute(13, 0, 0, 0, 0);
	}
}

//repeat scripts 12, 13, 14 and 15 for y and z also, with a slightly different delay to get the mixing of colors properly.
Any help to make this work appreciated. :)

Thanks people.

Share this post


Link to post
Kyka said:

Ok, this is truly a bit of an abomination of coding, but it is the best I could come up with... I am trying to make a sector (tag 132) where the values of red, green and blue scroll from 0 to 255 and then back down to 0 again, and continue this cycle repeatedly.

My idea was to have the values of x,y and z count up to 255 in one script, terminate that script when they got to 255, and execute another script that would start the count back down to zero, etc etc.

It seemed like a good idea to me.

Not to me. I think it's better to use only one script, and use a value that'll be alternatively +1 or -1.

#include "zcommon.acs"

// Value variables -- go from 255 to 0 and back
int x = 255;
int y = 255;
int z = 255;
// Iteration variables -- either -1 or 1
int xi = -1;
int yi = -1;
int zi = -1;
// Time variables for delay, set to whatever you want
int xt =  2;
int yt =  3;
int zt =  5;

Script 11 OPEN
{
	while (1) // Needs to be done every tic
	{
		Sector_SetColor(132, x, y, z);
		delay(1);
	}
}

Script 12 OPEN
{
	while (1)
	{
		if (xi == 255) xi = -1;
		else if (xi == 0) xi = 1;
		x = x + xi;
		delay(xt);
	}
}

Script 13 OPEN
{
	while (1)
	{
		if (yi == 255) yi = -1;
		else if (yi == 0) yi = 1;
		y = y + yi;
		delay(yt);
	}
}
Script 14 OPEN
{
	while (1)
	{
		if (zi == 255) zi = -1;
		else if (zi == 0) zi = 1;
		z = z + zi;
		delay(zt);
	}
}
Even better you could use a random value instead of xt/yt/zt.

Note: you use "while (1)" so it's always true and loop endlessly. You use delay() so it doesn't loop endlessly in the same tic, or you'll get a runaway script error -- you want the loop to happen at most once per tic, after all.

Share this post


Link to post

One quick and simple way to do it:

#include "zcommon.acs"

int x = 255; //If red, green and blue always have the same value, you can do with just one variable
int addition = -1;

Script 11 OPEN
{
	Sector_SetColor(132, x, x, x);
}

Script 12 (void)
{
	while(1){ //Should be "always true", however I'm not 100% sure about if this works in ACS
		x = x + addition; //If 'addition' is -1, this decrements!
		if(x == 255){
			addition = -1; //Make it so we'll start decrementing
		}else if (x == 0){
			addition = 1; //Make it so we'll start incrementing
		}
		delay(35);
	}

}
If it's unclear, just ask. :)


edit: Heh, Gez posted at the same time. His example is better if you need more control over the color change, other than that it's essentially the same idea.

Share this post


Link to post

Yea FOR and WHILE loops would be so much easier to read ... so if I'm reading this right you want ALL the colors to go up and down at the same time? Or one at a time?

For doing all at once, you could use something like;

int updown = 0; int x = 0; int y = 0; int z = 0; //would be outside this script

script xx (int updown)
{While(updown == 0)
  {
     for (int up = 0; up < 255; up ++)
       { x ++; y ++; z ++; }
     updown ++;
   }
  While (updown == 1)
  {
     for (itn down = 0; down < 255; down ++)
        { x --; y --; z --; }
      updown --;
   }
}
I'm probably off on somethings, especially on the space between ++/--, but it should resemble something like that.

EDIT: dang, that was fast you guys ... ugh my script be so noob cause it's off a single semester of C++ and no ACS experience -.-

Share this post


Link to post

ArmouredBlood said:

int updown = 0; int x = 0; int y = 0; int z = 0; //would be outside this script

script xx (int updown)
{While(updown == 0)
  {
     for (int up = 0; up < 255; up ++)
       { x ++; y ++; z ++; }
     updown ++;
   }
  While (updown == 1)
  {
     for (itn down = 0; down < 255; down ++)
        { x --; y --; z --; }
      updown --;
   }
}
I'm probably off on somethings, especially on the space between ++/--, but it should resemble something like that.

A problem with this is that the script doesn't loop infinitely: After the "While (updown == 1)"-loop the script simply terminates. Also, you have the int "updown" both outside the script and as a parameter, which is a bit redundant.

Learning from mistakes of your own is the only way to get better at programming, so don't take this the wrong way. :)



@Kyka: Also just noticed that you needed different delays for each color, so do what Gez did. :P He also got the loop for setting the sector colors right... (see his script 11)

Share this post


Link to post

Huh, I didn't know how ACS does it's infinite loops so I just put what I knew down. And thanks for that lesson, I'm sure I'll eventually get to scripting sometime and have to bother you guys with questions too, so anything I get before then will help.

Share this post


Link to post

Thanks heaps for the replies everyone. Hugely appreciated. And while I will probably cut and paste Gez's code, I have certainly spent some time (and will spend some more time again) making sure that I at least have a good idea of what all the code you guys have put here does. No point if I just grab the first piece of code I see without learning anything about how it works...

I can follow the logic behind the coding here pretty well. What is much harder for me is getting to grips with the syntax and how that syntax kind of shapes the deeper logic behind the code itself.

Couple of quick questions.

while(1) means "always true" unless I specify something else, yes? So Jodwin, in your code, the while(1) remains always true, so the script should always add x and the variable "addition". And when x = 0 or x = 255, the variable "addition" is changed from a value of 1 to a value of -1 and vice versa.

I think I have that right.

In ArmouredBlood's code, there is a line that says:

for (int up = 0; up < 255; up ++)

If I understand, this line is saying "set the value of up to 0, then add 1 to up while up < 255" Is this right?

And would it be possible to fix ArmouredBlood's code by simply putting a "restart;" right at the end, which would loop it endlessly?

Anyway, thanks again everyone for the replies, especially:

ArmouredBlood said:

Well we like kyka and want to help him ;)


Yaaay. My whole existence has been validated. ;)

You guys are awesome. Man, coding is an art.

Share this post


Link to post
Kyka said:

Couple of quick questions.

while(1) means "always true" unless I specify something else, yes? So Jodwin, in your code, the while(1) remains always true, so the script should always add x and the variable "addition". And when x = 0 or x = 255, the variable "addition" is changed from a value of 1 to a value of -1 and vice versa.

I think I have that right.

A while()-loop loops until it's either ended with "break;", the script is terminated or the condition in the while(...)-statement becomes false (or "not true"). In C-like languages (which ACS could count as) the number 1 is "true" and since "while(1)" does not include anything else in the statement, it's "always true" and thus loops infinitely.

Kyka said:

In ArmouredBlood's code, there is a line that says:

for (int up = 0; up < 255; up ++)

If I understand, this line is saying "set the value of up to 0, then add 1 to up while up < 255" Is this right?

Yep.

Kyka said:

And would it be possible to fix ArmouredBlood's code by simply putting a "restart;" right at the end, which would loop it endlessly?

As long as you also fix the 'updown' variable. A more preferable way would be to put it all inside a while(1)-loop, though. ;) Also, you could actually remove the current while()-loops altogether, since they're almost completely useless (the contents are executed only once, if at all). Replace the word "while" with "if" and you'll see what I mean. The way it would work wouldn't change at all...

Share this post


Link to post
Kyka said:

Yaaay. My whole existence has been validated. ;)


Now don't feel too good just yet. I've seen your project screenshots and I have high expectations! you fail me and ... well, you dont want to knows what happens to people who don't finish/release their projects ...

Share this post


Link to post

*gulp*

Heh. One thing I have a lot of is persistence tho. So it will get done. Though I make no promises on any timeline. :) And I haven't forgotten your offer of some playtesting. Soon, my pretties, soon there will be some levels to have a look at.

Thanks for the encouragement. :)

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×