Jump to content

[Help] Scripting Concepts


Army@1

Recommended Posts

Vector is an array of numbers (V2 has two variables, V3 has three and so on).

Vector examples: (1, 2, 3) ; (4, 5)

Vector addition: (4, 5, 6) + (1, 2, 3) = (5, 7, 9)

Vector subtraction: (4, 5, 6) - (1, 2, 3) = (3, 3, 3)

Vector multiplication: (4, 5, 6) * (1, 2, 3) = (4, 10, 18)

Vector division: (4, 5, 6) / (1, 2, 3) = (4, 2.5, 2)

Scalar vector multiplication: 2 * (1, 2, 3) = (2, 4, 6)

Vectors are a type of matrices, but they're one dimensional. Matrices are actually multi-dimensional.

Example: fe4dce294d36a7791e984b51818639c6.png

Addition: 671bc3a25c671495de4a09f189e5a6c6.png

Scalar multiplication: 6986f615791c2a3aa366891ab08ab865.png

For multiplication and division, matrices do not have to the same size. Search 'matrices' on Google for more info.

Link to comment
BTW, in lua, whats the different between just 'function' and 'local function'. I know about variables but functions seems bit confusing to me as I hear we can called them outside scope too.
function helloWorld() 
  print("Hello World") 
end 
-- is the same as 
helloWorld = function() 
  print("Hello World") 
end 
  
-- Therefore, 
local function helloWorld() 
  print("Hello World") 
end 
-- is the same as 
local helloWorld = function() 
  print("Hello World") 
end 

A local function is just a function but assigned to a local variable.

Link to comment
BTW, in lua, whats the different between just 'function' and 'local function'. I know about variables but functions seems bit confusing to me as I hear we can called them outside scope too.
function helloWorld() 
  print("Hello World") 
end 
-- is the same as 
helloWorld = function() 
  print("Hello World") 
end 
  
-- Therefore, 
local function helloWorld() 
  print("Hello World") 
end 
-- is the same as 
local helloWorld = function() 
  print("Hello World") 
end 

A local function is just a function but assigned to a local variable.

Thanks but my actual question was that both are similar,so when to use one or another. I already got that as I did a lot of research on it.

BTW,

  
function helloWorld() 
  print("Hello World") 
end 
-- is the same as 
helloWorld = function() 
  print("Hello World") 
end 
  
-- Therefore, 
local function helloWorld() 
  print("Hello World") 
end 
-- is not 
local helloWorld = function() 
  print("Hello World") 
end 
-- but 
local helloWorld  
helloWorld = function() 
  print("Hello World") 
end 
  

If my understanding is to be believe.

And @Revolt, thanks, I got that but when/where to use that in MTA scripting? :roll:

Link to comment

It is actually the same. You can think of a function as just a variable when executed ( helloWorld() ), will cause something to happen. By declaring helloWorld local, you're automatically setting your function to be local as well, since it doesn't exist outside the helloWorld variable.

-- file1.lua 
local abc = function () 
   print("hello world") 
end 
hello = abc 
  
-- file2.lua 
hello() -- you'll be able to use 'hello', but not 'abc' 
local secondVariable = hello -- you will not be able to use 'secondVariable' in file1 

Link to comment
f = function() end -- this is accessible across the whole resource-side (i.e. all server/client-side files of a resource) 
  
local f 
f = function() end -- this is accessible to the whole file 
  
local f = function() end -- this is accessible to the whole file 
  
if true then 
  local f = function() end -- this is accessible to the scope it's on only (the if-then-end scope). 
end 

Link to comment

Thanks MrTasty.

I guess that's not the same because in the previous case the function can call itself from within itself, yet in the other one it cannot (since it is being declared at the time the function is being declared).

Oh, so the latter is useful and should be use when one may need to call the function inside the same function. Thanks.

But can you please answer the following?

And @Revolt, thanks, I got that but when/where to use that in MTA scripting?

Thanks both of you guys I am learning more and more. :)

Link to comment

Matrices are useful for finding the front of an object, for example.

localPlayer.vehicle.matrix.forwards * 5 = 5 units forwards 

Vectors are for positioning or velocities

