Jump to content

I'm confused. Unclear scripting and wiki guide?


Recommended Posts

Okay so I did scripting a while back for MTA:SA but never actually knew what I was doing, just more or less copied + pasted code from others and made it work. How ever I'm now fully experienced and I'm just lost on a few things if people could clear up that would be great, thank you!

Okay so usually in programming languages like C or Java you provide the parameters, and I noticed in MTA you don't always do so, like this for example:

  
function whenPlayerJoins(player) 
  
end 
  
--Now here we have the event, getRootElement(), and the function. But why isn't the function like so: 
--whenPlayerJoins(source) or something similar? 
addEventHandler ( "onPlayerJoin", getRootElement(), whenPlayerJoins ) 
  
  
  

Also there are certain things on the wiki which don't make much sense to me like setCameraMatrix, I'm guessing this just suspends the player's camera in a location, however when you actually want to set the player's camera back to his body how can you do such things? It doesn't really say :/

How can I determine these things without asking here? Where on the wiki can I get the information I need for resetting a player's camera etc...

Link to comment

Actually the event onPlayerJoin doesn't have parameters, you can see this in the wiki.

https://wiki.multitheftauto.com/wiki/OnPlayerJoin

Parameters:

No parameters.

-----------------------------------------------

Source:

The source of this event is the player who joined.

source is an hidden variable, you don't need to define it.

There is a list of predefined variable:

https://forum.multitheftauto.com/viewtopic.php?f=91&t=39678

-----------------------------------------------

And about setCameraMatrix, the wiki say it:

https://wiki.multitheftauto.com/wiki/SetCameraMatrix

This function sets the camera's position and direction. The first three arguments are the point at which the camera lies, the last three are the point the camera faces (or the point it "looks at").

Note: Calling this function takes the camera's focus away from the player and sets the camera in a fixed position and rotation. The camera's focus can be brought back to the player using the setCameraTarget function.

Link to comment
Actually the event onPlayerJoin doesn't have parameters, you can see this in the wiki.

https://wiki.multitheftauto.com/wiki/OnPlayerJoin

Parameters:

No parameters.

-----------------------------------------------

Source:

The source of this event is the player who joined.

source is an hidden variable, you don't need to define it.

There is a list of predefined variable:

https://forum.multitheftauto.com/viewtopic.php?f=91&t=39678

-----------------------------------------------

And about setCameraMatrix, the wiki say it:

https://wiki.multitheftauto.com/wiki/SetCameraMatrix

This function sets the camera's position and direction. The first three arguments are the point at which the camera lies, the last three are the point the camera faces (or the point it "looks at").

Note: Calling this function takes the camera's focus away from the player and sets the camera in a fixed position and rotation. The camera's focus can be brought back to the player using the setCameraTarget function.

Oh okay so there are parameters but they're like hidden ones? okay, I kinda understand now :D thanks

Link to comment

Parameters is not hidden, they're what we put between the brackets. The hidden is the predefined variable only.

So for example the event onMarkerHit:

Parameters:
element hitElement, bool matchingDimension 

hitElement: The element that hit the marker

matchingDimension: True if the element is in the same dimension as the marker he hit

So the function is like this:

function WhateverName() 

If you wanted to use the parameters, you will put them in the brackets:

function WhateverName(hitElement, matchingDimension) 

If you not going to use matchingDimension you can remove it:

function WhateverName(hitElement) 

If you not going to use hitElement you can remove it and it would look like:

function WhateverName(_, matchingDimension) 

The name does not matter, you can do this:

function WhateverName(whatever1, whatever2) 

Also note that you must supply all parameters before the one you wish to use.

Link to comment

Events have 'hidden' variables, one of which is 'source' (more info about them: https://wiki.multitheftauto.com/wiki/Event_system). 'Hidden' variables are actually global variables that are set before the handler is executed and unset after it's done. Every event has source which is an element related to that event (In case of "onPlayerJoin" it's the player who joined), and if there's additional data, it's passed via function parameters.

As for "providing parameters" in 'addEventHandler', no, you don't provide parameters when you pass the function as a value.

-- this will call 'someFunction' and pass 'someOtherFunction' as an argument: 
someFunction(someOtherFunction)  
  
-- this will use the return value of 'someOtherFunction' as the argument for 'someFunction': 
someFunction(someOtherFunction(someArgument)) 
  
-- this has the same effect as 'someFunction(someOtherFunction(someArgument))': 
local returnval = someOtherFunction(someArgument) 
someFunction(returnval) 

When you pass 'whenPlayerJoins' as an argument to 'addEventHandler', that means MTA will call 'whenPlayerJoins' function every time "onPlayerJoin" event is triggered. The parameters passed to the handler function depend on event. It's not so much different from what you can do in C:

void someFunction(int (*funcPtr)(int funcArg)); 
int (*someOtherFunction)(int funcArg); 
  
int main() 
{ 
    someFunction(someOtherFunction); 
    return 0; 
} 

(Not sure if syntax in this example is correct)

Basically, if you have 'someFunction' and 'someOtherFunction' defined, this will call 'someFunction', passing 'someOtherFunction' as an argument, and if 'someFunction' calls 'funcPtr', it will call the function that was provided. The difference is that C, as a low level language, requires function parameter lists to be predefined and matching (both 'funcPtr' and 'someOtherFunction' accept one integer), while Lua doesn't make it all that strict.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...