Vice City: Multiplayer

Server Development => Scripting and Server Management => Topic started by: vito on Jun 19, 2016, 06:26 PM

Title: how to add method to datatype, really how
Post by: vito on Jun 19, 2016, 06:26 PM
(https://s32.postimg.org/ysngdt179/Untitled.png)

How SLC did it?

In javascript we can:
<type>.prototype.mycoolmethod = function (){ }Can we do same in squirrel for "player" type?
Title: Re: how to add method to datatype, really how
Post by: EK.IceFlake on Jun 19, 2016, 08:19 PM
/Like this
function CPlayer::Msg(whatever)
{
//...
}
Title: Re: how to add method to datatype, really how
Post by: Xmair on Jun 20, 2016, 06:02 AM
Compile @SLC's plugin.
Title: Re: how to add method to datatype, really how
Post by: KAKAN on Jun 20, 2016, 08:57 AM
function CPlayer::Msg( text )
{
   ::MessagePlayer( this, text );
}
CPlayer is the class, here, in squirrel, we use :: to add a method to a class.
Title: Re: how to add method to datatype, really how
Post by: vito on Jun 20, 2016, 09:14 AM
Thanks guys. All works fine.
function CPlayer::sethp( val )
{
this.Health = val;
::MessagePlayer("Your HP now is "+ val, this );
MessagePlayer();
}

function CPlayer::MessagePlayer()
{
::MessagePlayer("I'm in this class too", this );
}

function onPlayerCommand( player, cmd, text )
{
if(cmd == "sethp"){
player.sethp( text.tointeger() );
}
}

function onPlayerEnterVehicle( player, vehicle, door ){
vehicle.Up();
}

function CVehicle::Up()
{
this.Pos.z = this.Pos.z + 5;
}
Title: Re: how to add method to datatype, really how
Post by: vito on Jul 28, 2016, 06:23 PM
I will re-open this topic and bump it to make one more question.
Is it possible to add methods to string/array/table/integer objects?
Title: Re: how to add method to datatype, really how
Post by: . on Jul 28, 2016, 06:34 PM
I don't think so. At least, not from what I recall. Assuming you want to do this from script.
Title: Re: how to add method to datatype, really how
Post by: vito on Jul 28, 2016, 06:49 PM
Quote from: . on Jul 28, 2016, 06:34 PMI don't think so. At least, not from what I recall. Assuming you want to do this from script.
Sure I want to do it under native plugin only.
Title: Re: how to add method to datatype, really how
Post by: . on Jul 28, 2016, 06:59 PM
The only way I can think of is to modify the Squirrel source. I don't think the primitive types can be altered by any third-party code/plugin/module/library w/e.
Title: Re: how to add method to datatype, really how
Post by: KAKAN on Jul 29, 2016, 08:32 AM
Quote from: vito on Jul 28, 2016, 06:23 PMI will re-open this topic and bump it to make one more question.
Is it possible to add methods to string/array/table/integer objects?
I always wanted that, but, you can't do it without editing the source :(
But, you can make another class for string/table/... manipulation though :p