Spinner routine I made

Post your scripting questions / solutions here

Moderator: Moderators

Post Reply
Grassy
First Lieutenant
Posts: 221
Joined: Sun Aug 22, 2004 11:36 am

Spinner routine I made

Post by Grassy »

G'day there, I wonder if this could be optimised a little better?
I wrote a small routine to use for various gags like roulete wheels, or a random selector for a random outcome in some of our Madness maps.
It works well but was wondering if any of you more experienced scripters could have a look and it and advise me if it could be improved on.

Code: Select all

// Routine for a roulete wheel or similar
// Decreases timer, increases delay
//------------------------
spinner_gag:
//------------------------
  $trigger_spin waittill trigger
  local.player = parm.other
      
  local.spintime = (randomint(15)+5)
  local.wait = 0.2
  while ( local.wait < local.spintime )
  {
    local.inc = ( local.wait / local.spintime )
    local.wait += local.inc

    //--switch on a light here and play a short sound
    $trigger_spin playsound snd_ping
    
    wait local.wait
    
    //--switch off lights here
    local.spintime -= 0.75
  } 
  goto spinner_gag
  end
Thanks, Grassy
jv_map
Site Admin
Posts: 6521
Joined: Tue Sep 03, 2002 2:53 pm
Location: The Netherlands
Contact:

Post by jv_map »

Sounds fun, well done I think... but never use goto :wink: it makes your code hard to read. I agree with this little method it isn't much of a problem but best make it your habbit to stick with proper while loops rather than gotos.

Code: Select all

// Routine for a roulete wheel or similar
// Decreases timer, increases delay
//------------------------
spinner_gag:
//------------------------

 // you can now immediately see the code below is executed in a loop :)
 while(1)
 {

  $trigger_spin waittill trigger
  local.player = parm.other
     
  local.spintime = (randomint(15)+5)
  local.wait = 0.2
  while ( local.wait < local.spintime )
  {
    local.inc = ( local.wait / local.spintime )
    local.wait += local.inc

    //--switch on a light here and play a short sound
    $trigger_spin playsound snd_ping
   
    wait local.wait
   
    //--switch off lights here
    local.spintime -= 0.75
  }
 }
end
Other than that I think it's great :)
Image
Grassy
First Lieutenant
Posts: 221
Joined: Sun Aug 22, 2004 11:36 am

Post by Grassy »

Hey thanks JV, those comments comming from you means a great deal.
It's funny how these things come about, this one popped into my head one night while I was lying awake unable to get to sleep.. LOL

Just one thing though, wouldn't your changes cause the dreaded infinate loop error? The other thing was I thought it might be better to only loop when trigger is activated, would having while(1) cause it to loop or would it do the same thing and pause at the trigger wait command?
Grassy.
jv_map
Site Admin
Posts: 6521
Joined: Tue Sep 03, 2002 2:53 pm
Location: The Netherlands
Contact:

Post by jv_map »

No it will not cause a command overflow (infinite loop) because of the wait command in the subsequent while loop. Also the code with the while-loop I posted is exactly identical to the original code, so it will behave exactly the same too. Hence it will wait for some player to activate the trigger before something starts spinning :)
Image
Grassy
First Lieutenant
Posts: 221
Joined: Sun Aug 22, 2004 11:36 am

Post by Grassy »

Cool, thanks mate. :D
I will try not to use too many goto's in future..
Grassy
Bjarne BZR
Site Admin
Posts: 3298
Joined: Wed Feb 05, 2003 2:04 pm
Location: Sweden
Contact:

Post by Bjarne BZR »

When you use sane control statements ( anything but goto ) and indent your code in a consistent manner; it practically optimizes itself :)
OK, it does not, but it is sooooo much easier to see what is going on in you r own and others code.

The first thing I do before truing to get into code is to indent it properly. Saves a loooot of time later.
Admin .MAP Forums
Image
Head above heels.
Splaetos
Major General
Posts: 730
Joined: Tue Jan 20, 2004 2:55 pm
Contact:

Post by Splaetos »

ah the ancient arguement!

how is goto harder to read then a while(1)?

