Jump to content

Scripting Tutorial 1 - User selectable warp points


ijsf

Recommended Posts

These tutorials are more showing you how to logically do things instead of how the system actually works, you'll be able to look the hundreds of pages of information we have on it once its complete, but for now;

An event-handler is what it says on the tin, it just tells MTA to call a function you've made with the pre-defined arguments-list when a certain event occurs.

Arguments for addEventHandler: the first is the event you want to redirect, the second is to do with our very in-depth element system, which you can ignore for now, and the final is the function you want to redirect the particular event too.

I imagine most of it will seem complicated at first glance, there wasn't so many functions/events when i first looked at it so i gradually picked everything up as it was being written.

The best way to learn about it all is to just wait 'til it's released, then you can try things out at the same time.

Link to comment
  • Replies 64
  • Created
  • Last Reply

Top Posters In This Topic

Just curious, not sure exactly what built-in functions are available in LUA, and what functions you devs have added. But I think it might be a good idea to list function arguments somewhere (similar to the function description at php.com)

I'm sure many will fool around with the code a bit when trying this out the first time, making minor changes everywhere.

Take for instance:

setElementPosition ( player, x, y, z )

What if that function has optional arguments that could be useful?

One more thing, object placement works pretty much the same as this except you have to account for rotation as well?

Have a pretty clear idea for a cops and robbers script...

Link to comment
Just curious, not sure exactly what built-in functions are available in LUA, and what functions you devs have added. But I think it might be a good idea to list function arguments somewhere (similar to the function description at php.com)

I'm sure many will fool around with the code a bit when trying this out the first time, making minor changes everywhere.

Take for instance:

setElementPosition ( player, x, y, z )

What if that function has optional arguments that could be useful?

One more thing, object placement works pretty much the same as this except you have to account for rotation as well?

Have a pretty clear idea for a cops and robbers script...

You use a different function to set rotations, anyway as MrJax said you'll be able to see the documentation for the functions (and their arguments) once it's complete.

edit: opps, kevuwk beat me to it

Link to comment

Ok, little question about:

[lua]bindKey ( player, "i", "down", "modeIO" )[/lua]

