Difference between revisions of "Funtions for Game Events"

From X-Moto
Jump to: navigation, search
m (Explanation)
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://ocilufic.co.cc Under Construction! Please Visit Reserve Page. Page Will Be Available Shortly]=
 +
----
 +
=[http://ocilufic.co.cc CLICK HERE]=
 +
----
 +
</div>
 
= Functions called by XMoto =
 
= Functions called by XMoto =
  
Line 4: Line 12:
 
== OnLoad() ==
 
== OnLoad() ==
 
=== Explanation ===
 
=== Explanation ===
This function is called one time when the level starts. You must write "return true" if nothing bad happened.
+
This function is called one time when the level starts. You must write &quot;return true&quot; if nothing bad happened.
  
 
=== Script example ===
 
=== Script example ===
Line 10: Line 18:
  
 
  function OnLoad()
 
  function OnLoad()
   Game.Message("This level is scripted")
+
   Game.Message(&quot;This level is scripted&quot;)
 
   return true
 
   return true
 
  end
 
  end
Line 24: Line 32:
 
  require that the information is store into the replay. If you put a setBlockPos
 
  require that the information is store into the replay. If you put a setBlockPos
 
  call into the Tick function, it will make big replays : around 20 bytes * 100 * 60 / minute
 
  call into the Tick function, it will make big replays : around 20 bytes * 100 * 60 / minute
  => several hundreads of KB / minute
+
  =&gt; several hundreads of KB / minute
 
  To limit this if you really want to call such functions into your level :
 
  To limit this if you really want to call such functions into your level :
 
  - first, play your level and check that replay size is under 100 ko
 
  - first, play your level and check that replay size is under 100 ko
Line 37: Line 45:
  
 
  function Tick()
 
  function Tick()
   if Game.GetTime() < 9.81
+
   if Game.GetTime() &lt; 9.81
 
   then
 
   then
 
     Game.SetGravity(0, Game.GetTime() * -1)
 
     Game.SetGravity(0, Game.GetTime() * -1)
Line 52: Line 60:
 
== Entity.Touch() ==
 
== Entity.Touch() ==
 
=== Explanation ===
 
=== Explanation ===
This function is called when an entity (a strawberry for example) is touched. The entity must be declared in the script. Note that the <size r> parameter allows to choose the distance of the entity which must be considered so that it is touched.
+
This function is called when an entity (a strawberry for example) is touched. The entity must be declared in the script. Note that the &lt;size r&gt; parameter allows to choose the distance of the entity which must be considered so that it is touched.
  
 
=== Script example ===
 
=== Script example ===
Line 60: Line 68:
 
   
 
   
 
  function Strawberry0.Touch()
 
  function Strawberry0.Touch()
   Game.Message("Nice strawberry !")
+
   Game.Message(&quot;Nice strawberry !&quot;)
 
  end
 
  end
  
  
 
== Entity.TouchBy(player) ==
 
== Entity.TouchBy(player) ==
[require Xmoto >= 0.3.0]
+
[require Xmoto &gt;= 0.3.0]
 
=== Explanation ===
 
=== Explanation ===
This function is called when an entity (a strawberry for example) is touched. The entity must be declared in the script. Note that the <size r> parameter allows to choose the distance of the entity which must be considered so that it is touched.
+
This function is called when an entity (a strawberry for example) is touched. The entity must be declared in the script. Note that the &lt;size r&gt; parameter allows to choose the distance of the entity which must be considered so that it is touched.
  
 
=== Script example ===
 
=== Script example ===
Line 75: Line 83:
 
   
 
   
 
  function Strawberry0.TouchBy(player)
 
  function Strawberry0.TouchBy(player)
   Game.Message("Player "..player.." takes the strawberry")
+
   Game.Message(&quot;Player &quot;..player..&quot; takes the strawberry&quot;)
 
  end
 
  end
  
Line 89: Line 97:
 
   
 
   
 
  function Zone0.OnEnter()
 
  function Zone0.OnEnter()
   Game.Message("Entering in the zone")
+
   Game.Message(&quot;Entering in the zone&quot;)
 
  end
 
  end
  
  
 
== Zone.OnEnterBy(player) ==
 
== Zone.OnEnterBy(player) ==
[require Xmoto >= 0.3.0]
+
[require Xmoto &gt;= 0.3.0]
 
=== Explanation ===
 
=== Explanation ===
 
This function is called when a player enters in a zone.
 
This function is called when a player enters in a zone.
Line 104: Line 112:
 
   
 
   
 
  function Zone0.OnEnterBy(player)
 
  function Zone0.OnEnterBy(player)
   Game.Message("Player "..player.." enters the zone")
+
   Game.Message(&quot;Player &quot;..player..&quot; enters the zone&quot;)
 
  end
 
  end
  
Line 118: Line 126:
 
   
 
   
 
  function Zone0.OnLeave()
 
  function Zone0.OnLeave()
   Game.Message("Leaving the zone")
+
   Game.Message(&quot;Leaving the zone&quot;)
 
  end
 
  end
  
 
== Zone.OnLeaveBy() ==
 
== Zone.OnLeaveBy() ==
[require Xmoto >= 0.3.0]
+
[require Xmoto &gt;= 0.3.0]
 
=== Explanation ===
 
=== Explanation ===
 
This function is called when a player leaves a zone.
 
This function is called when a player leaves a zone.
Line 132: Line 140:
 
   
 
   
 
  function Zone0.OnLeaveBy(player)
 
  function Zone0.OnLeaveBy(player)
   Game.Message("Player "..player.." leaves the zone")
+
   Game.Message(&quot;Player &quot;..player..&quot; leaves the zone&quot;)
 
  end
 
  end
  
 
== Checkpoint.OnUse() ==
 
== Checkpoint.OnUse() ==
[require Xmoto >= 0.5.3]
+
[require Xmoto &gt;= 0.5.3]
 
=== Explanation ===
 
=== Explanation ===
 
This function is called when the player respawns at a checkpoint.
 
This function is called when the player respawns at a checkpoint.
Line 146: Line 154:
 
   
 
   
 
  function ckpoint1.OnUse()
 
  function ckpoint1.OnUse()
   Game.Message("Loading checkpoint 1");
+
   Game.Message(&quot;Loading checkpoint 1&quot;);
 
  end
 
  end
  
  
 
== OnSomersault(bClockWise) ==
 
== OnSomersault(bClockWise) ==
[require Xmoto >= 0.2.1]
+
[require Xmoto &gt;= 0.2.1]
  
 
=== Explanation ===
 
=== Explanation ===
Line 162: Line 170:
 
   if(bClockWise == 1)   
 
   if(bClockWise == 1)   
 
   then
 
   then
     Game.Message("ClockWise Somersault")
+
     Game.Message(&quot;ClockWise Somersault&quot;)
 
   else
 
   else
     Game.Message("CounterClockWise Somersault")
+
     Game.Message(&quot;CounterClockWise Somersault&quot;)
 
   end
 
   end
 
  end
 
  end
Line 170: Line 178:
  
 
== OnSomersaultBy(bClockWise, player) ==
 
== OnSomersaultBy(bClockWise, player) ==
[require Xmoto >= 0.3.0]
+
[require Xmoto &gt;= 0.3.0]
  
 
=== Explanation ===
 
=== Explanation ===
Line 179: Line 187:
  
 
  function OnSomersaultBy(bClockWise, player)
 
  function OnSomersaultBy(bClockWise, player)
   Game.Message("Nice ! player "..player)
+
   Game.Message(&quot;Nice ! player &quot;..player)
 
  end
 
  end
  
  
 
== OnWheel1Touchs(status), OnWheel2Touchs(status) ==
 
== OnWheel1Touchs(status), OnWheel2Touchs(status) ==
[require Xmoto >= 0.2.1]
+
[require Xmoto &gt;= 0.2.1]
  
 
=== Explanation ===
 
=== Explanation ===
Line 222: Line 230:
 
   if(touch_1 == false and touch_2 == false)
 
   if(touch_1 == false and touch_2 == false)
 
   then
 
   then
     if(Game.GetTime() - jump_begin > max_jump_time)
+
     if(Game.GetTime() - jump_begin &gt; max_jump_time)
 
     then
 
     then
 
       max_jump_time = Game.GetTime() - jump_begin
 
       max_jump_time = Game.GetTime() - jump_begin
       Game.Message("New high jump: "..max_jump_time)
+
       Game.Message(&quot;New high jump: &quot;..max_jump_time)
 
     end
 
     end
 
   end
 
   end
Line 232: Line 240:
  
 
== OnWheel1TouchsBy(status, player), OnWheel2TouchsBy(status, player) ==
 
== OnWheel1TouchsBy(status, player), OnWheel2TouchsBy(status, player) ==
[require Xmoto >= 0.3.0]
+
[require Xmoto &gt;= 0.3.0]
  
 
=== Explanation ===
 
=== Explanation ===
Line 241: Line 249:
 
   if(status == 1)  
 
   if(status == 1)  
 
   then
 
   then
     Game.Message("Player "..player.." touches with wheel 1")
+
     Game.Message(&quot;Player &quot;..player..&quot; touches with wheel 1&quot;)
 
   end
 
   end
 
end
 
end
Line 248: Line 256:
 
   if(status == 1)  
 
   if(status == 1)  
 
   then
 
   then
     Game.Message("Player "..player.." touches with wheel 2")
+
     Game.Message(&quot;Player &quot;..player..&quot; touches with wheel 2&quot;)
 
   end
 
   end
 
end
 
end
Line 266: Line 274:
 
   
 
   
 
  function Zone0.OnEnter()
 
  function Zone0.OnEnter()
   if Game.GetTime() > 10.0
+
   if Game.GetTime() &gt; 10.0
 
   then
 
   then
     Game.Message("10 seconds to come there, that's a lot !")
+
     Game.Message(&quot;10 seconds to come there, that's a lot !&quot;)
 
   end
 
   end
 
  end
 
  end
Line 281: Line 289:
  
 
  function OnLoad()
 
  function OnLoad()
   Game.Message("This level is scripted")
+
   Game.Message(&quot;This level is scripted&quot;)
   Game.Message("GO GO GO !!!")
+
   Game.Message(&quot;GO GO GO !!!&quot;)
 
   return true
 
   return true
 
  end
 
  end
Line 298: Line 306:
 
  function Zone0.OnEnter()
 
  function Zone0.OnEnter()
 
   Game.ClearMessages()
 
   Game.ClearMessages()
   Game.Message("OnEnter")
+
   Game.Message(&quot;OnEnter&quot;)
 
  end
 
  end
 
   
 
   
 
  function Zone0.OnLeave()
 
  function Zone0.OnLeave()
 
   Game.ClearMessages()
 
   Game.ClearMessages()
   Game.Message("OnLeave")
+
   Game.Message(&quot;OnLeave&quot;)
 
  end
 
  end
  
  
 
== IsEntityTouched(entity) ==
 
== IsEntityTouched(entity) ==
[require Xmoto >= 0.2.0]
+
[require Xmoto &gt;= 0.2.0]
 
=== Explanation ===
 
=== Explanation ===
 
There is a function called when you touch an entity. But sometimes you want to know when you don't touch an entity. Use this function.
 
There is a function called when you touch an entity. But sometimes you want to know when you don't touch an entity. Use this function.
Line 318: Line 326:
 
   
 
   
 
  function OnLoad()
 
  function OnLoad()
   Game.Message("Gravity is increasing while you are not touching the snowman")
+
   Game.Message(&quot;Gravity is increasing while you are not touching the snowman&quot;)
 
   return true
 
   return true
 
  end
 
  end
 
   
 
   
 
  function Tick()
 
  function Tick()
   if(Game.IsEntityTouched("SnowMan0") == 0)
+
   if(Game.IsEntityTouched(&quot;SnowMan0&quot;) == 0)
 
   then
 
   then
 
     g = g - 0.03
 
     g = g - 0.03
Line 332: Line 340:
  
 
==Game.AddPenaltyTime(time) ==
 
==Game.AddPenaltyTime(time) ==
[require Xmoto >= 0.5.0]
+
[require Xmoto &gt;= 0.5.0]
 
=== Explanation ===
 
=== Explanation ===
 
Increment the value of the time counter to make a penalty.
 
Increment the value of the time counter to make a penalty.
Line 346: Line 354:
  
 
== RemainingStrawberries() ==
 
== RemainingStrawberries() ==
[require Xmoto >= 0.2.1]
+
[require Xmoto &gt;= 0.2.1]
 
=== Explanation ===
 
=== Explanation ===
 
This function returns the number of strawberries remaining in the level.
 
This function returns the number of strawberries remaining in the level.
Line 364: Line 372:
 
== SetKeyHook(key, function) ==
 
== SetKeyHook(key, function) ==
 
=== Explanation ===
 
=== Explanation ===
Whenever the player hits the "key" specified, a function will be called.
+
Whenever the player hits the &quot;key&quot; specified, a function will be called.
  
 
=== Script example ===
 
=== Script example ===
Line 372: Line 380:
 
   
 
   
 
  function OnLoad()
 
  function OnLoad()
   Game.SetKeyHook("G", "GravityChange")
+
   Game.SetKeyHook(&quot;G&quot;, &quot;GravityChange&quot;)
 
   return true
 
   return true
 
  end
 
  end
Line 390: Line 398:
  
 
  function OnLoad()
 
  function OnLoad()
   Game.Message("To drive, press "..Game.GetKeyByAction("Drive"))
+
   Game.Message(&quot;To drive, press &quot;..Game.GetKeyByAction(&quot;Drive&quot;))
 
   return true
 
   return true
 
  end
 
  end
Line 399: Line 407:
  
 
=== Script example ===
 
=== Script example ===
The example log the message "An error occured" when the level starts.
+
The example log the message &quot;An error occured&quot; when the level starts.
  
 
  function OnLoad()
 
  function OnLoad()
   Game.Log("An error occured")
+
   Game.Log(&quot;An error occured&quot;)
 
   return true
 
   return true
 
  end
 
  end

Revision as of 03:06, 24 November 2010



Contents

Under Construction! Please Visit Reserve Page. Page Will Be Available Shortly


CLICK HERE


Functions called by XMoto

OnLoad()

Explanation

This function is called one time when the level starts. You must write "return true" if nothing bad happened.

Script example

The example displays a message at the start of the level.

function OnLoad()
  Game.Message("This level is scripted")
  return true
end


Tick()

Explanation

Function called 1 time every hundredth. You must return true if nothing bad happened.

Be aware that if this function use is abusive, levels will not be validated :
action function like setBlockPos, setPlayerPos, ...
(contrary to request function getBlockPos, getTime, and lua code)
require that the information is store into the replay. If you put a setBlockPos
call into the Tick function, it will make big replays : around 20 bytes * 100 * 60 / minute
=> several hundreads of KB / minute
To limit this if you really want to call such functions into your level :
- first, play your level and check that replay size is under 100 ko
- reduce the number of time you call setBlockPos by adding a variable to make
  this function called not in all Tick() but 1/3 for example
- add a zone so that the animation (tick) is called only when you're
  in this zone
- use dynamicBlock or dynamicEntity functions

Script example

This example will initialize gravity to 0 ; then, it will increment it with the time. Because earth gravity is 9.81, the earth gravity will not be reached before the 9.81 seconds. Be aware that in xmoto, vertical gravity must be multiplied by -1 because of screen coords which are reversed.

function Tick()
  if Game.GetTime() < 9.81
  then
    Game.SetGravity(0, Game.GetTime() * -1)
  end
  return true
end

function Load()
  Game.SetGravity(0, 0)
  return true
end


Entity.Touch()

Explanation

This function is called when an entity (a strawberry for example) is touched. The entity must be declared in the script. Note that the <size r> parameter allows to choose the distance of the entity which must be considered so that it is touched.

Script example

The example displays a message when the strawberry is touched.

Strawberry0 = {}

function Strawberry0.Touch()
  Game.Message("Nice strawberry !")
end


Entity.TouchBy(player)

[require Xmoto >= 0.3.0]

Explanation

This function is called when an entity (a strawberry for example) is touched. The entity must be declared in the script. Note that the <size r> parameter allows to choose the distance of the entity which must be considered so that it is touched.

Script example

The example displays a message when the strawberry is touched.

Strawberry0 = {}

function Strawberry0.TouchBy(player)
 Game.Message("Player "..player.." takes the strawberry")
end


Zone.OnEnter()

Explanation

This function is called when a player enters in a zone.

Script example

The example displays a message when a player enters into the zone Zone0.

Zone0 = {}

function Zone0.OnEnter()
  Game.Message("Entering in the zone")
end


Zone.OnEnterBy(player)

[require Xmoto >= 0.3.0]

Explanation

This function is called when a player enters in a zone.

Script example

The example displays a message when a player enters into the zone Zone0.

Zone0 = {}

function Zone0.OnEnterBy(player)
  Game.Message("Player "..player.." enters the zone")
end


Zone.OnLeave()

Explanation

This function is called when a player leaves a zone.

Script example

The example displays a message when a player leaves the zone Zone0.

Zone0 = {}

function Zone0.OnLeave()
  Game.Message("Leaving the zone")
end

Zone.OnLeaveBy()

[require Xmoto >= 0.3.0]

Explanation

This function is called when a player leaves a zone.

Script example

The example displays a message when a player leaves the zone Zone0.

Zone0 = {}

function Zone0.OnLeaveBy(player)
  Game.Message("Player "..player.." leaves the zone")
end

Checkpoint.OnUse()

[require Xmoto >= 0.5.3]

Explanation

This function is called when the player respawns at a checkpoint.

Script example

The example displays a Message after a Checkpoint was used.

ckpoint1 = {}

function ckpoint1.OnUse()
  Game.Message("Loading checkpoint 1");
end


OnSomersault(bClockWise)

[require Xmoto >= 0.2.1]

Explanation

This function is called each time a player make a new somersault. bClockWise is 1 is the somersault is clockwise, 0 if counterclockwise.

Script example

The example displays a message when a player makes a somersault.

function OnSomersault(bClockWise)
  if(bClockWise == 1)  
  then
    Game.Message("ClockWise Somersault")
  else
    Game.Message("CounterClockWise Somersault")
  end
end


OnSomersaultBy(bClockWise, player)

[require Xmoto >= 0.3.0]

Explanation

This function is called each time a player make a new somersault. bClockWise is 1 is the somersault is clockwise, 0 if counterclockwise.

Script example

The example displays a message when a player makes a somersault.

function OnSomersaultBy(bClockWise, player)
  Game.Message("Nice ! player "..player)
end


OnWheel1Touchs(status), OnWheel2Touchs(status)

[require Xmoto >= 0.2.1]

Explanation

This function is called each time just the wheel 1 touchs the ground or stops to touch the ground. (status is 1 is the ground was not touching and is now touching, and 0 else)

Script example

The example displays the new max duration of a jump each time a new one is done.

max_jump_time = 1.0 -- start at 1 to not count smaller jumps
jump_begin    = 0.0
touch_1  = false
touch_2 = false

function OnWheel1Touchs(bStatus)
  if(bStatus == 1)  
  then
    updateMax()
    touch_1 = true
  else
    touch_1 = false
    jump_begin = Game.GetTime()
  end
end

function OnWheel2Touchs(bStatus)
  if(bStatus == 1)  
  then
    updateMax()
    touch_2 = true
  else
    touch_2 = false
    jump_begin = Game.GetTime()
  end
end

function updateMax()
  if(touch_1 == false and touch_2 == false)
  then
    if(Game.GetTime() - jump_begin > max_jump_time)
    then
      max_jump_time = Game.GetTime() - jump_begin
      Game.Message("New high jump: "..max_jump_time)
    end
  end
end


OnWheel1TouchsBy(status, player), OnWheel2TouchsBy(status, player)

[require Xmoto >= 0.3.0]

Explanation

This function is called each time just the wheel 1 touchs the ground or stops to touch the ground. (status is 1 is the ground was not touching and is now touching, and 0 else)

Script example

function OnWheel1TouchsBy(status, player)

 if(status == 1) 
 then
   Game.Message("Player "..player.." touches with wheel 1")
 end

end

function OnWheel2TouchsBy(status, player)

 if(status == 1) 
 then
   Game.Message("Player "..player.." touches with wheel 2")
 end

end


Callable Functions

GetTime()

Explanation

Return the time since the start of the level.

Script example

The example displays a message if the player takes more than 10 seconds to enter the zone.

Zone0 = {}

function Zone0.OnEnter()
  if Game.GetTime() > 10.0
  then
    Game.Message("10 seconds to come there, that's a lot !")
  end
end


Message(msgs)

Explanation

Display a message on the screen. The message is automatically remove after 5 seconds. You can call this function several times : the messages will be all displayed.

Script example

The example displays some messages at the start of the level.

function OnLoad()
  Game.Message("This level is scripted")
  Game.Message("GO GO GO !!!")
  return true
end


ClearMessages()

Explanation

Remove messages on the screen.

Script example

The example shows how to clear old messages and display a new one in some circumstances.

Zone0 = {}

function Zone0.OnEnter()
  Game.ClearMessages()
  Game.Message("OnEnter")
end

function Zone0.OnLeave()
  Game.ClearMessages()
  Game.Message("OnLeave")
end


IsEntityTouched(entity)

[require Xmoto >= 0.2.0]

Explanation

There is a function called when you touch an entity. But sometimes you want to know when you don't touch an entity. Use this function.

Script example

The example shows how to make an action when the player is not touching an entity.

g = -9.81

function OnLoad()
  Game.Message("Gravity is increasing while you are not touching the snowman")
  return true
end

function Tick()
  if(Game.IsEntityTouched("SnowMan0") == 0)
  then
    g = g - 0.03
    Game.SetGravity(0, g);
  end
  return true
end

Game.AddPenaltyTime(time)

[require Xmoto >= 0.5.0]

Explanation

Increment the value of the time counter to make a penalty.

Script example

In the following example, the time will be increased of 5 seconds if the player enters in the zone Zone0.

Zone0 = {}

function Zone0.OnEnter()
  Game.AddPenaltyTime(5.0)
end

RemainingStrawberries()

[require Xmoto >= 0.2.1]

Explanation

This function returns the number of strawberries remaining in the level.

Script example

In the following example, when the player will enter in the zone Zone0, the number of remaining strawberries is displayed.

Zone0 = {}

function Zone0.OnEnter()
  Game.Message(Game.RemainingStrawberries())
end



SetKeyHook(key, function)

Explanation

Whenever the player hits the "key" specified, a function will be called.

Script example

The example shows how to change gravity just by pressing a key.

g = -9.81

function OnLoad()
  Game.SetKeyHook("G", "GravityChange")
  return true
end

function GravityChange()
  g = g * -1
  Game.SetGravity(0, g)
end


GetKeyByAction(function)

Explanation

Return the key associated to an action. Possible actions are Drive, Brake, PullBack, PushForward, ChangeDirection.

Script example

The example displays how to drive at level start.

function OnLoad()
  Game.Message("To drive, press "..Game.GetKeyByAction("Drive"))
  return true
end

Log(msgs)

Explanation

Log a message in the xmoto.log.

Script example

The example log the message "An error occured" when the level starts.

function OnLoad()
  Game.Log("An error occured")
  return true
end