Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Very basic volume script - why does it not work?

Piggie Paule
Registered User
Join date: 22 Jul 2008
Posts: 675
08-23-2009 05:01
Firstly let me say I'm VERY new to scripting.
My mind (unfortunatly) can't seem to shake off BASIC programming I did years ago on the 8-Bit machines and the Amiga

And I want to do things like:

a=a+1
b=a*2
If b>20 then gosub (a subroutine)

To me, that makes sense.

a+=1 just does not seem to "look" sensible :)

So not only am I struggling with Syntax but also the way things work and program flow.

But anyway....................... LOL

I've been getting some basics done, learning very very slowley by editing and adjusting some very short basic scripts (albeit there are some commands I don't really know what they are doing, but I leave alone as they seem to work and be needed) ;)

Using a combination of things, I've put together the simple script below to play (loop) a sound sample, and every time you click the prim it increases the volume from 0.0 to 1.0 in 0.2 increments, tells you on screen the volume level and plays the sample loop at that volume.

Well, it all works and on screen you are told the current level, BUT the Volume is not changing.

I realise something is wrong, and yet it LOOKS like it must work to me.





float VOLUME = 0.0;

default

{
touch_start(integer n)
{
VOLUME += 0.2;
if(VOLUME > 1.0)
{
VOLUME = 0.0;
llStopSound();
llSay(0, " Sound Off ";);
}
else
{
llLoopSound("SoundSample",VOLUME);
llSay(0, " Sound Level = " + (string) VOLUME);
}
}
}
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
08-23-2009 05:38
You can say "volume = volume + 0.2", the "+=" operator is a shorthand.
_____________________
Argent Stonecutter - http://globalcausalityviolation.blogspot.com/

"And now I'm going to show you something really cool."

Skyhook Station - http://xrl.us/skyhook23
Coonspiracy Store - http://xrl.us/coonstore
Piggie Paule
Registered User
Join date: 22 Jul 2008
Posts: 675
08-23-2009 05:47
From: Argent Stonecutter
You can say "volume = volume + 0.2", the "+=" operator is a shorthand.


Thanks.

I know it's worse coming from BASIC into LSL than perhaps just starting with LSL

With a bit of guesswork, and afterwards following it up in Wiki, it looks like I've fixed my problem.

It's (as you all know, but I didn't) you can't change the volume on the fly like that, but have to stop the sound before issuing a new volume command

So I had to put the line: llStopSound();
before the line that sets the volume level.

:)
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
08-23-2009 08:57
From: Piggie Paule
I know it's worse coming from BASIC into LSL than perhaps just starting with LSL
Basic is not the best introduction to programming. You would have been better off with Lisp, Forth, or even assembly language. Though I'm nowhere near as down on BASIC as some. The famous mathematician Edgar Dijsktra, for example:

From: Edgar Dijkstra
* The teaching of BASIC should be rated as a criminal offense: it mutilates the mind beyond recovery.

* It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration.
_____________________
Argent Stonecutter - http://globalcausalityviolation.blogspot.com/

"And now I'm going to show you something really cool."

Skyhook Station - http://xrl.us/skyhook23
Coonspiracy Store - http://xrl.us/coonstore
Piggie Paule
Registered User
Join date: 22 Jul 2008
Posts: 675
08-23-2009 10:01
From: Argent Stonecutter
Basic is not the best introduction to programming. You would have been better off with Lisp, Forth, or even assembly language. Though I'm nowhere near as down on BASIC as some. The famous mathematician Edgar Dijsktra, for example:


Oh well, looks like I'm doomed for all eternity then! LOL

I Learned to "get by" in BASIC and after that, when I looked at any other language I used to think, why have they made this "other" language so deliberatly difficult to understand.

And I guess (even after years and years of nor programming) I still think I suffer from this view.

I'm doomed I tell's ya, I'm doomed..........

Well, I still think I'm better off than someone who have never programmed (in some ways)

At least I know about variable and commands that do things.

We've just lost the line numbers, thrown in a shed load of semi colons and brackets here and there, and made things happen on events rather than running all the time.

It's just some of the ways maths and variables interact with each other that throws me and it may just be cos I have to type it the other way round than I'm used to.

(And don't even start me on Reverse Polish Notation, cos I ain't going there!)

Main thing I'd like to know right now is the very basics about how to get the blue dialog box to pass a bunch of variables (set when you click on a button) to specific prims in a grouped set.

Oh, and while I'm puzzling that one, here's a photo of one of my Ferrets for you:

