Difference between revisions of "Client side scripts"

From Multi Theft Auto: Wiki
Jump to: navigation, search
 
 
(21 intermediate revisions by 14 users not shown)
Line 1: Line 1:
Client side scripts are scripts that run inside the deathmatch mod client side. This means that have access to more information about the game world, but slightly less information about the rest of the players in the game.
+
Client side scripts are scripts that run inside the deathmatch mod client side. This means that the game has access to more information about the game world, but slightly less information about the rest of the players in the game.
  
==Why are we implementing client scripts now?==
+
This is useful for things that need to be done client side, such as visual effects, creation and manipulation of GUI elements.
We had planned to do client scripts in a future release, but its become apparent that it may be the fastest way to make a new editor for deathmatch. The alternatives we've got for the editor are:
 
* Hack the race editor to work with DM: This is possible, but would take a lot of work. The editor currently doesn't even compile with the dm code base and it doesn't support many things that are in dm. The old race editor is a mess code-wise and as a consequence really needs a rewrite to add anything new to it.
 
* Write a new editor using wxwidgets. This would be ideal, but its clear that this is actually a larger task than we envisioned. We may do this in the future, but we don't have the time right now if we want a release within the next month or so.
 
* Implement an editor in server side scripts. This could work, but the server is not really aware enough of the client to make it a smooth enough experience. This would be the fastest to do, much of this has already been done.
 
* Implement client scripts. This is what we intend to do. The advantages are that its something we intended to do in the future anyway, it hopefully won't take too long and we can "open source" the editor script and allow QA to help us write it. This allows us to focus on just writing functionality client and server side, while letting 3rd parties write our scripts for us!
 
  
==How will it work?==
+
==How does it work?==
It is intended that client side scripts will follow the same pattern as server side scripts. We will provide as many as the same functions client side as exist server side, and we'll provide a simple way of interfacing between the two halves of a script, using the same event system as we already have. Client and server scripts will either have to be in two separate .lua files or within the .map file in a <script> tag.
+
Client side scripts follow the same pattern as server side scripts. We will try to provide the necessary functionality for client side scripts. Interfacing between a server side and client side script is done by using the same event system as we already have. The server side and client side scripts will need to be in two different files, which are included from the resource (in the metafile) by using a <script> tag (and type attribute).
  
 
For example:
 
For example:
 +
 +
'''meta.xml'''
 
<syntaxhighlight lang="xml">
 
<syntaxhighlight lang="xml">
<map>
+
<meta>
    <script src="race_clientside.lua" runat="client" />
+
<script src="c_gui.lua" type="client" />
    <script src="race_serverside.lua" />
+
<script src="s_gui.lua" type="server" />
</map>
+
</meta>
 
</syntaxhighlight>
 
</syntaxhighlight>
  
If you wanted to trigger a client side event from the server, you would first have to register the client side event using addEvent. Then, you can attach a handler to the event as you would in a server side script. Then in the server side script, you'll be able to call triggerClientEvent ( player, "eventName", element, args ... ) which will trigger the event client side. The same will be able to be done in reverse.
+
If you wanted to trigger a client side event from the server, you would first have to register the client side event using [[addEvent]]. Then, you can attach a handler to the event as you would in a server side script. Then in the server side script, you'll be able to call [[triggerClientEvent]], which will trigger the event client side. The same can be done in reverse using [[triggerServerEvent]].
  
 
For example:
 
For example:
'''Client side:'''
+
 
 +
'''Client side'''
 
<syntaxhighlight lang="lua">
 
<syntaxhighlight lang="lua">
addEvent("ShowObjectBrowser", "id")
+
function showObjectBrowser ( id )
addEventHandler("ShowObjectBrowser", getRootElement(), "showObjectBrowser")
+
-- code here
function showObjectBrowser(id)
 
  -- code here
 
 
end
 
end
 +
addEvent( "doShowObjectBrowser", true )
 +
addEventHandler( "doShowObjectBrowser", getRootElement(), showObjectBrowser )
 
</syntaxhighlight>
 
</syntaxhighlight>
'''Server side:'''
+
 
 +
'''Server side'''
 
<syntaxhighlight lang="lua">
 
<syntaxhighlight lang="lua">
triggerClientEvent ( source, "ShowObjectBrowser", getRootElement(), 1034 )
+
triggerClientEvent ( somePlayer, "doShowObjectBrowser", getRootElement(), 1034 )
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==More?==
+
[[Category:Scripting Concepts]]
First draft, pester me (eAi) to answer anything I've missed or isn't clear.
+
 
 +
[[hu:Client side scripts]]
 +
[[ru:Client side scripts]]
 +
[[it:Script client-side]]
 +
[[es:Scripts de Cliente]]

Latest revision as of 14:45, 24 October 2018

Client side scripts are scripts that run inside the deathmatch mod client side. This means that the game has access to more information about the game world, but slightly less information about the rest of the players in the game.

This is useful for things that need to be done client side, such as visual effects, creation and manipulation of GUI elements.

How does it work?

Client side scripts follow the same pattern as server side scripts. We will try to provide the necessary functionality for client side scripts. Interfacing between a server side and client side script is done by using the same event system as we already have. The server side and client side scripts will need to be in two different files, which are included from the resource (in the metafile) by using a <script> tag (and type attribute).

For example:

meta.xml

<meta>
	<script src="c_gui.lua" type="client" />
	<script src="s_gui.lua" type="server" />
</meta>

If you wanted to trigger a client side event from the server, you would first have to register the client side event using addEvent. Then, you can attach a handler to the event as you would in a server side script. Then in the server side script, you'll be able to call triggerClientEvent, which will trigger the event client side. The same can be done in reverse using triggerServerEvent.

For example:

Client side

function showObjectBrowser ( id )
	-- code here
end
addEvent( "doShowObjectBrowser", true )
addEventHandler( "doShowObjectBrowser", getRootElement(), showObjectBrowser )

Server side

triggerClientEvent ( somePlayer, "doShowObjectBrowser", getRootElement(), 1034 )