[Suggestion] Allow vehicles to be set with player.Vehicle

Started by EK.IceFlake, May 06, 2017, 04:00 AM

Previous topic - Next topic

EK.IceFlake

Well...
if (player.Vehicle == null)works for determining if a player is on foot

But
player.Vehicle = null;causes this error:
[ERR] the index 'Vehicle' does not exist
I suggest you make player.Vehicle = ??? to be an alias of player.Embark(???);

kennedyarz

else if ( cmd == "veh" )
{
    if ( player.Vehicle )
    {
        MessagePlayer( "You are in vehicle " + GetVehicleNameFromModel( player.Vehicle.Model ) + " .", player );
    }
else MessagePlayer("You no are in a vehicle",player)
}

yes is rare

EK.IceFlake

Quote from: kennedyarz on May 06, 2017, 04:49 AMelse if ( cmd == "veh" )
{
    if ( player.Vehicle )
    {
        MessagePlayer( "You are in vehicle " + GetVehicleNameFromModel( player.Vehicle.Model ) + " .", player );
    }
else MessagePlayer("You no are in a vehicle",player)
}

yes is rare
That code
  • is not an answer to the question
  • is on a completely different plugin

kennedyarz

At some specific time the plugin? If you do not like the answers do not ask

EK.IceFlake


kennedyarz

The plugin you're talking about is not specified. And you complain when you receive an answer. So why ask for an answer? We are not soothsayers

.

There are some things in the plugin called Null Instances. But before why I tell you the direct answer let me tell you why this design was chosen.

Let's assume that you have a function like:
function FindVehicle(info_about_it)
{
    if (!info_about_it)
    {
        return null;
    }
    return the_vehicle_that_matched;
}

So now let's assume that you use that function and the value returned by it:
local obj = FindVehicle(false); // Obviously it returned null here
print(obj.Model);

Well now you're trying to access the member .Model on a null value. So you'll get something like The index 'Model' does not exist. And while in this particular code it's easy to track down and debug the issue. Imagine a more complex example and imagine the code coming from someone else. You have to admit that It'll be a little confusing at first.

So now let's change the FindVehicle() function to:
function FindVehicle(info_about_it)
{
    if (!info_about_it)
    {
        return SqVehicle.NullInst();
    }
    return the_vehicle_that_matched;
}

Well now, when you execute that same code above. You should get a more specific answer "Invalid vehicle reference []" and in the brackets there would even be a name if one was assigned to it. I should've set that tag to "null" by default but I'll do it in the next release. So now the error is more descriptive.

So applying that to your code it should become:
player.Vehicle = SqVehicle.NullInst();

But unfortunately it doesn't work like that because it may be confusing when someone reads the code. Well, it was before I introduced the null instances.

So the actual code is:
player.Disembark();

Found at: https://github.com/iSLC/VCMP-SqMod/blob/master/source/Entity/Player.cpp#L2463

The reason why I've avoided things like "abc.something = null;" is because it may look confusing in code and probably not too descriptive of the intention. And even the generated errors (if any) might be a little ambiguous at first.



The only exception to that NullInst() approach is player.Spec = null; which I had to leave it like that temporarily because it crashed (according to some people who didn't provided the example to replicate the bug) and I didn't had the time to debug it.
.

EK.IceFlake

Quote from: kennedyarz on May 06, 2017, 12:33 PMThe plugin you're talking about is not specified. And you complain when you receive an answer. So why ask for an answer? We are not soothsayers
Mind looking in the section this topic is posted?
It very clearly says "SLC's Squirrel Plugin".