Jump to content

A couple questions


Xdr

Recommended Posts

Hello,

I've started to pick up on the Lua language in the past couple of days and have a few questions;

1. Is it possible to hook functions? For example, if I wanted to make the function setPlayerMoney actually call a custom function I created, and not do what it actually does, natively.

2. Is it possible to hook events? Like with function hooks, but BOTH the custom function I create and the event that is inside the gamemode script will be called. (Or does this not need to be a problem, as it will be called anyway in the resource?) Specifically, I'm looking to hook the event when the player spawns.

I'm sorry if anything what I'm asking is confusing, I'm use to using terms for the Pawn language, so may mix a few things up here and there. If I'm giving things the wrong names, please, let me know!

Thanks.

Link to comment

1. Easy, functions work just like variables in Lua, and you can overwrite them locally.

function setPlayerMoney () -- insert your variables you want in here, like "player" and "money" 
    --insert your code 
end 

If you still want to use the original setPlayerMoney function, save it somewhere first:

setPlayerMoneyOriginal = setPlayerMoney 
function setPlayerMoney ( player, addmoney ) 
    local money = getPlayerMoney ( player ) 
    setPlayerMoneyOriginal ( player, money + addmoney ) 
end 

2. You can trigger your own custom event then:

  
function a () 
  local player = source 
  triggerEvent ( "onCustomPlayerJoin", root, player ) 
end 
addEventHandler("onPlayerJoin", root, a ) 
  
function b (player) 
  outputChatBox("Joined: " .. getPlayerName(player)) 
end 
addEvent ( "onCustomPlayerJoin" ) 
addEventHandler("onCustomPlayerJoin", root, b) 

Link to comment
There is no such "onCustomPlayerJoin" event on MTA Wiki

Yes, thanks for stating the obvious.

@Xdr About 2, I'm not sure what you meant and I never touched Pawn so I wrote something that could be wrong. Could you explain more'?

Link to comment

Thanks SDK. I remember reading functions worked as variables, but didn't know that they could be overwritten that easily.

As for the second question, in Pawn, when you included a library (which is similar to a "resource" that provides functions) and wanted to use a callback (event) such as onPlayerConnect, you would need to "hook" the callback in the library. For example, if in the main gamemode script someone had onPlayerConnect callback, and the library also used the callback, then you'd have to hook it, or call a separate function (such as onPlayerConnectEx (a custom function)). The method of hooking would eliminate this, so when the main gamemode script read the onPlayerConnect callback, it would also automatically call the one from inside the library. (It's not as complicated as it seems, I just can't find the right words to explain it)

However, I don't believe that will be a problem, as resources are used differently, in Lua. I'm assuming I could run a separate resource with an event, regardless of whether or not it's already in another resource (like a gamemode)? If so, I don't need any guidance on the second question.

Once again, thanks, it's much appreciated! If I have any more questions, I'll post again.

EDIT: I'm sorry, but I missed the whole reason I posted in the first place :P

Included in my first question, I meant, is it possible to replace the function with all functions not only in my resources, but all that loaded on the server. So if they included my resource, then all of the resources that use that function will call the "hooked" (or custom/replaced) function.

Link to comment

I still don't get why there are humans learning or scripting in Pawn.

Anyway,

I will try to help you, if i understood you correctly.

Library in Lua:

Well, a library as you said is something like a resource, that provides functions.

Well, here it's a resource that provides exported functions which can be used by another resources.

A script file in the resource (library):

function setPlayerMoney(player, money) 
--code 
end 
  
function sayHi(player) 
outputChatBox("Hi.", player) 
end 

Those the functions we want to use in another resources.

In order to do so, you have to export the functions:

This exports functions from this resource, so other resources can use them with call

function: The function name

type Whether function is exported server-side or client-side (valid values are: "server" and "client")

http: Can the function be called via HTTP (true/false)

meta.xml:

<export function="setPlayerMoney" type="server" /> 
<export function="sayHi" type="server" /> 

And you are done, now both functions can be used in any script file, but it must be server side, because we specified the type "server".

Way of using the exported functions.

exports.resourceName.functionName(arguments) 

That's it, resourceName is the name of the resource which have the exported functions, functionName is the function name we want to use, so it will be something like:

i will call the resource exportedFunctions.

addEventHandler("onPlayerJoin", root, 
function() 
exports.exportedFunctions.sayHi(source) 
end) 

Events:

Example of making a new event, using it in another resource.

addEvent("onPlayerJoinEx") 
addEventHandler("onPlayerJoin", root, 
function() 
triggerEvent("onPlayerJoinEx", source) 
end) 

In another resource

addEvent("onPlayerJoinEx") 
addEventHandler("onPlayerJoinEx", root, 
function() 
outputChatBox("Hi.", source) 
end) 

Source here will be the player who joined, why?, because we specified the second argument @ triggerEvent "source".

Which is the player who joined in onPlayerJoin, so now onPlayerJoinEx 's source is the player who joined.

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