Helpful Script Examples

Forum Archive - from http://theuniversal.net
Locked
User avatar
zaroba
World Owner
World Owner
Posts: 7257
Joined: Fri Oct 10, 2003 11:06 pm
Location: Hereford, PA
Contact:

Helpful Script Examples

Post by zaroba »

The IF statement is likely the most commonly used script element. It can be used in many different ways and combined with other script elements with ease. The following are many examples displaying various methods of using it. These script pieces all work and can be copied into your world to see their output and how they work.

If anybody has any other suggestions of script pieces to make examples for, or would like ot see how something is used, feel free to post a request for it.


IF ELSE statement
Perhaps the most common use of the IF statement. Due to containing the ESLE statement the script will perform whatever IF action applies and then stop. If no IF statements apply, it performs the ELSE statement.

Code: Select all

Event ("&command","test")
	{
	$TempVar = sysRand(6)
	If ( $TempVar = 1 )
		{
		*say TempVar equals one
		}
	Else If ( $TempVar = 2 )
		{
		*say TempVar equals two.
		}
	Else If ( $TempVar = 3 )
		{
		*say TempVar equals three.
		}
	Else If ( $TempVar = 4 )
		{
		*say TempVar equals four.
		}
	Else If ( $TempVar = 5 )
		{
		*say TempVar equals five.
		}
	Else
		{
		*say TempVar equals six.
		}
	}

IF statement - single output
The ELSE statement is somewhat optional. This script is the same as the above, but without the ELSE. Excluding the ELSE statement means you can add additional stuff after the IF statements.

Code: Select all

Event ("&command","test")
	{
	$TempVar = sysRand(5)
	If ( $TempVar = 1 )
		{
		*say one
		}
	If ( $TempVar = 2 )
		{
		*say two.
		}
	If ( $TempVar = 3 )
		{
		*say three.
		}
	If ( $TempVar = 4 )
		{
		*say four.
		}
	If ( $TempVar = 5 )
		{
		*say five.
		}
	*say complete
	}

IF statement - multiple output
Also similar to the above two, by excluding the ELSE statement this script shows how multiple IFs can be used and applied without the need to nest them or use ELSE statements. It also allows additional stuff to be added at the end of the script.

Code: Select all

Event ("&command","test")
	{
	$TempVar = sysRand(5)
	If ( $TempVar > 0 )
		{
		*say one
		}
	If ( $TempVar > 1 )
		{
		*say two.
		}
	If ( $TempVar > 2 )
		{
		*say three.
		}
	If ( $TempVar > 3 )
		{
		*say four.
		}
	If ( $TempVar > 4 )
		{
		*say five.
		}
	*say Count complete
	}
Nested IF ELSE statement- single output
This is an example of an IF ELSE statement with nested IFs that have no ELSE. The script parser will go down whichever first level of the script applies, then perform whatever 2nd level IF statement applies, and continue to the end of the first level statement and stop.

Code: Select all

Event ("&command","test")
	{
	$TempVar = sysRand(10)
	If ( $TempVar > 5 )
		{
		If ( $TempVar = 6 )
			{
			*say six.
			}
		If ( $TempVar = 7 )
			{
			*say seven.
			}
		If ( $TempVar = 8 )
			{
			*say eight.
			}
		If ( $TempVar = 9 )
			{
			*say nine.
			}
		If ( $TempVar = 10 )
			{
			*say ten.
			}
		*say Script complete
		}
	Else
		{
		If ( $TempVar = 1 )
			{
			*say one
			}
		If ( $TempVar = 2 )
			{
			*say two.
			}
		If ( $TempVar = 3 )
			{
			*say three.
			}
		If ( $TempVar = 4 )
			{
			*say four.
			}
		If ( $TempVar = 5 )
			{
			*say five.
			}
		*say Script complete
		}
	}
Last edited by zaroba on Fri Jan 11, 2013 1:59 pm, edited 3 times in total.
User avatar
zaroba
World Owner
World Owner
Posts: 7257
Joined: Fri Oct 10, 2003 11:06 pm
Location: Hereford, PA
Contact:

Post by zaroba »

