Liberty Unleashed

Scripting => Scripting Discussion => Topic started by: Vortrex on April 05, 2012, 09:30:04 pm

Title: Player/Vehicle Classes
Post by: Vortrex on April 05, 2012, 09:30:04 pm
Hey, not sure how this works, but so far I think that its not possible.

I have a little knowledge of how classes work in Squirrel. I was wondering, because after reading over the section in the Squirrel 2 and 3 manual's, I am under the assumption that I won't be able to add more methods to a LU built class when running the server. I have a little example of how I was wanting to do it:

Lets say a player connects. His instance is created (for things like player.Pos). I was wondering, can I add more stuff to this instance? Like, if I wanted to preserve their password, use things like player.Password, and be able to read/write the string to it?

Also, I was wondering if it would be possible to add extra functions to it, like for a player instance to have like, player.SaveToDatabase(); or something.

Any support on this would be appreciated, thanks!
[GLT]WtF
Title: Re: Player/Vehicle Classes
Post by: SugarD on April 06, 2012, 02:00:55 am
For the latter, I believe you could use one of the database plugins and save their information there to be referenced, but I'm not really a scripter, so don't rely on my statement too much for that. D:
Title: Re: Player/Vehicle Classes
Post by: Vortrex on April 06, 2012, 02:13:05 am
I use a database to store everything, however for database overload prevention purposes, I am using a temporary table solution at runtime to keep data, and saving it every so often or on certain events. I was just wondering if it were possible to use the player instances for this.
Title: Re: Player/Vehicle Classes
Post by: Thijn on April 06, 2012, 01:54:11 pm
Don't think you can. But not sure.
Title: Re: Player/Vehicle Classes
Post by: stormeus on April 06, 2012, 03:03:14 pm
Unlike a normal Squirrel class, the Player and Vehicle classes are not traditionally extensible (class herp extends derp) due to the way they are implemented. However, upon loading the scripts, you CAN create new slot instances in either class, and they will be accessible through any instance.

Code: (Squirrel) [Select]
function onPlayerJoin( pPlayerInst )
{
Player.Herp <- function() // Inserts into base class
{
::print( "LOLOLO" );
}

pPlayerInst.Herp(); // Player instance changed dynamically, function works
}

EDIT: Should probably add that you should be extending the classes in onScriptLoad (or onServerStart). Doing Player.NewSlot <- in onPlayerJoin isn't healthy.

EDIT 2: This is more kosher.
Code: (Squirrel) [Select]
function hook_Herp()             { ::print( "LOLOLO" );      }
function onScriptLoad()          { Player.Herp <- hook_Herp; }
function onPlayerJoin( pPlayer ) { pPlayer.Herp();           }