Palette cycling!

Make games! Discuss those games here.

Moderators: Bob the Hamster, marionline, SDHawk

Post Reply
User avatar
Foxley
Metal Slime
Posts: 832
Joined: Sat Nov 09, 2013 5:54 pm

Palette cycling!

Post 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: )
Last edited by Foxley on Sun Jan 14, 2018 10:26 pm, edited 1 time in total.
User avatar
kylekrack
Liquid Metal Slime
Posts: 1240
Joined: Mon Jun 16, 2014 8:58 am
Location: USA
Contact:

Post 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?
My pronouns are they/them
Ps. I love my wife
User avatar
Foxley
Metal Slime
Posts: 832
Joined: Sat Nov 09, 2013 5:54 pm

Post 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.
User avatar
Bob the Hamster
Lord of the Slimes
Posts: 7658
Joined: Tue Oct 16, 2007 2:34 pm
Location: Hamster Republic (Ontario Enclave)
Contact:

Post 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 :)
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post 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
   )
Last edited by TMC on Mon Jan 15, 2018 1:34 am, edited 2 times in total.
User avatar
Foxley
Metal Slime
Posts: 832
Joined: Sat Nov 09, 2013 5:54 pm

Post 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]
Last edited by Foxley on Tue Jan 16, 2018 9:33 pm, edited 1 time in total.
User avatar
Foxley
Metal Slime
Posts: 832
Joined: Sat Nov 09, 2013 5:54 pm

Post 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]
Last edited by Foxley on Tue Jan 16, 2018 9:34 pm, edited 1 time in total.
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post 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.
User avatar
Foxley
Metal Slime
Posts: 832
Joined: Sat Nov 09, 2013 5:54 pm

Post by Foxley »

Sorry, you're right. I was interpreting the first/darkest color in my test ramp as 0 for no explainable reason.
User avatar
guo
Metal Slime
Posts: 749
Joined: Fri Dec 04, 2009 9:12 pm

Post by guo »

Cool effect!
vvight.wordpress.com
User avatar
Foxley
Metal Slime
Posts: 832
Joined: Sat Nov 09, 2013 5:54 pm

Post 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.
Last edited by Foxley on Tue Jan 16, 2018 9:46 pm, edited 1 time in total.
Post Reply