Sindy Tsure
Will script for shoes
Join date: 18 Sep 2006
Posts: 4,103
08-23-2009 10:07
From: Piggie Paule
I'm doomed I tell's ya, I'm doomed..........

LOL.. Not even close.. Anybody who thinks that somebody who started with BASIC is beyond being taught something else is making excuses.

Just take your time and you'll do fine.
_____________________
Sick of sims locking up every time somebody TPs in? Vote for SVC-3895!!!
- Go here: https://jira.secondlife.com/browse/SVC-3895
- If you see "if you were logged in.." on the left, click it and log in
- Click the "Vote for it" link on the left
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
08-23-2009 10:11
From: Piggie Paule

We've just lost the line numbers, thrown in a shed load of semi colons and brackets here and there, and made things happen on events rather than running all the time.
There are much deeper differences between Basic and C-derived languages like LSL. The biggest is that Basic is "statement-oriented" and C-derived languages are "expression-oriented".

For example:

In Basic "A = A + 1" is an abbreviation of "LET A = A + 1", the "LET" statement.
In C-derived languages, "A = A + 1" is an expression, there's nothing fundamentally different between the "=" operator and the "+" operator. You can write "l = [a=a * 2, b = b / 2];" to simultaneously create a list and modify the variables the list is being created from.

It's a shame this forum doesn't allow inline images, but that's a very alert looking critter.

Back to the topic, one might say that treating different languages as being the same kind of thing with the bits and pieces moved about a bit, well, that's like treating a ferret as a rat, or a cat. :)
_____________________
Argent Stonecutter - http://globalcausalityviolation.blogspot.com/

"And now I'm going to show you something really cool."

Skyhook Station - http://xrl.us/skyhook23
Coonspiracy Store - http://xrl.us/coonstore
Piggie Paule
Registered User
Join date: 22 Jul 2008
Posts: 675
08-23-2009 10:23
From: Argent Stonecutter

Back to the topic, one might say that treating different languages as being the same kind of thing with the bits and pieces moved about a bit, well, that's like treating a ferret as a rat, or a cat. :)


Well, they all get born, start of cute, grown to different sizes and colours but all eat and poo and need cleaning. (as do we all!)

I did look at C (or C+) many years ago when the 1st C languages came out but they looked weird so I never made any headway (to be honest, I never had any reason to make any headway)

SL is giving me a reason as I want to make some basic things work.

Just finding a simple demo script, editing it, adding bits I know into it, and taking bits I don't want out from it, is the most useful way I learn things.

That said if I was in a 1 on 1 class in real life with a understanding programmer it would make life so much easier to GRASP principles that others "who know" take for granted.

I'm not even wanting to code the latest super defence/attack HUD, just the basics of moving some prims, changing colours and parameters & the use of menus would make me happy right now :)

I know more now than I did a week ago, so that's progress
Talarus Luan
Ancient Archaean Dragon
Join date: 18 Mar 2006
Posts: 4,831
08-23-2009 11:09
Pascal is also a very good starting language. The latest iterations have all the power of C/C++ with fewer of the "gotchas" for learning.

http://www.freepascal.org
http://www.codegear.com/en/products/delphi
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
08-23-2009 11:20
Pascal's problem is that it's also statement-oriented, so it's going to leave Piggy just as confused when something like Strife's list optimization hits. :D
_____________________
Argent Stonecutter - http://globalcausalityviolation.blogspot.com/

"And now I'm going to show you something really cool."

Skyhook Station - http://xrl.us/skyhook23
Coonspiracy Store - http://xrl.us/coonstore
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
08-23-2009 11:21
From: Piggie Paule
Well, they all get born, start of cute, grown to different sizes and colours but all eat and poo and need cleaning. (as do we all!)
Luckily, cats are self-cleaning.

If you'd ever bathed a cat you'd understand. :D
_____________________
Argent Stonecutter - http://globalcausalityviolation.blogspot.com/

"And now I'm going to show you something really cool."

Skyhook Station - http://xrl.us/skyhook23
Coonspiracy Store - http://xrl.us/coonstore
Rolig Loon
Not as dumb as I look
Join date: 22 Mar 2007
Posts: 2,482
08-23-2009 14:21
Hmmmm... Over the years, I've gone from Fortran II to Fortran IV through BASIC and a bunch of odd languages they used to use on DEC machines into C and out again, and now into LSL. They're each a puzzle. Just as with learning languages, you can find similarities that sometimes help you learn the next one. If you fall into the trap of thinking of any language purely from the viewpoint of its syntax and vocabulary, however, you'll get all messed up. You have to stand back and understand the language's world view before it starts to make sense. The best way I've found is to deconstruct scripts that other, more experienced people have written.
_____________________
It's hard to tell gender from names around here but if you care, Rolig = she. And I exist only in SL, so don't ask.... ;)

