Hello,
I am working on a gamemode and an issue I've run into has to do with usermessages. I have created a few usermessage hooks for the sole purpose of setting up player table information on the client. The problem I have is with the fact that it appears LocalPlayer() is not accessible from my UserMessage.
I am confused because I swear I've seen it done before, but for the life of me I can't seem to get it to work. I've gotten around the issue by simply creating client-side global variables, but it pains me to do this because it feels dirty =P. Here is an example of what I'm trying to do:
[B]Client[/B]
[lua]
function AddObjective( data )
local objective = data:ReadString();
/* what I would like to do */
LocalPlayer():GetTable().ObjectiveList = LocalPlayer():GetTable().ObjectiveList or {};
table.insert( LocalPlayer():GetTable().ObjectiveList, objective );
end
usermessage.Hook("AddObjective", AddObjective)
[/lua]
[B]Note[/B]
The usermessage.Hook code is on a client-side only file. And just to be safe, I've wrapped it in [I]if ( CLIENT ) then[/I] tags.
And then on the server I send the usermessage (I don't think I need to demonstrate that code here). I know the usermessage gets sent, and it is being sent to a specific player.
My question is this: should I be using some different syntax to access the localPlayer [I]or[/I] is there a better method to use for setting up player data tables?
Thanks!
Hey, I appreciate providing us with all this information, it makes it much easier for us to help you. In the future I think you should provide us with the error, though!
Try this:
[lua]
LocalObjectives = {};
usermessage.Hook("AddObjective", function( data )
table.insert(LocalObjectives, data:ReadString());
end)
[/lua]
That's all you need to do! Since each client will be distributed their own objectives, there is no need to store it on the player object, but rather a new table. All you need to do is loop through the table named 'LocalObjectives' and display them accordingly.
Also, next time try this:
[lua]
LocalPlayer().Variable = newvalue;
[/lua]
You can then effectively access that variable from any file or scope! However, only in the client lua state.
[QUOTE=JustSoFaded;35421495]Hey, I appreciate providing us with all this information, it makes it much easier for us to help you. In the future I think you should provide us with the error, though!
Try this:
[lua]
LocalObjectives = {};
usermessage.Hook("AddObjective", function( data )
table.insert(LocalObjectives, data:ReadString());
end)
[/lua]
That's all you need to do! Since each client will be distributed their own objectives, there is no need to store it on the player object, but rather a new table. All you need to do is loop through the table named 'LocalObjectives' and display them accordingly.
Also, next time try this:
[lua]
LocalPlayer().Variable = newvalue;
[/lua]
You can then effectively access that variable from any file or scope! However, only in the client lua state.[/QUOTE]
Thanks for the quick reply! I appreciate it. :D
The solution you've given is actually what I ended up doing, haha, but I am not a fan of global variables because they could easily provide scoping issues and seem like a lesser way of achiving my desired functionality. Here is a bit more info:
[B]More Info[/B]
I am not getting an error, per-se, rather when I call [B]LocalPlayer()[/B] within the client-side usermessage hook function, it returns a nil entity which makes it impossible to access any table information.
[lua]
function AddObjective( data )
print(LocalPlayer());
end
usermessage.Hook( "AddObjective", AddObjective );
[/lua]
[B]Output:[/B] [I][NULL Entity][/I]
At this point, I am really just interested in why it is not letting me access LocalPlayer(). As for actual functionality, I have my stuff working through the method you suggested.
Again, thank you for the response!
[QUOTE=SharpCoder;35421544]The solution you've given is actually what I ended up doing, haha, but I am not a fan of global variables because they could easily provide scoping issues and seem like a lesser way of achiving my desired functionality[/QUOTE]
Scoping issues? I'm sorry, that doesn't make sense. Globals are in some cases perfectly fine, however if you don't need to access it outside of that scope then there is no need to globalize it and is considered bad practice to make a global variable over a local (when not needed).
As far as your LocalPlayer() issue goes, that's very strange. I just have 2 questions, though.
1. Where is this file placed? Would you mind providing a file path for me?
2. Are you sure this isn't being ran too early? LocalPlayer() could potentially return a NULL entity if your calling it too early in the process. Remember that lua is being initialized and ran before you even get into the game, causing LocalPlayer() to have no valid use.
[QUOTE=JustSoFaded;35421667]Scoping issues? I'm sorry, that doesn't make sense. Globals are in some cases perfectly fine, however if you don't need to access it outside of that scope then there is no need to globalize it and is considered bad practice to make a global variable over a local (when not needed).
As far as your LocalPlayer() issue goes, that's very strange. I just have 2 questions, though.
1. Where is this file placed? Would you mind providing a file path for me?
2. Are you sure this isn't being ran too early? LocalPlayer() could potentially return a NULL entity if your calling it too early in the process. Remember that lua is being initialized and ran before you even get into the game, causing LocalPlayer() to have no valid use.[/QUOTE]
Bloody hell... Can't believe I skimmed over that small detail! It was, in fact, because I was calling the usermessage [I]slightly[/I] too early. Now it works perfectly =) Thanks so much!
As for the scoping comments:
You're right in that there is nothing wrong with global variables. I simply hate using them because typically, if you dont [I]have[/I] to use a global, then it is best not to. Since there is already a convenient place to store information inside the LocalPlayer() namespace I would much rather use that instead of creating something accessible by far more things than it needs to be. I think this also just comes down to personal preference =)
But I digress.
Thanks again for the ideas and for helping me through my problems! Works perfectly now.
Anytime! Also, Since it's on the client (assuming you are validating all data via server rather then client), you really don't need to worry about anything accessing the global table! Even if they do access and modify it, it will just be fake information. It won't actually change anything!
Sorry, you need to Log In to post a reply to this thread.