OSDs are an extension of scripting that provides a user interface. What is displayed in an OSD can be defined by IF and ELSE statements that are used the same ways the examples in the above post show.


OSD IF statement - single output
In this OSD, the IF statements specify which number is displayed on the OSD. Additional OSD stuff can be added after the IF statements.

Code: Select all

Event ("&command","test")
	{
	$TempVar = sysRand(10)
	osdcreate(OSDCreate,"test","example")
	If ( $TempVar = 1 )
		{
		osdaddat(OSDBIGTEXT, 150, 150, 200, 25, " ", "one")
		}
	If ( $TempVar = 2 )
		{
		osdaddat(OSDBIGTEXT, 150, 150, 200, 25, " ", "two")
		}
	If ( $TempVar = 3 )
		{
		osdaddat(OSDBIGTEXT, 150, 150, 200, 25, " ", "three")
		}
	If ( $TempVar = 4 )
		{
		osdaddat(OSDBIGTEXT, 150, 150, 200, 25, " ", "four")
		}
	If ( $TempVar = 5 )
		{
		osdaddat(OSDBIGTEXT, 150, 150, 200, 25, " ", "five")
		}
	If ( $TempVar = 6 )
		{
		osdaddat(OSDBIGTEXT, 150, 150, 200, 25, " ", "six")
		}
	If ( $TempVar = 7 )
		{
		osdaddat(OSDBIGTEXT, 150, 150, 200, 25, " ", "seven")
		}
	If ( $TempVar = 8 )
		{
		osdaddat(OSDBIGTEXT, 150, 150, 200, 25, " ", "eight")
		}
	If ( $TempVar = 9 )
		{
		osdaddat(OSDBIGTEXT, 150, 150, 200, 25, " ", "nine")
		}
	If ( $TempVar = 10 )
		{
		osdaddat(OSDBIGTEXT, 150, 150, 200, 25, " ", "ten")
		}
	osdaddat(OSDBUTTON, 300, 300, 130, 25, "close", "Close")
	osdactivate()
	}
OSD IF statement - multiple output
Like the above, this script uses no ELSE statement. It will apply all the different IF statements that apply. In-Game it will give the appearance of showing buttons either being clickable or not, like a light switch being on or off. Additional OSD stuff can be added after the IF statements.

Code: Select all

Event ("&command","test")
	{
	$TempVar = sysRand(5)
	osdcreate(OSDCreate,"test","example")
	If ( $TempVar > 0 )
		{
		osdaddat(OSDBUTTON, 50, 200, 50, 25, " ", "one")
		}
	If ( $TempVar > 1 )
		{
		osdaddat(OSDBUTTON, 100, 200, 50, 25, " ", "two")
		}
	If ( $TempVar > 2 )
		{
		osdaddat(OSDBUTTON, 150, 200, 50, 25, " ", "three")
		}
	If ( $TempVar > 3 )
		{
		osdaddat(OSDBUTTON, 200, 200, 50, 25, " ", "four")
		}
	If ( $TempVar > 4 )
		{
		osdaddat(OSDBUTTON, 250, 200, 50, 25, " ", "five")
		}
	If ( $TempVar < 1 )
		{
		osdaddat(OSDFADEDBUTTON, 50, 200, 50, 25, " ", "one")
		}
	If ( $TempVar < 2 )
		{
		osdaddat(OSDFADEDBUTTON, 100, 200, 50, 25, " ", "two")
		}
	If ( $TempVar < 3 )
		{
		osdaddat(OSDFADEDBUTTON, 150, 200, 50, 25, " ", "three")
		}
	If ( $TempVar < 4 )
		{
		osdaddat(OSDFADEDBUTTON, 200, 200, 50, 25, " ", "four")
		}
	If ( $TempVar < 5 )
		{
		osdaddat(OSDFADEDBUTTON, 250, 200, 50, 25, " ", "five")
		}
	osdaddat(OSDBUTTON, 300, 300, 130, 25, "close", "Close")
	osdactivate()
	}