Look for my work in XStreetSL at
DoteDote Edison
Thinks Too Much
Join date: 6 Jun 2004
Posts: 790
08-23-2009 20:17
From: Piggie Paule
It's (as you all know, but I didn't) you can't change the volume on the fly like that, but have to stop the sound before issuing a new volume command
Actually, you can. Use llAdjustSoundVolume(float volume);
http://lslwiki.net/lslwiki/wakka.php?wakka=llAdjustSoundVolume
Talarus Luan
Ancient Archaean Dragon
Join date: 18 Mar 2006
Posts: 4,831
08-23-2009 21:20
From: Argent Stonecutter
Pascal's problem is that it's also statement-oriented, so it's going to leave Piggy just as confused when something like Strife's list optimization hits. :D


You say that like it is a BAD thing. :p
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
08-24-2009 08:12
From: Talarus Luan
You say that like it is a BAD thing. :p
Yes, I do. Strife's list optimization hack is not complicated. Google for "Duffs Device" some time.
_____________________
Argent Stonecutter - http://globalcausalityviolation.blogspot.com/

"And now I'm going to show you something really cool."

Skyhook Station - http://xrl.us/skyhook23
Coonspiracy Store - http://xrl.us/coonstore
Talarus Luan
Ancient Archaean Dragon
Join date: 18 Mar 2006
Posts: 4,831
08-25-2009 01:26
From: Argent Stonecutter
Yes, I do. Strife's list optimization hack is not complicated. Google for "Duffs Device" some time.


Trust me, statement-based languages for learning AND use are NOT a bad thing. Expression-based ones make for some REALLY nasty code. Strife's hacks may be cute and fun to us, but they are dense and mostly impenetrable for many people. With respect to LSL and systems that MUST have those optimizations in the human-readable source, it *may* be a necessary evil, but I still avoid it anytime/anywhere I can. It's also nice to have a language which makes such an effort worthwhile.

I'll take Object Pascal/Delphi ANY day over C/C++/C#, thanks.
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
08-25-2009 02:23
From: Talarus Luan
Trust me, statement-based languages for learning AND use are NOT a bad thing.
Since LSL *is* an expression-based language, so the option of "not using an expression-based language" isn't there, what's your point?
_____________________
Argent Stonecutter - http://globalcausalityviolation.blogspot.com/

"And now I'm going to show you something really cool."

Skyhook Station - http://xrl.us/skyhook23
Coonspiracy Store - http://xrl.us/coonstore
Piggie Paule
Registered User
Join date: 22 Jul 2008
Posts: 675
08-25-2009 03:10
Interesting the posts here (did I stir up something) :)

As I freely admit, I "was" a BASIC person and that's how my mind kinda saw problems.

Then IF something THEN do something.

The dreaded GOTO LINE 50

And the GOSUB and RETURN

I'm still having a few (basic) issues with loops/decision making.

I never really used things like WHILE and ELSE in Basic although I know these things were added later.

Just yesterday I could not work out why something was not running, as the conditions in the statement seemed to have been right, but the code was not jumping to that part.

It's the IF and ELSE that was catching me out, and I had to reverse some bits and change the order to get it to work.

All VERY simple stuff I know (hangs head in shame)

Feel I need some learning to do on the Commands and Logic and Order of things, as well as the functions that are specific to SL.
Helium Loon
Registered User
Join date: 27 Jul 2007
Posts: 2
08-25-2009 08:15
From: Piggie Paule
Interesting the posts here (did I stir up something) :)

As I freely admit, I "was" a BASIC person and that's how my mind kinda saw problems.


Nothing wrong with "statement oriented languages, as ALL programming languages are compiled into statement oriented code....namely machine code (or bytecode, which is very similar, though interpreted). Machine code is low-level assembler bytecode, effectively.

From: Piggie Paule

Then IF something THEN do something.

The dreaded GOTO LINE 50

And the GOSUB and RETURN


The equivalents in psuedo-assembler:

IF/THEN becomes:
LoadRegisterA <- value1
LoadRegisterB <- value2
Compare A,B
BranchEQ offset (or BranchNE, or BranchGT, or BranchLT, or BranchGTE, or BranchLTE,...)

GOTO becomes:
Jump address

GOSUB becomes:
JumpSubroutine address

Programmers forget that all their high-level code has to be compiled into machine code. And that machine code uses absolute jumps (GOTOs) and lots of other "bad programming" techniques during optimization.

