Keitboor

Tutorial #3 - Crazy Clothes

Recommended Posts

 

 

mta-sa-dm-tutorials.gif

 

Vidéo :

 

 

 

  • Le script complet est accessible à cette adresse : Script3.lua
  • Sujet traitant de ce script sur le site officiel : Cliquez ici

Counting the clothes

 

First, we are going to count how many items there are in each category, and store the counts in this table:

 

 

CODE
local count = {}

We have 18 clothing types to check (these will be documented along with the rest of IDs on release) :

 

 

CODE
for type = 0, 17 do

--initialize a counter

local clothes = 0

 

--we increase the counter until it's an invalid clothing ID

while getClothesByTypeIndex ( type, clothes ) ~= false

do clothes = clothes + 1 end

 

--remember the last one didn't exist: we've got to remove it

--from the count when storing it

count[type] = clothes - 1

end

Note : This step may be replaced by declaring a hardcoded table with fixed clothes counts, as all unmodded games have the same number of clothes in each category.

 

Creating the clothes change function

 

Now we'll create the clothes changing function itself. It will have to take a few things into account, which will be described in a moment. The declaration starts here :

 

 

CODE
function doChangeClothes ()

Now, we're going to run it for every connected player. This is done by first retrieving a table of every player in the server, then looping through that table and changing their clothes. First we retrieve a table of our players:

 

 

CODE
local players = getElementsByType ( "player" )

Next we use Lua's generic for loop to go through every player in that table.

 

 

CODE
for i, player in players do

First, we have to choose a random type using Lua's math.random. We'll leave tattoos (type IDs 4-12) out because they're not visible with clothes on anyways :

 

 

CODE
local accessory = math.random ( 0, 1 )

if accessory == 1 then

 

--pick an accessory type

type = math.random ( 13, 17 )

 

else

--pick hairstyles/shirts/trousers/shoes type

type = math.random ( 0, 3 )

 

end

Complete suits (type ID 17) are applied on top of shirts and trousers, so we'll want to remove them if any of these two are selected:

 

 

CODE
if type == 0 or type == 2 then

 

removePlayerClothes ( player, 17 )

end

Now that we've got our type, we have to select a random piece of clothing within it:

 

 

CODE
local clothing = math.random ( 0, count[type] )

Only adding these clothes to the player is left to do.

 

 

CODE
--we get the texture and model via getClothesByTypeIndex

texture, model = getClothesByTypeIndex ( type, clothing )

--and we add that piece of clothing to the player.

addPlayerClothes ( player, texture, model, type )

We mustn't forget to close the "for" loop, and the function declaration.

 

 

CODE
end

end

Starting the function

 

Lastly, we'll trigger our function repeatedly when this script "resource" (these will be explained later) is started.

 

 

CODE
addEventHandler("onResourceStart", getRootElement(), "clothesStart")

function clothesStart ( resourcename )

Our clothesStart function should only be triggered when this script loads. The following line makes the script ignore any resource loads other than its own :

 

 

CODE
if resourcename ~= getThisResource() then return end

Finally, we'll employ the setTimer function, as in the last tutorial, to trigger the function every two seconds.

 

 

CODE
--0 means infinite times, until the timer is killed

setTimer ( "doChangeClothes", 2000, 0 )

end

Partager ce message


Lien à poster
Partager sur d’autres sites

Créer un compte ou se connecter pour commenter

Vous devez être membre afin de pouvoir déposer un commentaire

Créer un compte

Créez un compte sur notre communauté. C’est facile !

Créer un nouveau compte

Se connecter

Vous avez déjà un compte ? Connectez-vous ici.

Connectez-vous maintenant