OSD IF ESLE statement
This OSD uses the If and ELSE statements to decixde what to show. When it comes to OSDs, it might be a bit of a bulky way to do things because additional OSD stuff can't be added at the end. Due to the ELSE statement, the script will only display what is in the applicable IF statement and stop, or go to the ELSE if no IF statements apply.

Code: Select all

Event ("&command","test")
	{
	$TempVar = sysRand(5)
	osdcreate(OSDCreate,"test","example")
	osdaddat(OSDFADEDBUTTON, 150, 150, 50, 25, " ", "1")
	osdaddat(OSDFADEDBUTTON, 200, 150, 50, 25, " ", "2")
	osdaddat(OSDFADEDBUTTON, 250, 150, 50, 25, " ", "3")
	osdaddat(OSDFADEDBUTTON, 300, 150, 50, 25, " ", "4")
	osdaddat(OSDFADEDBUTTON, 350, 150, 50, 25, " ", "5")
	osdaddat(OSDBUTTON, 300, 300, 130, 25, "close", "Close")
	If ( $TempVar = 1 )
		{
		osdaddat(OSDBUTTON, 150, 200, 50, 25, " ", "1")
		osdactivate()
		}
	Else If ( $TempVar = 2 )
		{
		osdaddat(OSDBUTTON, 200, 200, 50, 25, " ", "2")
		osdactivate()
		}
	Else If ( $TempVar = 3 )
		{
		osdaddat(OSDBUTTON, 250, 200, 50, 25, " ", "3")
		osdactivate()
		}
	Else If ( $TempVar = 4 )
		{
		osdaddat(OSDBUTTON, 300, 200, 50, 25, " ", "4")
		osdactivate()
		}
	Else
		{
		osdaddat(OSDBUTTON, 350, 200, 50, 25, " ", "5")
		osdactivate()
		}
	}
OSD IF ELSE statement, Nested single output IF
OSD with IF ELSE statement and single output nested IF statements. Like in the last example of the first post, the script parser will choose which first tier IF/ELSe to go in, then perform the actions in that tier and stop. The 2nd tier nested IFs have no ELSE statement on their level so the parser will perform all of them that apply. Additional OSD stuff can be put at end of the 2nd tier stuff, but not the firsty tier due to using the ELSE statement.

Code: Select all

Event ("&command","test")
	{
	$TempVar = sysRand(10)
	osdcreate(OSDCreate,"test","example")
	If ( $TempVar > 5 )
		{
		If ( $TempVar = 6 )
			{
			osdaddat(OSDBIGTEXT, 150, 150, 200, 25, " ", "six")
			}
		If ( $TempVar = 7 )
			{
			osdaddat(OSDBIGTEXT, 150, 150, 200, 25, " ", "seven")
			}
		If ( $TempVar = 8 )
			{
			osdaddat(OSDBIGTEXT, 150, 150, 200, 25, " ", "eight")
			}
		If ( $TempVar = 9 )
			{
			osdaddat(OSDBIGTEXT, 150, 150, 200, 25, " ", "nine")
			}
		If ( $TempVar = 10 )
			{
			osdaddat(OSDBIGTEXT, 150, 150, 200, 25, " ", "ten")
			}
		osdaddat(OSDBUTTON, 300, 300, 130, 25, "close", "Close")
		osdactivate()
		}
	Else
		{
		If ( $TempVar = 1 )
			{
			osdaddat(OSDBIGTEXT, 150, 150, 200, 25, " ", "one")
			}
		If ( $TempVar = 2 )
			{
			osdaddat(OSDBIGTEXT, 150, 150, 200, 25, " ", "two")
			}
		If ( $TempVar = 3 )
			{
			osdaddat(OSDBIGTEXT, 150, 150, 200, 25, " ", "three")
			}
		If ( $TempVar = 4 )
			{
			osdaddat(OSDBIGTEXT, 150, 150, 200, 25, " ", "four")
			}
		If ( $TempVar = 5 )
			{
			osdaddat(OSDBIGTEXT, 150, 150, 200, 25, " ", "five")
			}
		osdaddat(OSDBUTTON, 300, 300, 130, 25, "close", "Close")
		osdactivate()
		}
	}
Locked