Don't rag on GOTOs. There is NOTHING wrong with using them. It's MIS-using them that is bad practice.

BASIC is not a bad language. It is actually very close to some of the 8-bit and 12-bit assembly that was prevalent during its development. As noted above, it was pretty fast to interpret, and line-numbers provided an EASY mechanism that translated almost directly to addresses in memory (since statements would be effectively fixed-length in machine code for each type of statement.)

From: someone

I'm still having a few (basic) issues with loops/decision making.

I never really used things like WHILE and ELSE in Basic although I know these things were added later.

Just yesterday I could not work out why something was not running, as the conditions in the statement seemed to have been right, but the code was not jumping to that part.

It's the IF and ELSE that was catching me out, and I had to reverse some bits and change the order to get it to work.


WHILE and ELSE were present in basic very early on. And don't bang on yourself for messing up condition tests......even experienced programmers mess them up regularly. That's what debugging is all about.

From: someone

All VERY simple stuff I know (hangs head in shame)

Feel I need some learning to do on the Commands and Logic and Order of things, as well as the functions that are specific to SL.


No, not simple stuff. People that have been doing it for decades view it that way, simply from constant exposure and usage. Toss them into a language they aren't familiar with, and they'll make the same mistakes too. I love taking the average high-level programmer and asking them to code something in assembler. It's almost funny to watch how they the craziest gyrations of code to try to make it more like the code they are used to writing, despite it actually being more flexible, efficient, and faster to just USE the abilities of that language.

I remember the first time I had to write some low-level code professionally....it was in an embedded system, it had to be fast, reliable, and near-bulletproof. And I did it. And horrified another programmer on the project.....my code was self-modifying! Horrors! In his mind, it was blasphemous. However, I had debugged the code thoroughly.....it worked correctly, handled all edge-conditions, AND was fast. Very fast. 3.4 TIMES faster than any code my fellow programmer could write 'correctly'.

Computer languages should not be considered idioms.....they are TOOLS. Programming is the skill. Learn about PROGRAMMING. Not "Functional Programming", or "Object Oriented Programming", or any other particular TYPE of programming. Learn and master the fundamentals. Then all the other idioms are easily switched between as needed. Languages simply become a set of keywords and syntactical sugar to do all the same things.

And have FUN with it!
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
08-25-2009 08:32
I think describing machine code as statement oriented is misleading. For example, this is one form of assembly language for a processor I once used:

create (0bran) ;code sp @+ 0= if, sp @+ ip mov, else, sp inc, then, next,
_____________________
Argent Stonecutter - http://globalcausalityviolation.blogspot.com/

"And now I'm going to show you something really cool."

Skyhook Station - http://xrl.us/skyhook23
Coonspiracy Store - http://xrl.us/coonstore
Piggie Paule
Registered User
Join date: 22 Jul 2008
Posts: 675
08-25-2009 08:50
10 // SL world programming
20
30 Do current features work correctly?
40 Check features
50 Bugs have found to exist in current features
60 Goto 80
70 Fix current features
80 Add more features
90 Goto 30

;)
Talarus Luan
Ancient Archaean Dragon
Join date: 18 Mar 2006
Posts: 4,831
08-25-2009 11:22
From: Argent Stonecutter
Since LSL *is* an expression-based language, so the option of "not using an expression-based language" isn't there, what's your point?


LSL is not ONLY "expression-based". You can quite easily write statement-only code in it.

In fact, I daresay the only reason to write expression-oriented code is for obfuscation, since quite often, it doesn't result in appreciable increases in speed or efficiency. Even in the documented cases where it is with LSL, I still avoid using them unless I absolutely have to, which I think is the best policy. It results both in more readable and more maintainable code.

As for Duff's Device, while it may be novel, I think I would rather use an assembly-language library routine. While arguably less portable (which I don't think Duff's Device is really all that portable, or even necessary), it definitely would have been more readable and maintainable.

That said, I have no doubt that Strife is more than capable of winning the IOCCC, if he bothered to enter. :p
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
08-25-2009 11:32
From: Talarus Luan
LSL is not ONLY "expression-based". You can quite easily write statement-only code in it.
Not if you want to work with code written by other people who don't so constrain themselves.

I keep running into people who say "you don't need to deal with the complexity of Perl/C++/Common Lisp/My Favorite Complicated Language if you don't want to". No, you don't, if you never want to collaborate with anyone else, use published examples, modify open source code, or in any way interact with anyone else using the language.