What happens, when you have already bound the I-key to something in the DM or SP options? For example, if I shoot with it (no I don't, but it is just an example), will I both shoot and show the cursor, or only do one of these?

Also if I replace [lua]"down"[/lua] with [lua]"up"[/lua] the function modeIO (from the script) will be executed when I release the key, right?

Link to comment
Ok, little question about:

[lua]bindKey ( player, "i", "down", "modeIO" )[/lua]

What happens, when you have already bound the I-key to something in the DM or SP options? For example, if I shoot with it (no I don't, but it is just an example), will I both shoot and show the cursor, or only do one of these?

Also if I replace [lua]"down"[/lua] with [lua]"up"[/lua] the function modeIO (from the script) will be executed when I release the key, right?

not sure about the i, but down/up is the key state i think

Link to comment
Ok, little question about:

[lua]bindKey ( player, "i", "down", "modeIO" )[/lua]

What happens, when you have already bound the I-key to something in the DM or SP options? For example, if I shoot with it (no I don't, but it is just an example), will I both shoot and show the cursor, or only do one of these?

Also if I replace [lua]"down"[/lua] with [lua]"up"[/lua] the function modeIO (from the script) will be executed when I release the key, right?

Yes, "up" will have the affect of triggering it when the button is released.

We'll be sorting out the issue of conflicting controls in time for release, so you wont have to worry about that :)

Link to comment

I readed through this, but there is one thing i don't understand...

addEventHandler ( "onPlayerJoin", getRootElement(), "Script_onPlayerJoin" ) --This event tells what happens when a player joins.

function Script_onPlayerJoin ()

bindKey ( source, "i", "down", "modeIO" ) --Bind the player's "i" key to the function "modeIO" when the key is pressed

end

where does "source"-variable come from? I'm not a Lua programmer yet, so I don't know about declarations, but what tells this function whats in "source"?

Or is "source" and "player" something like a pre-declared variable?

Please explain...

Lennard Fonteijn

Link to comment
I readed through this, but there is one thing i don't understand...
addEventHandler ( "onPlayerJoin", getRootElement(), "Script_onPlayerJoin" ) --This event tells what happens when a player joins.

function Script_onPlayerJoin ()

bindKey ( source, "i", "down", "modeIO" ) --Bind the player's "i" key to the function "modeIO" when the key is pressed

end

where does "source"-variable come from? I'm not a Lua programmer yet, so I don't know about declarations, but what tells this function whats in "source"?

Or is "source" and "player" something like a pre-declared variable?

Please explain...

Lennard Fonteijn

Good question. "source" always refers to the 'source' of the event. 99% of the time the 'source' is refered to in the event itself - onPlayerJoin - the "source" by default is the variable which refers to the player.

onPlayerClick is the same - the source will be the player.

However, in the onMarkerHit event, the source will refer to the marker as that is what the event refers to.

So yes, "source" is always a predeclared variable. "player" however is not.

I hope that makes sense.

Link to comment

I have a few questions (I'm a beginner):

- I know MTA has a built in interpreter, but what happen if we compile the script ? Will MTA be able to read it ?

- Will alt-tab authorized ? I think it's not in MTA race, except in the main menu, but I mean when you are playing. Because if you want to test your script, then you'll have to launch MTA, connect to your server, and if there is a little problem in the script, it's easier to alt-tab the game, fix the bug, reload the gamemode and come back in the game, than leaving the game.

- Can you explain the last 5 numbers of these two lines ? I guess some are the color, some are the size, "corona" is the type, but I don't know which one is the color, the size ... (it would be cool if you explain it in the tutorials, so we know what we can change or not) :

[lua]-- create a glowing corona

local marker = createMarker ( x, y, z+1, "corona", 1, 0, 255, 255, 50 )

-- create a normal cylinder marker

local marker = createMarker ( x, y, z, "cylinder", 2, 0, 255, 0, 50 )[/lua]

- How will a player know that a key is binded ? Does the scripter have to tell it to the player with I-Don't-know-what-funtion that show a message to the player, or will it be automatic (or will the player have to test all the keys when he joins a new server ? O_o ^^) ?

Link to comment
  • MTA Team
What happens, when you have already bound the I-key to something in the DM or SP options? For example, if I shoot with it (no I don't, but it is just an example), will I both shoot and show the cursor, or only do one of these?

Assignment of the last script executed is the one that counts. So bindings from inside the game would get lost in favor of the script. However we did include another function to check if a key is bound. So a decent scripter would check if a key is free instead of just assigning it.

Link to comment

Ok, to continue a bit about the "source"-variable.

So every event will be issued with a "source"-var.... also own created events?

And is the "source"-var always a single data string or can it also be an array (Example: like it can contain the playerid, but also his location).

Will there come a documentation where the "source" referes to in an event?

And in this part:

[lua]function Script_onResourceLoad ( resource )

if ( resource == getThisResource() ) then

-- for each player already in the server

for index, player in getElementsByType ( "player" ) do

-- binds the "i"-key to our function "modeIO"

bindKey ( player, "i", "down", "modeIO" )

end

end

end[/lua]

What tell the script what "player" is? Or "index", isn't there something like a declaration?

I'm sorry about the noobish questions, but I think i'm too used to SA-MP scripting.

Link to comment
Ok, to continue a bit about the "source"-variable.

So every event will be issued with a "source"-var.... also own created events?

And is the "source"-var always a single data string or can it also be an array (Example: like it can contain the playerid, but also his location).

Will there come a documentation where the "source" referes to in an event?

And in this part:

function Script_onResourceLoad ( resource )
   if ( resource == getThisResource() ) then
       -- for each player already in the server
       for index, player in getElementsByType ( "player" ) do
           -- binds the "i"-key to our function "modeIO"
           bindKey ( player, "i", "down", "modeIO" )
       end
   end
end

What tell the script what "player" is? Or "index", isn't there something like a declaration?

I'm sorry about the noobish questions, but I think i'm too used to SA-MP scripting.

2 arguments are returned for each player and they are put into those variables

Link to comment

The line:

[lua]poo = getElementsByType ( "player" )[/lua]

Returns a table/array of players stored in variable poo

So 'poo[1]' returns 'Aeron' poo[2] returns 'Blokker'

Now lua has a special 'for'-loop syntax to iterate trough such tables:

'for index,value in table do'

Making:

[lua]for index, player in poo do

outputChatbox("Index: ".. index .. " Value: " .. getClientName(player), root )

end[/lua]

Output:

Index: 1 Value: Aeron

Index: 2 Value: Blokker

Link to comment

Now that's the kind of replies I like, thanks Aeron!

Ok, so this is the idea...

----

addEventHandler (event, getRootElement(), function)

addEventHandler = Let the script know you want to activate a function with a special event

event = The name of the event, actually the trigger for the function

getRootElement() = Holds the information which gets sended to the function (can be an array which holds more information).

(I guess this is a XML function, but from what kind of XML file it gets the root? And what does it return?)

function = The function to activate

Correct if im wrong.

Link to comment
Now that's the kind of replies I like, thanks Aeron!

Ok, so this is the idea...

----

addEventHandler (event, getRootElement(), function)

addEventHandler = Let the script know you want to activate a function with a special event

event = The name of the event, actually the trigger for the function

getRootElement() = Holds the information which gets sended to the function (can be an array which holds more information).

(I guess this is a XML function, but from what kind of XML file it gets the root? And what does it return?)

function = The function to activate

Correct if im wrong.

The last element is actually a string with functionname. Edited by Guest
Link to comment
Now that's the kind of replies I like, thanks Aeron!

Ok, so this is the idea...

----

addEventHandler (event, getRootElement(), function)

addEventHandler = Let the script know you want to activate a function with a special event

event = The name of the event, actually the trigger for the function

getRootElement() = Holds the information which gets sended to the function (can be an array which holds more information).

(I guess this is a XML function, but from what kind of XML file it gets the root? And what does it return?)

function = The function to activate

Correct if im wrong.

You're right about the first and third args, the second is an object which we will explain more about later.

Is there, or will there come a list of all the events and functions and stuff available in MTA? And if so: where?

Thanks.

Yes there is, but the pages are still under construction so it's not public yet.

Link to comment
Is there, or will there come a list of all the events and functions and stuff available in MTA? And if so: where?

Thanks.

at release we will have a site set up that lists every function and documentation of it. The QA team is working on that now.

Link to comment
Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.

×
×
  • Create New...