Page 1 of 1

Palette cycling!

Posted: Sun Jan 14, 2018 10:26 pm
by Foxley
I just came up with a fairly simple script to cycle a range of colors forward.

Code: Select all

plotscript, cycle palette, begin
	variable (i, col1, col2, first, last)
	## Create a range using color indexes in the master palette.
	## This example is the first color ramp, minus index 0.
	first := 1
	last := 15
	
	col1 := get color (last)
	for (i, first, last, 2) do (
		col2 := get color (i)
		set color (i, col1)
		if (i == last) then (break) ## Interrupts an odd numbered range.
		col1 := get color (i+1)
		set color (i+1, col2)
	)
	update palette
	set timer (2, 1, 1, @cycle palette)
end
Here's a couple of examples:
Image
Image

One thing I'd like to do next is make the first and last range into script arguments, but unfortunately then using timers would become impossible since timers can't take script arguments. ( :gonk: )

Posted: Sun Jan 14, 2018 11:24 pm
by kylekrack
What's your reasoning for not using a main loop instead of timers? Just simplicity, or is there something more complicated about it that I'm not seeing?

Posted: Sun Jan 14, 2018 11:51 pm
by Foxley
Mostly simplicity. While timers can just be thrown into a game, you do need to set up a main loop properly so there'd be more of a scripting burden for people who're just doing light scripting for a RPG.

I figure I'll refactor some things and work it out for a main loop setup later.

Posted: Mon Jan 15, 2018 12:05 am
by Bob the Hamster
Timers can't take arguments, but you could use globals, and then make a wrapper script like "start pallete cycle" that takes the arguments, puts them in the globals, and starts the cycle palette timer.

Not exactly the same thing, but might be close enough :)

Posted: Mon Jan 15, 2018 1:32 am
by TMC
Very nice! I've been waiting years/decades for someone to do this :)

You can simplify your for loop like so (untested):

Code: Select all

   col1 := get color (last)
   for (i, first, last) do (
      col2 := get color (i)
      set color (i, col1)
      col1 := col2
   )

Posted: Mon Jan 15, 2018 3:02 am
by Foxley
TMC wrote:You can simplify your for loop like so (untested):

Code: Select all

   col1 := get color (last)
   for (i, first, last) do (
      col2 := get color (i)
      set color (i, col1)
      col1 := col2
   )
[s]That works... but for some reason, when I do 1,15 for the range, it's cycling index 0 as well. I had to change "first" to 2 to make it do 1-15.[/s]

Posted: Mon Jan 15, 2018 3:24 am
by Foxley
Thanks to suggestions from Feenicks and TMC, the script is shorter and can also be reversed by changing a variable from 1 to -1.

Code: Select all

plotscript, cycle palette, begin
	variable (i, col1, col2, first, last, dir)
	
	## Create a range using color indexes in the master palette.
	## If doing reverse, switch the numbers (e.g. first=15 last=1)
	first := 1
	last := 15
	dir := 1			## Direction:	1=forward	-1=reverse
	
	col1 := get color (last)
	for (i, first, last, dir) do (
      col2 := get color (i)
      set color (i, col1)
      col1 := col2
   )
	update palette
	set timer (2, 1, 1, @cycle palette)
end
Image

[s]Unfortunately the thing where it starts at 0 when you have 1 as the "first" value is still happening, not sure why.[/s]

Posted: Tue Jan 16, 2018 1:25 am
by TMC
Foxley wrote:Unfortunately the thing where it starts at 0 when you have 1 as the "first" value is still happening, not sure why.
I tested, it works fine. I think that maybe you omitted colour 0 because it's the transparent colour, and forgot about the omission.

Posted: Tue Jan 16, 2018 2:01 am
by Foxley
Sorry, you're right. I was interpreting the first/darkest color in my test ramp as 0 for no explainable reason.

Posted: Tue Jan 16, 2018 9:32 am
by guo
Cool effect!

Posted: Tue Jan 16, 2018 9:43 pm
by Foxley
I'll try to incorporate James's suggestion soon and see if I can find a way to work in arguments.

My ideal scenario: you can assign simultaneous independent palette cycles to different timers, each with their own range and forward/backward direction. I'm thinking I could just have pcyclefirst1, pcyclelast1, pcycledir1 globals, then pcyclefirst2, pcyclelast2, pcycledir2, etc. I'll try it out soon, unless someone else wants to do it first.