I'm not saying ANYTHING here OR in the message you replied to about whether it's "better" or "worse", I'm simply saying "this is how the language works, you're going to be better off if you understand it".
_____________________
Argent Stonecutter - http://globalcausalityviolation.blogspot.com/

"And now I'm going to show you something really cool."

Skyhook Station - http://xrl.us/skyhook23
Coonspiracy Store - http://xrl.us/coonstore
Talarus Luan
Ancient Archaean Dragon
Join date: 18 Mar 2006
Posts: 4,831
08-25-2009 12:52
From: Argent Stonecutter
Not if you want to work with code written by other people who don't so constrain themselves.


Oh, I work with it all the time. I end up de-obfuscating all manner of crap that people turn out, once they get tired of (not) supporting it, and their customer calls someone else in (me) to clean up the mess they left behind. Sometimes, I have to just throw out the tangled rats' nest and refactor it from scratch.

Just because you can do a thing doesn't mean that you SHOULD do a thing. This is something that a lot of programmers should learn in Comp Sci 101. Languages are tools, and if you operate them with wild abandon, you're going to get burned at some point (or, worse, your company/customer).

From: someone
I keep running into people who say "you don't need to deal with the complexity of Perl/C++/Common Lisp/My Favorite Complicated Language if you don't want to". No, you don't, if you never want to collaborate with anyone else, use published examples, modify open source code, or in any way interact with anyone else using the language.


That's hardly the case. There are plenty of projects out there, both open and closed source, public and private, which adhere to good coding practices which don't require you to deal with obfuscation to extreme levels. So, you don't HAVE to, but if you are going to end up working on projects like I do for a living, then you will HAVE to at some point. Either that, or you're going to learn it the hard way.

Even still, in many cases, experience is the ONLY teacher, with what some people do to torture source code into squeezing out something in the name of efficiency or "coolness factor" (the later of which I sometimes would like to rend to ribbons, when they do it for no other reason than because they could, in a customer's project).

From: someone
I'm not saying ANYTHING here OR in the message you replied to about whether it's "better" or "worse", I'm simply saying "this is how the language works, you're going to be better off if you understand it".


Actually, I only suggested it as a STARTING LANGUAGE, and you pointed out problems with it being "statement oriented", and thus, worse. Every language has its nuances, and I will be the first one to say you can write some pretty nasty, obfuscated crap in Object Pascal, too. It's just a lot harder, and rarely necessary, so the whole point with doing it is moot.

Starting out learning any language is not going to prepare you adequately for idiosyncrasies and obfuscations in it, or in any other. Those you will simply have to learn by exposure and experience. Hell, many of Strife's optimizations *I* have to study and research for a bit before I can understand them, but that's because they are particular to LSL, and are present to torture the compiler into doing something unintentional that it couldn't normally do, or to get it to do something more efficiently. It has nothing specifically to do with statement vs expression, it is just the way it had to be done to get what he wanted.
Argent Stonecutter
Emergency Mustelid
Join date: 20 Sep 2005
Posts: 20,263
08-25-2009 14:25
From: Talarus Luan
Oh, I work with it all the time. I end up de-obfuscating all manner of crap that people turn out, once they get tired of (not) supporting it, and their customer calls someone else in (me) to clean up the mess they left behind. Sometimes, I have to just throw out the tangled rats' nest and refactor it from scratch.
Been there, done that, and it doesn't matter whether they were trained on C or Basic. But if you don't understand LSL you won't be able to do that.

From: someone
That's hardly the case. There are plenty of projects out there, both open and closed source, public and private, which adhere to good coding practices which don't require you to deal with obfuscation to extreme levels.
I'm not referring to "extreme levels of obfuscation". I'm referring to normal idioms of the language, such as:

for(i = 0; i < 10; i++) { // Look! Statements used as expressions!
...
}

And why you would use that rather than:

i = 0;
while(i < 10) {
...;
i++;
}

And there are good reasons for using both to produce the most readable and understandable code for the specific instance.

From: someone
Actually, I only suggested it as a STARTING LANGUAGE, and you pointed out problems with it being "statement oriented", and thus, worse.


Not "worse". I didn't say that it was "worse", I said that it was not the best training for someone who is going to be programming in LSL.

I brought up Strife's code because those are things that the user WILL have to deal with, because LOTS of people use that code as their guides, not because it's the kind of code I would recommend writing.
_____________________
Argent Stonecutter - http://globalcausalityviolation.blogspot.com/

"And now I'm going to show you something really cool."

Skyhook Station - http://xrl.us/skyhook23
Coonspiracy Store - http://xrl.us/coonstore
1 2