Vector3(0,0,1) -- could mean position X:0, Y:0: Z:1 or could mean Z:+1 velocity (m/s maybe?) depending on how you use it (setElementPosition or setElementVelocity) 

Link to comment
Matrices are useful for finding the front of an object, for example.
localPlayer.vehicle.matrix.forwards * 5 = 5 units forwards 

Vectors are for positioning or velocities

Vector3(0,0,1) -- could mean position X:0, Y:0: Z:1 or could mean Z:+1 velocity (m/s maybe?) depending on how you use it (setElementPosition or setElementVelocity) 

Thanks, I think I got the vectors concept. If I did then

  
setElementPosition( player, x, y, z) -- here x,y,z are vectors 
--so we can do 
x,y,z = vector3(0,0,0) 
setElementPosition( player, x, y, z) -- player will spawn at origin 
  

If I didn't, how can I use vectors to set element's position and velocity?

Link to comment

You can replace the x, y, and z arguments with one vector argument, i.e.

    setElementPosition( player, Vector3( 1, 2, 3 ) ) 
  
-- or 
  
    local pos = Vector3( 1, 2, 3 ) 
    setElementPosition( player, pos ) 
  
-- or (only in OOP) 
  
    player.position = Vector3( 1, 2, 3 ) 

Link to comment
You can replace the x, y, and z arguments with one vector argument, i.e.
    setElementPosition( player, Vector3( 1, 2, 3 ) ) 
  
-- or 
  
    local pos = Vector3( 1, 2, 3 ) 
    setElementPosition( player, pos ) 
  
-- or (only in OOP) 
  
    player.position = Vector3( 1, 2, 3 ) 

Wow, that really helped me. Thanks.

Matrices are useful for finding the front of an object, for example.
localPlayer.vehicle.matrix.forwards * 5 = 5 units forwards 

Is that matrix one dimension i.e vector? If yes, why do we need matrice? we can easily work out the same problem by using vectors.

And also vector3 have x, y, z then what does 3x2 or above matrices have i.e x,y,z,_,_,_?

Link to comment

Vectors go from two dimensions up to four dimensions (xy, xyz, xyzw).

Noki executed command: Player("Noki").matrix

Command results: Matrix: { -0.196, 0.981, -0.000 } { -0.981, -0.196, 0.000 } { -0.000, 0.000, 1.000 } { 2363.031, -1439.156, 31.498 } [userdata]

Let's say I wanted to get a position that's in front of me:

local pos = player.position 
local r = player.rotation 
local x = pos.x - math.sin(math.rad(r.z)) * 4 
local y = pos.y + math.cos(math.rad(r.z)) * 4 
player.position = Vector3(x, y, pos.z) 

If I used matrices:

local pos = player.matrix.position + player.matrix.forward * 4 
player.position = pos 

Link to comment

Oh yeah, I got it.

What would be the outcome if we use '+' instead of '*'? As if I understand correctly '* 4* would get position 4 blocks in front of the player then what would '+4' do?

Noki executed command: Player("Noki").matrix

Command results: Matrix: { -0.196, 0.981, -0.000 } { -0.981, -0.196, 0.000 } { -0.000, 0.000, 1.000 } { 2363.031, -1439.156, 31.498 } [userdata]

Whose coordination's are these? Player's position,rotation or something?

Link to comment
What would be the outcome if we use '+' instead of '*'? As if I understand correctly '* 4* would get position 4 blocks in front of the player then what would '+4' do?
You can't add a number to a vector. However, you can add a Vector to a Vector.

If we assume adding 4 means adding a 3-Dimensional Vector with all values set to 4, then you'll simply increment the vector by 4 in all absolute (not relative to the context) directions (resulting in a position 6.92820323028 units to the north-east, slightly upwards)

Link to comment

Okay.

But what are these co-ordinates?

Command results: Matrix: { -0.196, 0.981, -0.000 } { -0.981, -0.196, 0.000 } { -0.000, 0.000, 1.000 } { 2363.031, -1439.156, 31.498 }

If { -0.196, 0.981, -0.000 } is player's position(x,y,z) then what are others? rotations? velocities? I mean matrix is consist of 4 sets of x,y,z's and I find it hard to understand what it is actually.

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...