UE4SS Function Overview
UE4SS comes with a built-in Lua API. Part of that is an array of useful functions that let us do cool stuff. In this section we're going to cover some of the more useful functions in the context of how they might get used in Palworld.
We have two functions that make up the bread and butter of our Lua scripting: RegisterHook
and NotifyOnNewObject
. You'll get a better hands-on experience with them in the next tutorial, but for now let's just introduce you to them.
RegisterHook
This hooks on to SomeFunction and fires after SomeFunction is executed.
For example, one of the most common RegisterHooks
you'll see in scripts right now is:
RegisterHook("/Script/Engine.PlayerController:ClientRestart", function (Context)
-- do something
end)
What's the function being hooked?
Hopefully you were thinking ClientRestart, or we might have a long road ahead of us
Now, what is Context in this example?
If you said PlayerController, good job! I'm proud of you. The first parameter in the callback is always the UObject calling the function. Aka, the context.
What's the point of this hook, why do so many scripts use it?
Well, what does it do? It executes --do something
after the client restarts. It's a handy way to init stuff.
Why do most scripts use it? Well, not everything is available right away when the game launches, so sometimes you need to delay your logic until you know whatever you want will be accessible. A general rule of thumb is anything that starts with /Script/
should be available immediately. Anything else, you probably should put behind a hook like this.
But this hook also sucks because it doesn't work for dedicated servers. So I'mma teach you a better one:
RegisterHook("/Script/Engine.PlayerController:ServerAcknowledgePossession", function(Context)
-- do something
end)
This should get called whenever a client connects to the server. It also works for local games. I use it in most my scripts.
Sometimes it doesn't work and I don't know why but we don't talk about that and just claim ignorance and blame the person running the server for setting up something wrong, idk.
We said before the first parameter of the callback function is the UObject, but you can also get the params from the invoked function. The callback function of this is always: function(UObject self, UFunctionParams)
So if I have StupidFunction(bool isTrue, int Id, string Message)
I can do
RegisterHook("/Script/Example.SomeObject:StupidFunction", function(Context, isTrue, Id, Message)
print("This message is: " .. Message:get())
print("The bool is: " .. isTrue:get())
print("The id is: " .. id:get())
)