Weird GUI Glitch with Tween module and Lightweight BASS Template

Started by Tyshalle, Thu 14/11/2013 06:19:43

Previous topic - Next topic

Tyshalle

Running into a very bizarre situation. I was pointed in the direction of the Lightweight BASS Template since I wanted an inventory bar and for the game to work with two clicks. I installed that and it's been working great. Earlier tonight I was looking for a way to have the Inventory bar slide in and out instead of simply appearing and disappearing. I'll admit that Tween seems pretty far above my head right now, but after a lot of messing around with it I've started to make headway.

The TwoClickHandler.asc from the BASS template had a pretty simple setup for making the inventory bar show up:

Code: ags

function repeatedly_execute()
{
	if (!gInventoryBar.Visible && mouse.y <= INVENTORY_POPUP_POSITION)
	{
		gInventoryBar.Visible = true;
	}
	
	if (gInventoryBar.Visible && mouse.y >= gInventoryBar.Height)
	{
    gInventoryBar.Visible = false;
	}
}



Pretty simple. I added to it just a little:

Code: ags

function repeatedly_execute()
{
	if (!gInventoryBar.Visible && mouse.y <= INVENTORY_POPUP_POSITION)
	{
		gInventoryBar.Visible = true;
    gInventoryBar.TweenPosition(0.5, 10, 0, eEaseOutTween, eNoBlockTween);
	}
	
	if (gInventoryBar.Visible && mouse.y >= gInventoryBar.Height)
	{
    gInventoryBar.TweenPosition(0.5, 10, -30, eEaseInTween, eNoBlockTween);
    gInventoryBar.Visible = false;
	}
}


In theory, this should have the bar slide in and out of position instead of simply having its visibility turned on and off. However, when I run this, it slides into view like it should, but instead of sliding back out of view it pops out of view. I suspect it's because it's changing it's visibility to false before it has a chance to fully move out of position. This seems confirmed when I put in a Wait(20) script in between .TweenPosition and .Visible. It does exactly what it should be, which would be fine if it weren't for the fact that I'd hate to force the player to wait half a second every time he leaves his inventory.

So I tried setting a timer, and the bar slid out like it was supposed to, but when I moved the mouse down to close it, I got kicked out of the game, and it sent me to the Tween.asc under function _AssertTrue(bool statement, String errorMessage) and highlighted AbortGame(errorMessage), and I got the following error: "Cannot create new tween because the Tween module is currently playing 16 tween(s), which is the maximum. You can increase this max number on the Tween module script header."

This doesn't seem right, as this is the only thing I've used the Tween module for so far. Unless it comes with 16 other Tweens automatically running, I don't get how that could be possible. I increased the Tween count to 20, and got the same message. I increased it to 30, and now I don't get the message, but instead I get a HUGE slowdown upon it sliding close. And then, weirdly, the inventory bar won't slide back out again after that.

I've messed with this a lot trying to work it out. I'm not sure if it's Tween's fault or the BASS Template or something else entirely. It sounded to me like it might have been something that repeated and got caught up by the gInventoryBar.Height line, so I changed that to mouse.y >= 40 and it still caused the same exact issue.

Does anyone have any thoughts on what could be causing this, and what might fix it?

Scavenger

Code: ags

function repeatedly_execute()
{
	if (!gInventoryBar.Visible && mouse.y <= INVENTORY_POPUP_POSITION)
	{
		gInventoryBar.Visible = true;
                if (gInventoryBar.Y == -30) gInventoryBar.TweenPosition(0.5, 10, 0, eEaseOutTween, eNoBlockTween); //This only needs to be done once.
	}
	
	else if (gInventoryBar.Visible && mouse.y >= gInventoryBar.Height)
	{
             if (gInventoryBar.Y == 0) gInventoryBar.TweenPosition(0.5, 10, -30, eEaseInTween, eNoBlockTween); //The tween will only happen when the Inventory is fully out.
             if (gInventoryBar.Y == -30) gInventoryBar.Visible = false; //Only remove it when it's done tweening, or it'll be gone instantly.
	}
}


repeatedly_execute will runs functions once per loop, so every time those conditions are satisfied you'll get one new Tween a loop. You need to make sure you are in control of when a tween happens to avoid this.

Khris

Yes, that should work, assuming that the tween module will change the GUI position by at least one pixel right at the start of the tween.
The safest way is to introduce a state variable:

Code: ags
int inv_state = 0;  // 0: up, 1: sliding down, 2: down, 3: sliding up
function repeatedly_execute()
{
        // finished sliding?
        if (inv_state == 1 && gInventoryBar.Y == 0) inv_state = 2;
        if (inv_state == 3 && gInventoryBar.Y == -30) inv_state = 0;

        if (inv_state == 0 && mouse.y <= INVENTORY_POPUP_POSITION)
        {
                inv_state = 1;
                gInventoryBar.TweenPosition(0.5, 10, 0, eEaseOutTween, eNoBlockTween);
        }
        else if (inv_state == 2 && mouse.y >= gInventoryBar.Height)
        {
                inv_state = 3;
                if (gInventoryBar.Y == 0) gInventoryBar.TweenPosition(0.5, 10, -30, eEaseInTween, eNoBlockTween);
        }
}

Setting the GUI to visible/invisible should be no longer necessary, btw.

Edit: yes, I had X instead of Y, code corrected

Tyshalle

Very cool. Both methods worked, though I noticed in the first method there was a little bit of lag/slowdown/stuttering when it came down or went up. I had to change a few attributes in the second way, but when it worked, it worked very well.

Thank you both a ton!

SMF spam blocked by CleanTalk