how to add method to datatype, really how

Started by vito, Jun 19, 2016, 06:26 PM

Previous topic - Next topic

vito



How SLC did it?

In javascript we can:
<type>.prototype.mycoolmethod = function (){ }Can we do same in squirrel for "player" type?

EK.IceFlake

/Like this
function CPlayer::Msg(whatever)
{
//...
}

Xmair


Credits to Boystang!

VU Full Member | VCDC 6 Coordinator & Scripter | EG A/D Contributor | Developer of VCCNR | Developer of KTB | Ex-Scripter of EAD

KAKAN

function CPlayer::Msg( text )
{
   ::MessagePlayer( this, text );
}
CPlayer is the class, here, in squirrel, we use :: to add a method to a class.
oh no

vito

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;
}

vito

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?

.

I don't think so. At least, not from what I recall. Assuming you want to do this from script.
.

vito

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.

.

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

KAKAN

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
oh no