PDA

View Full Version : Math Error



eric_darling
11-14-2003, 06:47 PM
I'm having a problem with some simple math in AMS 5. I've enclosed my code that shows the problem below - with the responsible lines highlighted in red.

This is code for a Volume Up button. The problem is that this whole shebang works great until I get the volume of the video under 5. If I get down to 4 (using the volume down button), I cannot get the button to increment the volume up anymore. Once I get to zero, that's where I am forced to stay.

It seems buggish to me, since there's no apparently reasonable explanation for this behavior. What is of particular interest is that the reverse of this situation is not true once I get the volume up to 96.

I guess I could just drop the nice volume tethers at the top and bottom because of this issue, but I'd like an explanation regardless.

Anyone have a suggestion for something I'm doing wrong?

The first part of the code that checks the status of the video is implemented to deactivate button sounds while the video is playing.



IsPlaying = Video.GetState("Video1");
if (IsPlaying == 0 or IsPlaying == 1) then
Audio.Load(CHANNEL_EFFECTS, "AutoPlay\\Audio\\Click1.ogg", true, false);
end

v = Video.GetVolume("Video1");

if (v < 95) and (v > 4) then
Video.SetVolume("Video1", v + 5);
end

if (v >= 95) and (v < 100) then
Video.SetVolume("Video1", v + 1);
end

if (v <= 4) then
Video.SetVolume("Video1", v + 1);
end

Paragraph.SetText("volumetext", v);

Incidentally, the Paragraph.SetText command is for a paragraph object where I report the current volume based on the 'v' variable.

Lorne
11-14-2003, 11:22 PM
What's in your volume down button?

eric_darling
11-15-2003, 05:28 AM
This is the code from Volume Down:

IsPlaying = Video.GetState("Video1");
if (IsPlaying == 0 or IsPlaying == 1) then
Audio.Load(CHANNEL_EFFECTS, "AutoPlay\\Audio\\Click1.ogg", true, false);
end

v = Video.GetVolume("Video1");

if (v > 5) and (v < 96) then
Video.SetVolume("Video1", v - 5);
end

if (v <= 5 and v > 0) then
Video.SetVolume("Video1", v - 1);
end

if (v >= 96) then
Video.SetVolume("Video1", v - 1);
end

Paragraph.SetText("volumetext", v);
Thanks for taking a look, Lorne.

Corey
11-15-2003, 09:35 AM
Hi. I think there's a more easy/streamlined method, but I don't understand why the increment value changes need to change on either end of the scale? What exactly are you trying to create Eric?

Corey Milner
Creative Director, Indigo Rose Software (http://www.indigorose.com)

eric_darling
11-15-2003, 11:11 AM
Well, like I said, it's not mandatory for the project. I was just testing some scripting out in the new engine. I must say, it's more powerful and streamlined by nature. I think I'm really going to come around with it soon.

I originally thought it would be nice to have smaller increments in volume adjustment at the top and bottom of the range (top and bottom 5 numbers).

But I was asking out of interest for why it isn't working as expected. Volume Up works as it should. Why not volume down?

And, of course, I'd love to have a more streamlined method. Other suggestions for better implementation are always welcome. I've not thought about it from more than just the "how do I do it this way" perspective.

Corey
11-15-2003, 12:12 PM
I know, but I'm lousy at fixing code so it's just more time efficient for me to build you a working example. Attached is a working example which may or may not be simpler but is fairly lean, maybe Lorne can shed some light on the former. Off hand though I do notice you didn't put in any absolute limits, i.e.

if (volume > 100) then
volume = 100;
end

and

if (volume < 0) then
volume = 0;
end

But I don't know if this is the only issue or not... BTW if you are using this on different pages with different buttons, don't forget it can be defined as a global function. For example if you had several items in your project which act as 0-100 value counters you can define it as a function and then you can just fire that function whenever neccesary... Simplifies code and keeps things efficient.

Corey Milner
Creative Director, Indigo Rose Software (http://www.indigorose.com)

eric_darling
11-15-2003, 03:09 PM
Oh yeah... Functions. I forgot all about that. Thanks for the reminder! My head is still so, er... 4.0. :)

I didn't realize absolute limits were necessary since the volume couldn't be set outside the 0-100 range. Thanks for that as well. Will test and report back.

Corey
11-15-2003, 03:19 PM
Well in a sense you are right, you can't exceed the system limits for sure but it's good practice to manage your values but also it's mostly for the display value than anything. Although if you don't add limits, and your user clicks past zero a few times they would have to click back the same amount of times to get back to zero since the variable got clicked down to -4 or whatever. In the example I didn't add any actual volume control, but that would be easy enough with a single action added to the end of each button's actions. I was more hung up on making it just display the values here so you could see it works...

Corey Milner
Creative Director, Indigo Rose Software (http://www.indigorose.com)

eric_darling
11-16-2003, 12:29 AM
Actually adding the limit commands did not affect the functionality (or dysfunctionality) of the scripts. In fact, it didn't affect their performance in any way.

So, I got to thinking about why this was happnin...

Turns out, AMS was referencing the first gathered variable 'v' for the current volume, where really, that value was just outdated by an adjustment to the volume on the video in the interim.

So, I simply added a second variable, 'v2' which does exactly the same thing, but it appears in the scripts after the volume adjustments occur (which I've now just simplified to blocks of 5 for both up and down). So, the setting of the paragraph text actually submits the 'v2' variable instead. Works like a charm.

This whole discussion has really got me thinking about how I code, though. The reminder about functions was crucial to me getting better at this kind of stuff - thanks, Corey!

Snippet of working code FYI:


IsPlaying = Video.GetState("Video1");
if (IsPlaying == 0 or IsPlaying == 1) then
Audio.Load(CHANNEL_EFFECTS, "AutoPlay\\Audio\\Click1.ogg", true, false);
end

v = Video.GetVolume("Video1");

if (v <= 95) then
Video.SetVolume("Video1", v + 5);
end

v2 = Video.GetVolume("Video1");
Paragraph.SetText("volumetext", v2);

Corey
11-16-2003, 12:31 AM
I try not to use 2 variables when 1 would do but I'm, "funny that way", :) it's no big deal either way on small to mid-size projects as long as you are able to read your own code quickly that's all that matters.

Corey Milner
Creative Director, Indigo Rose Software (http://www.indigorose.com)