Script Reloading Improperly?

Ask and answer questions about making games and related topics. Unrelated topics go in that other forum.

Moderators: marionline, SDHawk

Post Reply
User avatar
kylekrack
Liquid Metal Slime
Posts: 1242
Joined: Mon Jun 16, 2014 8:58 am
Location: USA
Contact:

Script Reloading Improperly?

Post by kylekrack »

I've got a script that brings down a menu tab to select other submenus. It works for the most part, except that after the first time the menu is used, the set sprite palette function stops working. The first time you open the menu, the icon will change palettes to show that it is selected according to which menu item is selected. But after you close the menu and reopen it, the palettes don't change. The menu still functions normally otherwise, but the palettes don't change. Is there some local variable that screws everything up when recalled or what?

Code: Select all

script, select menu, begin
	suspend player
	variable(running,i)
	move slice to(tab,0,0,8) #brings down GUI/"tab"
	wait(8)
	resume player
		open menu(1) #invisible menu
		busy := true
	
		running := true
	
		while(running) do, begin
			nullify palettes #makes all icons gray or "unselected"
			i := selected menu item(top menu)
			set sprite palette(slice child(tabicons,i--1),3) #makes icon "selected" with corresponding menu item
			show value(0) #ignore this
			if(key is pressed(key:space) || key is pressed(key:enter)) then(
				use menu item(i) #using the menu items
			)
			if(key is pressed(key:c)) then(
				close menu(top menu) #closing the menu (duh)
			)
			if(menu is open(top menu) == false) then(
				running := false #break from the loop
				show value(2)
			)
			wait(1)
		end
		
	move slice to(tab,0,-29,8) #move "tab" back up
	wait(8)
	show value(1)
	busy := false
end
My pronouns are they/them
Ps. I love my wife
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

The problem is that "selected menu item" returns a menu item handle, not the position of the item in the menu. Your script works the first time just because menu item handles are assigned counting up from zero (in hindsight this was a mistake), but the second time they won't start from zero. Write:

Code: Select all

i := menu item slot(selected menu item(top menu))
(You might also want to look at "menu item true slot".)
Also, what happens if i == 0? Then you call slicechild with an invalid argument.
Last edited by TMC on Thu Sep 10, 2015 2:17 pm, edited 1 time in total.
User avatar
kylekrack
Liquid Metal Slime
Posts: 1242
Joined: Mon Jun 16, 2014 8:58 am
Location: USA
Contact:

Post by kylekrack »

I believe select menu item sorts from 1 instead of 0. I can't find where I read that or if that's true, but I'm fairly certain it is because to that extent, it works. I may have tried using i instead of i -- 1 initially, but I can't remember. Now with the menu item slot added in, you're right, the order is messed up.

Thank you again, you're a life saver, like always :)

EDIT: SWEET HEVEANS IT FINALLY WORKS.
Last edited by kylekrack on Thu Sep 10, 2015 5:26 pm, edited 1 time in total.
My pronouns are they/them
Ps. I love my wife
TMC
Metal King Slime
Posts: 4308
Joined: Sun Apr 10, 2011 9:19 am

Post by TMC »

Oh, menu item handles actually count from 1, not 0. Naturally, since 0 is reserved to mean an invalid handle/no item. So you misinterpreted this to mean that menu item slots are counted from 1. Of course you should never make assumptions about the value of any kind of handle.

I recommend always going back and re-reading the plotscripting dictionary for the relevant commands when something isn't working and you're not absolutely sure about how a command works. Practically all the answers are there. I don't actually know everything off the top of my head, I typically look up things in the dictionary when someone asks for help with a script.
Last edited by TMC on Fri Sep 11, 2015 5:08 am, edited 2 times in total.
Post Reply