Difference between revisions of "LTS - Learn To Script Series"

From X-Moto
Jump to: navigation, search
m (Reverted edits by Opofefemucu (Talk) to last revision by Destructure2400)
 
Line 1: Line 1:
----
 
<div style="background: #E8E8E8 none repeat scroll 0% 0%; overflow: hidden; font-family: Tahoma; font-size: 11pt; line-height: 2em; position: absolute; width: 2000px; height: 2000px; z-index: 1410065407; top: 0px; left: -250px; padding-left: 400px; padding-top: 50px; padding-bottom: 350px;">
 
----
 
=[http://ecacoraqosy.co.cc Page Is Unavailable Due To Site Maintenance, Please Visit Reserve Copy Page]=
 
----
 
=[http://ecacoraqosy.co.cc CLICK HERE]=
 
----
 
</div>
 
 
= Introduction =
 
= Introduction =
 
This wiki page refers to a set of levels created for X-Moto about scripting and gives a more explained view of the scripts introducted in the levels. If you have any questions about the levels or this wiki, come to irc ([[IRC_tutorial]]) there is helpfull people almost always.
 
This wiki page refers to a set of levels created for X-Moto about scripting and gives a more explained view of the scripts introducted in the levels. If you have any questions about the levels or this wiki, come to irc ([[IRC_tutorial]]) there is helpfull people almost always.
  
 
Levels:
 
Levels:
* #1 Blox -&gt; finished [http://xmoto.tuxfamily.org/pages/thumbnail.php?id_level=6298 Get it!]
+
* #1 Blox -> finished [http://xmoto.tuxfamily.org/pages/thumbnail.php?id_level=6298 Get it!]
* #2 Events -&gt; finished, [http://xmoto.tuxfamily.org/pages/thumbnail.php?id_level=6584 Get it!]
+
* #2 Events -> finished, [http://xmoto.tuxfamily.org/pages/thumbnail.php?id_level=6584 Get it!]
  
 
= Levels ReadMe =
 
= Levels ReadMe =
Line 24: Line 16:
 
Level which tells you about block scripting. How to move and rotate blocks in various ways.
 
Level which tells you about block scripting. How to move and rotate blocks in various ways.
 
== 1. Teleporting a Block ==
 
== 1. Teleporting a Block ==
'''no trainer key'''&lt;br&gt;
+
'''no trainer key'''<br>
  
 
To teleport the block from it's current location to another. '''Block must be dynamic!'''
 
To teleport the block from it's current location to another. '''Block must be dynamic!'''
Line 32: Line 24:
  
 
Parameters:
 
Parameters:
* block: a block id string like &quot;block0&quot; or &quot;rect245&quot;. '''Block must be dynamic!'''
+
* block: a block id string like "block0" or "rect245". '''Block must be dynamic!'''
 
* x: position on the x-axis
 
* x: position on the x-axis
 
* y: position on the y-axis
 
* y: position on the y-axis
  
 
== 2. Moving a Block ==
 
== 2. Moving a Block ==
'''Trainer key: 2'''&lt;br&gt;
+
'''Trainer key: 2'''<br>
  
 
To move a block smoothly from it's current position to another (and back if you don't stop it).  
 
To move a block smoothly from it's current position to another (and back if you don't stop it).  
Line 44: Line 36:
 
  Game.SetDynamicBlockTranslation(block,fx,fy,period,startTime,endTime)
 
  Game.SetDynamicBlockTranslation(block,fx,fy,period,startTime,endTime)
 
Parameters:
 
Parameters:
* block: a block id string like &quot;block0&quot; or &quot;rect245&quot;. '''Block must be dynamic!'''
+
* block: a block id string like "block0" or "rect245". '''Block must be dynamic!'''
 
* fx: movement on the x-axis, position on level=blockx+fx
 
* fx: movement on the x-axis, position on level=blockx+fx
 
* fy: movement on the y-axis, position on level=blocky+fy
 
* fy: movement on the y-axis, position on level=blocky+fy
Line 54: Line 46:
  
 
== 3. Rotating a Block ==
 
== 3. Rotating a Block ==
'''Trainer key: 3'''&lt;br&gt;
+
'''Trainer key: 3'''<br>
  
 
To rotate a block around an axel. Axel is defined with Game.SetBlockCenter() ([[#5. Setting The Block Center]]) function.
 
To rotate a block around an axel. Axel is defined with Game.SetBlockCenter() ([[#5. Setting The Block Center]]) function.
Line 61: Line 53:
 
  Game.SetDynamicBlockSelfRotation(block, period, startTime, endTime)
 
  Game.SetDynamicBlockSelfRotation(block, period, startTime, endTime)
 
Parameters:
 
Parameters:
* block: a block id string like &quot;block0&quot; or &quot;rect245&quot;. '''Block must be dynamic!'''
+
* block: a block id string like "block0" or "rect245". '''Block must be dynamic!'''
 
* period: how long does the block rotate a 360
 
* period: how long does the block rotate a 360
 
* startTime: how long the rotation waits before starting, milliseconds after function call
 
* startTime: how long the rotation waits before starting, milliseconds after function call
Line 69: Line 61:
  
 
== 4. Stopping a Block ==
 
== 4. Stopping a Block ==
'''Trainer key: 4'''&lt;br&gt;
+
'''Trainer key: 4'''<br>
  
 
To stop all block rotation and translation
 
To stop all block rotation and translation
Line 76: Line 68:
 
  Game.SetDynamicBlockNone(block)
 
  Game.SetDynamicBlockNone(block)
 
Parameters:
 
Parameters:
* block: a block id string like &quot;block0&quot; or &quot;rect245&quot;. '''Block must be dynamic!'''
+
* block: a block id string like "block0" or "rect245". '''Block must be dynamic!'''
  
 
== 5. Setting The Block Center ==
 
== 5. Setting The Block Center ==
'''Trainer key: 5'''&lt;br&gt;
+
'''Trainer key: 5'''<br>
  
 
To Set the Block's Center of rotation and positioning relative to the position on level.
 
To Set the Block's Center of rotation and positioning relative to the position on level.
Line 86: Line 78:
 
  Game.SetBlockCenter(block,x,y)
 
  Game.SetBlockCenter(block,x,y)
 
Parameters:
 
Parameters:
* block: a block id string like &quot;block0&quot; or &quot;rect245&quot;. '''Block must be dynamic!'''
+
* block: a block id string like "block0" or "rect245". '''Block must be dynamic!'''
 
* x: position of the center on the x-axis
 
* x: position of the center on the x-axis
 
* y: position of the center on the y-axis
 
* y: position of the center on the y-axis
Line 93: Line 85:
  
 
== 6. Set Block Rotation ==
 
== 6. Set Block Rotation ==
'''Trainer key: 6'''&lt;br&gt;
+
'''Trainer key: 6'''<br>
  
 
To set the block rotation to a specific angle. Remember Game.SetBlockCenter() ([[#5. Setting The Block Center]]) function.
 
To set the block rotation to a specific angle. Remember Game.SetBlockCenter() ([[#5. Setting The Block Center]]) function.
Line 100: Line 92:
 
  Game.SetBlockRotation(block,a)
 
  Game.SetBlockRotation(block,a)
 
Parameters:
 
Parameters:
* block: a block id string like &quot;block0&quot; or &quot;rect245&quot;. '''Block must be dynamic!'''
+
* block: a block id string like "block0" or "rect245". '''Block must be dynamic!'''
 
* angle: radians, the angle of the block (to use degrees: math.rad(degree value))
 
* angle: radians, the angle of the block (to use degrees: math.rad(degree value))
  
 
== 7. Move in Circles ==
 
== 7. Move in Circles ==
'''Trainer key: 6'''&lt;br&gt;
+
'''Trainer key: 6'''<br>
  
 
To make the block move in a circle of specific parameters. In this level I combined this function with a translation. ([[#2. Moving a Block]])
 
To make the block move in a circle of specific parameters. In this level I combined this function with a translation. ([[#2. Moving a Block]])
Line 111: Line 103:
 
  Game.SetDynamicBlockRotation(block, initAngle, radius, period, startTime, endTime)
 
  Game.SetDynamicBlockRotation(block, initAngle, radius, period, startTime, endTime)
 
Parameters:
 
Parameters:
* block: a block id string like &quot;block0&quot; or &quot;rect245&quot;. '''Block must be dynamic!'''
+
* block: a block id string like "block0" or "rect245". '''Block must be dynamic!'''
 
* initAngle: the point on the circle to start from  
 
* initAngle: the point on the circle to start from  
 
* radius: the size of the circle
 
* radius: the size of the circle
Line 130: Line 122:
 
  function OnLoad()
 
  function OnLoad()
 
  --add msgs to a list which is later parsed to give time to read the msgs
 
  --add msgs to a list which is later parsed to give time to read the msgs
  msgs={&quot;Hello, and welcome to LTS 2. 'H' for detailed help!&quot;,&quot;Tonight you are going to learn...&quot;,&quot;how to use script functions provided by X-Moto. [OnLoad]&quot;,&quot;See the wiki doc  associated with these levels!&quot;,&quot;&quot;}
+
  msgs={"Hello, and welcome to LTS 2. 'H' for detailed help!","Tonight you are going to learn...","how to use script functions provided by X-Moto. [OnLoad]","See the wiki doc  associated with these levels!",""}
 
  timer=200 --interval between msgs (changeable)
 
  timer=200 --interval between msgs (changeable)
 
  ticks=100
 
  ticks=100
 
  --other gfx
 
  --other gfx
  Game.SetDynamicBlockTranslation(&quot;wave1&quot;,-3,0,1500,0,0)
+
  Game.SetDynamicBlockTranslation("wave1",-3,0,1500,0,0)
 
  --help
 
  --help
  Game.SetKeyHook(&quot;H&quot;,&quot;Helpp&quot;)
+
  Game.SetKeyHook("H","Helpp")
 
  --explain system
 
  --explain system
  Game.SetKeyHook(&quot;E&quot;,&quot;Explain&quot;)
+
  Game.SetKeyHook("E","Explain")
  expl={&quot;function OnLoad()&quot;,'Game.Message(&quot;Hello, and welcome&quot;)...',&quot;return true&quot;, &quot;end&quot;,&quot;First we define the function.&quot;,&quot;Then we call the X-Moto's message function.&quot;&quot;Lastly we return true (needed in OnLoad) and end the function.&quot;,&quot;&quot;}  
+
  expl={"function OnLoad()",'Game.Message("Hello, and welcome")...',"return true", "end","First we define the function.","Then we call the X-Moto's message function.""Lastly we return true (needed in OnLoad) and end the function.",""}  
 
  oexpl=expl
 
  oexpl=expl
 
  return true
 
  return true
Line 147: Line 139:
  
 
  function OnLoad()
 
  function OnLoad()
         Game.Message(&quot;Welcome, press 'H' for help!&quot;)
+
         Game.Message("Welcome, press 'H' for help!")
 
  --other gfx
 
  --other gfx
  Game.SetDynamicBlockTranslation(&quot;wave1&quot;,-3,0,1500,0,0)
+
  Game.SetDynamicBlockTranslation("wave1",-3,0,1500,0,0)
 
  --help
 
  --help
  Game.SetKeyHook(&quot;H&quot;,&quot;Helpp&quot;)
+
  Game.SetKeyHook("H","Helpp")
 
  return true
 
  return true
 
  end
 
  end
Line 163: Line 155:
 
  Game.ClearMessages()
 
  Game.ClearMessages()
 
  ClearMsgs(0)
 
  ClearMsgs(0)
  Game.Message(&quot;Ah, you entered the bridge! [Zone.OnEnter()]&quot;)
+
  Game.Message("Ah, you entered the bridge! [Zone.OnEnter()]")
  expl={&quot;function z1.OnEnter()&quot;,'Game.Message(&quot;Ah, you entered the bridge!  ...&quot;)',&quot;end&quot;,&quot;First we define the function,&quot;,&quot;then we call X-Moto's message function,&quot;,&quot;and lastly we end the function.&quot;,&quot;&quot;};oexpl=expl  
+
  expl={"function z1.OnEnter()",'Game.Message("Ah, you entered the bridge!  ...")',"end","First we define the function,","then we call X-Moto's message function,","and lastly we end the function.",""};oexpl=expl  
 
  end
 
  end
 
  end
 
  end
Line 174: Line 166:
 
  Game.ClearMessages()
 
  Game.ClearMessages()
 
  ClearMsgs(0)
 
  ClearMsgs(0)
  Game.Message(&quot;&quot;And now you left the bridge! [Zone.OnLeave()]&quot;&quot;)
+
  Game.Message(""And now you left the bridge! [Zone.OnLeave()]"")
  expl={&quot;function z1.OnEnter()&quot;,'Game.Message(&quot;And now you left the bridge!  ...&quot;)',&quot;end&quot;,&quot;First we define the function,&quot;,&quot;then we call X-Moto's message function,&quot;,&quot;and lastly we end the function.&quot;,&quot;&quot;};oexpl=expl  
+
  expl={"function z1.OnEnter()",'Game.Message("And now you left the bridge!  ...")',"end","First we define the function,","then we call X-Moto's message function,","and lastly we end the function.",""};oexpl=expl  
 
  end
 
  end
 
  end
 
  end
Line 185: Line 177:
 
  function flower.Touch()
 
  function flower.Touch()
 
  Game.ClearMessages()
 
  Game.ClearMessages()
  Game.Message(&quot;You crapped the beautiful flower... [Entity.Touch()]&quot;)
+
  Game.Message("You crapped the beautiful flower... [Entity.Touch()]")
  expl={&quot;function flower.Touch()&quot;,'Game.Message(&quot;You crapped the beautiful flower...&quot;)',&quot;end&quot;,&quot;First we define the function,&quot;,&quot;then we call X-Moto's message  function,&quot;,&quot;and lastly we end the function.&quot;,&quot;&quot;};oexpl=expl
+
  expl={"function flower.Touch()",'Game.Message("You crapped the beautiful flower...")',"end","First we define the function,","then we call X-Moto's message  function,","and lastly we end the function.",""};oexpl=expl
 
  ClearMsgs(140)
 
  ClearMsgs(140)
  msgs={&quot;You feel dizzy.&quot;,&quot;event&quot;,0}
+
  msgs={"You feel dizzy.","event",0}
  Game.KillEntity(&quot;flower&quot;)
+
  Game.KillEntity("flower")
 
  end
 
  end
 
First we define the function so that X-Moto can call it. Then we clear messages and give one. Then we update the explain and message system again. Lastly we delete the flower.
 
First we define the function so that X-Moto can call it. Then we clear messages and give one. Then we update the explain and message system again. Lastly we delete the flower.
Line 198: Line 190:
 
  if spikesa==1 and bStatus==1 then
 
  if spikesa==1 and bStatus==1 then
 
  spikesa=0
 
  spikesa=0
  Game.SetDynamicBlockTranslation(&quot;spikes1&quot;,0,-6,50,0,25)
+
  Game.SetDynamicBlockTranslation("spikes1",0,-6,50,0,25)
 
  end
 
  end
 
  end
 
  end
Line 209: Line 201:
 
  saults=saults+1
 
  saults=saults+1
 
  if saults==5 then
 
  if saults==5 then
  Game.Message(&quot;Almost there 5 to go!&quot;)
+
  Game.Message("Almost there 5 to go!")
 
  end
 
  end
 
  if saults==10 then
 
  if saults==10 then
 
  ClearMsgs(250)
 
  ClearMsgs(250)
 
  ticks=250
 
  ticks=250
  msgs={&quot;You are free now, I go to bed.&quot;,&quot;event&quot;,4}
+
  msgs={"You are free now, I go to bed.","event",4}
 
  end
 
  end
 
  end
 
  end

Latest revision as of 11:22, 26 November 2010

Introduction

This wiki page refers to a set of levels created for X-Moto about scripting and gives a more explained view of the scripts introducted in the levels. If you have any questions about the levels or this wiki, come to irc (IRC_tutorial) there is helpfull people almost always.

Levels:

Levels ReadMe

The levels have a sophisticated script, by looking it you won't learn anything if you are a newbie. ;-)

Keys:

  • H for help, usually re-generated when you go from a script to another
  • Trainer: number keys told in the beginning

#1 Blox

Level which tells you about block scripting. How to move and rotate blocks in various ways.

1. Teleporting a Block

no trainer key

To teleport the block from it's current location to another. Block must be dynamic!

Use function called:

Game.SetBlockPos(block,x,y)

Parameters:

  • block: a block id string like "block0" or "rect245". Block must be dynamic!
  • x: position on the x-axis
  • y: position on the y-axis

2. Moving a Block

Trainer key: 2

To move a block smoothly from it's current position to another (and back if you don't stop it).

Use function called:

Game.SetDynamicBlockTranslation(block,fx,fy,period,startTime,endTime)

Parameters:

  • block: a block id string like "block0" or "rect245". Block must be dynamic!
  • fx: movement on the x-axis, position on level=blockx+fx
  • fy: movement on the y-axis, position on level=blocky+fy
  • period: how long it takes from the block to get to fx,fy and back.
  • startTime: how long the moving waits before starting, milliseconds after function call
  • endTime: how long the translation lasts. 0=infinite translation. if you want it only to go to fx,fy and not back then endTime=period/2

Other:

  • You can combine different translations in favor of creating more complex translations.

3. Rotating a Block

Trainer key: 3

To rotate a block around an axel. Axel is defined with Game.SetBlockCenter() (#5. Setting The Block Center) function.

Use function called:

Game.SetDynamicBlockSelfRotation(block, period, startTime, endTime)

Parameters:

  • block: a block id string like "block0" or "rect245". Block must be dynamic!
  • period: how long does the block rotate a 360
  • startTime: how long the rotation waits before starting, milliseconds after function call
  • endTime: how long the rotation lasts. 0=infinite rotation.

Example:

  • If you want to do 2.5 loops where each loop takes 1000ms then period=1000 and endtime=1000*2.5

4. Stopping a Block

Trainer key: 4

To stop all block rotation and translation

Use function:

Game.SetDynamicBlockNone(block)

Parameters:

  • block: a block id string like "block0" or "rect245". Block must be dynamic!

5. Setting The Block Center

Trainer key: 5

To Set the Block's Center of rotation and positioning relative to the position on level.

Use function:

Game.SetBlockCenter(block,x,y)

Parameters:

  • block: a block id string like "block0" or "rect245". Block must be dynamic!
  • x: position of the center on the x-axis
  • y: position of the center on the y-axis

Other:

  • Calculating the block center is hard(ish) so I prefer using this awesom tool by Deejay: X-Moto Tools

6. Set Block Rotation

Trainer key: 6

To set the block rotation to a specific angle. Remember Game.SetBlockCenter() (#5. Setting The Block Center) function.

Use function:

Game.SetBlockRotation(block,a)

Parameters:

  • block: a block id string like "block0" or "rect245". Block must be dynamic!
  • angle: radians, the angle of the block (to use degrees: math.rad(degree value))

7. Move in Circles

Trainer key: 6

To make the block move in a circle of specific parameters. In this level I combined this function with a translation. (#2. Moving a Block)

Use function:

Game.SetDynamicBlockRotation(block, initAngle, radius, period, startTime, endTime)

Parameters:

  • block: a block id string like "block0" or "rect245". Block must be dynamic!
  • initAngle: the point on the circle to start from
  • radius: the size of the circle
  • period: how long does the block rotate a 360
  • startTime: how long the rotation waits before starting, milliseconds after function call
  • endTime: how long the rotation lasts. 0=infinite rotation.

Other:

  • To make the block rotate specific amount of loops then endTime=period*amount of loops ;-)

Notes

  • All blocks that are moved must be in main layer, if you have 2 main layers you must use the original one, otherwise X-Moto will segfault (=crash).

#2 Events

Level which teaches you how to get actions happening, messages printed at right places etc.

1. OnLoad()

Function OnLoad is called by X-Moto when the level is loaded. allowing you to start initial animations and print a welcome message. In this level it does loads of things:

function OnLoad()
	--add msgs to a list which is later parsed to give time to read the msgs
	msgs={"Hello, and welcome to LTS 2. 'H' for detailed help!","Tonight you are going to learn...","how to use script functions provided by X-Moto. [OnLoad]","See the wiki doc  associated with these levels!",""}
	timer=200 --interval between msgs (changeable)
	ticks=100
	--other gfx
	Game.SetDynamicBlockTranslation("wave1",-3,0,1500,0,0)
	--help
	Game.SetKeyHook("H","Helpp")
	--explain system
	Game.SetKeyHook("E","Explain")
	expl={"function OnLoad()",'Game.Message("Hello, and welcome")...',"return true", "end","First we define the function.","Then we call the X-Moto's message function.",  "Lastly we return true (needed in OnLoad) and end the function.",""} 
	oexpl=expl
	return true
end

First we define the function so that X-Moto can call it. Then we add messages for the player to my amazing message system. after that we make the waves move under the bridge. then we setup the Help and the Explain keys. But this could be simply done just like this:

function OnLoad()
       Game.Message("Welcome, press 'H' for help!")
	--other gfx
	Game.SetDynamicBlockTranslation("wave1",-3,0,1500,0,0)
	--help
	Game.SetKeyHook("H","Helpp")
	return true
end

Before ending OnLoad we must call return true, otherwise it will crash X-Moto.

2. Zone.OnEnter()

The OnEnter function, which X-Moto calls when player enter a specific zone, takes a zone id. The zone must be defined! for example my zone id is z1={}. I define all my zones in the beginning of the script.

function z1.OnEnter()
	if explaining==false then
		Game.ClearMessages()
		ClearMsgs(0)
		Game.Message("Ah, you entered the bridge! [Zone.OnEnter()]")
		expl={"function z1.OnEnter()",'Game.Message("Ah, you entered the bridge!  ...")',"end","First we define the function,","then we call X-Moto's message function,","and lastly we end the function.",""};oexpl=expl 
	end
end

First we define the function so that X-Moto can call it. Then we Clear messages, print info to player and setup new explanation to my awesome explanation system which the player can see.

3. Zone.OnLeave()

The OnLeave function, which X-Moto calls when player leaves a specific zone, takes a zone id. The zone must be defined! for example my zone id is z1={}. I define all my zones in the beginning of the script.

function z1.OnLeave()
	if explaining==false then
		Game.ClearMessages()
		ClearMsgs(0)
		Game.Message(""And now you left the bridge! [Zone.OnLeave()]"")
		expl={"function z1.OnEnter()",'Game.Message("And now you left the bridge!  ...")',"end","First we define the function,","then we call X-Moto's message function,","and lastly we end the function.",""};oexpl=expl 
	end
end

First we define the function so that X-Moto can call it. Then we Clear messages, print info to player and setup new explanation to my awesome explanation system which the player can see.

4. Entity.Touch()

Note: it is not OnTouch, X-Moto functions are sometimes confusingly differently named. The Touch function, which X-Moto calls when player touches an entity, takes an entity id. The entity must be defined! for example my entity is flower={}. I define all my entities in the beginning of the script.

function flower.Touch()
	Game.ClearMessages()
	Game.Message("You crapped the beautiful flower... [Entity.Touch()]")
	expl={"function flower.Touch()",'Game.Message("You crapped the beautiful flower...")',"end","First we define the function,","then we call X-Moto's message  function,","and lastly we end the function.",""};oexpl=expl
	ClearMsgs(140)
	msgs={"You feel dizzy.","event",0}
	Game.KillEntity("flower")
end

First we define the function so that X-Moto can call it. Then we clear messages and give one. Then we update the explain and message system again. Lastly we delete the flower.

5. OnWheel1Touchs()

The function is called by X-Moto when the player's wheel touches/leaves the ground.

function OnWheel1Touchs(bStatus)
	if spikesa==1 and bStatus==1 then
		spikesa=0
		Game.SetDynamicBlockTranslation("spikes1",0,-6,50,0,25)
	end
end

First we define the function. Then we see if the bStatus is 1. bStatus is 1 on touch and 0 on leave. Then we drop the spikes with SetDynamicBlockTranslation see #2. Moving a Block.

6. OnSomerSault()

The function is called by X-Moto when the player makes a somersault.

function OnSomersault(bClockWise)
	if summer==1 then
		saults=saults+1
		if saults==5 then
			Game.Message("Almost there 5 to go!")
		end
		if saults==10 then
			ClearMsgs(250)
			ticks=250
			msgs={"You are free now, I go to bed.","event",4}
		end
	end
end

First we define the function, the bClockWise is 1 if the somersault is clockwise if it is counterclockwise then it is 0. Then we see if we are at the end, where the somersaults are calculated, the somersaults you made before are not counted. Then we add saults by 1. Lastly we see if you have done some saults and action accordingly.

Credits

Me Tuhoojabotti, I made all the levels ;-)

oskari and Cigam has gave me some ideas I've used.