neither one has any controls on loop repitition...
same number of lines in either case...

goto within the thread should be almost immediately clear that it is re-initializing the thread. I dont think its essential to know of repitition before you know the contents of the loop, on the contrary, I think reading down... seeing each step of what is going to happen, the last step being... start over, is somewhat preferable to the precocieved notion that something to follow will be repeating forever, but of course thats all just personal preference... which is how i view the comment that while is easier to read then a goto at the end of the thread.

Ive never seen any proof that in this language goto or thread statements have any tendency to breakdown, nor do i find them harder to read(if using proper thread naming techniques i find it easier), so from my position its more of a conform or die! thing. lol j/k


course indentation is key, no doubt about that, reading code that isnt indented is an excesize in futility... I never make it to the end =) so in this case... conform or die! ok dont die... just dont expect people to read more then a couple of paragraphs of code if you cant be bothere to nest it!

oh by the way, congrats on getting site back up.
When I am king, you will be first against the wall~
Image
lizardkid
Windows Zealot
Posts: 3672
Joined: Fri Mar 19, 2004 7:16 pm
Location: Helena MT

Post by lizardkid »

well i've always wondered why programmers and scripters hate goto and yet keep supporting it and i find it useful, only thing you have to do is comment lines and make sure teh reader can understand where the focus is. :roll:
Moderator

۞
Abyssus pro sapientia
Olympus pro Ignarus
۞

AND STUFF™ © 2006
jv_map
Site Admin
Posts: 6521
Joined: Tue Sep 03, 2002 2:53 pm
Location: The Netherlands
Contact:

Post by jv_map »

In such a relatively simple method using goto doesn't really harm anything. However if you get into more complicated stuff you'll see the use of properly indented for and while loops instead of criss-crossing through the code with gotos makes your life so much easier :wink:
Image
Bjarne BZR
Site Admin
Posts: 3298
Joined: Wed Feb 05, 2003 2:04 pm
Location: Sweden
Contact:

Post by Bjarne BZR »

Goto is not evil in itself. The problem is that im most cases a goto will not help readability and very seldom improve execution speed enough to warrant its use. I only know one good place for a goto, and that is for exiting very deeply nested loops.

Code: Select all

// Do some stuff here
// Do some stuff here
// Do some stuff here
for( local.i=0;lcal.i<100;local.i++)
{
	for( local.i=0;lcal.i<100;local.i++)
	{
		for( local.i=0;lcal.i<100;local.i++)
		{
			for( local.i=0;lcal.i<100;local.i++)
			{
				for( local.i=0;lcal.i<100;local.i++)
				{
					// Condition discovered that
					// Warrants exit of all loops
					// at this point... usually
					// an exceptional error...
					if(local.shitHitFan)
					{
						goto HELL
					}
					// As the "break" statement
					// Only breaks one level
				}
			}
		}
	}
}
HELL:
// Do some stuff here
// Do some stuff here
// Do some stuff here
As the goto statement is unstructured, it is almost never clear what can be expected... in a switch, for, while statement: a clear and commonly accepted structure is used an can be expected.

Basically: Goto is almost always a bad idea, there are lots of better ways to control execution. But if you really HAVE to use goto: use thise rules to make it a bit more readable:
If you have to use a goto, remember these rules:
Rule #1 with a goto: NEVER JUMP BACKWARD. Jumping backward is the hardest code to read, you can easily replace this with another loop construct.

Rule #2 DO NOT JUMP FAR OR OUT OF THE FUNCTION! Don't jump functions or jump too far from the goto, this starts to create messy and unreadable code.

Rule #3 Make your label jump names IN ALL CAPS and make them SELF EXPLANATORY i.e. don't name them X or Y name them like EXIT_ON_BREAK:
(I did not write these, I fund them on a site and agree with them completely).
Admin .MAP Forums
Image
Head above heels.
User avatar
HDL_CinC_Dragon
Brigadier General
Posts: 574
Joined: Mon Dec 22, 2003 8:32 pm

Post by HDL_CinC_Dragon »

in all my 24 hours of experience with that type of scripting (local.name), I would say that script looks pretty good. :lol:
Image
Post Reply