Author Topic: Making function onDriverEnter as a mirror to onDriverLeave  (Read 2470 times)

So, I wrote this thing and called it Support_DriverLights.cs

It parents armor::onMount and armor::onUnmount and checks to see if the thing being mounted or unmounted is a vehicle with the variable "driverlightscompatible" (and slot = 0, meaning it's the driver who mounted or unmounted).

The point of this is to hide and unhide a group of nodes so that the headlights switch "on" when there's a driver.

However, I just discovered that I can simply write function CrownVictoriaVehicle::onDriverLeave(%this, %veh) and then do everything I need to do with %veh (the unhiding and hiding of nodes to turn off the headlights when the driver leaves the driving position of this specific vehicle)

this makes DriverLights halfway useless; now I hope to discover some complement to onDriverLeave, like... onDriverArrive, but I found no such thing. I want to do some stuff whenever my vehicle acquires a driver, without having to parent onMount.
« Last Edit: March 30, 2016, 11:52:52 PM by Teneksi »

What do you mean by "onDriverArrive"?

You can find all of the functions on a datablock by doing MyDatablock.dump();
You can also find all functions on an object by looking at it and doing /getID and then doing ID.dump();

onDriverLeave is called by the default onUnMount for whatever reason:

function Armor::onUnMount(%this, %obj, %vehicle, %node)
{
   %obj.lastMountTime = getSimTime();

   if(%node == 0 && isObject(%vehicle))
      %vehicle.onDriverLeave(%obj);

   %obj.setLookLimits("1", "0");
   %obj.playThread("0", "root");
}


There's no onDriverEnter to complement it, so you'll have to package onMount.

you have both been very  help

here's the current situation

Code: [Select]
package onDriverEnter
{
function armor::onMount(%this, %obj, %vehicle, %node)
{
Parent::onMount(%this, %obj, %vehicle, %node);

if(%node == 0 && isObject(%vehicle))
{
announce("trying to do onDriverEnter...");
%vehicle.onDriverEnter(%obj);
}
}
};
activatePackage(onDriverEnter);

meanwhile, inside package crownvictoriaspecialstuff...

Code: [Select]
function CrownVictoriaVehicle::onDriverEnter(%this, %veh)
{
announce("Driver entered Crown Vic");

%veh.hidenode(brake0);
%veh.hideNode(headlight0);

%veh.unhideNode(brake1);
%veh.unhideNode(headlight1);
}

function CrownVictoriaVehicle::onDriverLeave(%this, %veh)
{
announce("Driver left Crown Vic");

%veh.unhidenode(brake0);
%veh.unhideNode(headlight0);

%veh.hideNode(brake1);
%veh.hideNode(headlight1);

%veh.hidenode(brakeON);
%veh.hidenode(brakeONflare);
}


onDriverLeave works just fine, and "trying to do onDriverEnter..." appears when it's supposed to.

However, I simply get the message "Unknown command onDriverEnter."

I'm not sure how to write it in such a way that it is... known.





9:33 PM - Buddy: wait
9:33 PM - Buddy: nevermind haha
9:33 PM - Buddy: i see whats wrong
9:34 PM - Buddy: %vehicle.getDatablock().onDriverEnter(%obj);
9:34 PM - Buddy: most likely
9:34 PM - Teneksi: huh... ok. but what about what Zeblote posted?
9:34 PM - Buddy: or does onMount %vehicle give you the datablock
9:34 PM - Teneksi: I'm pretty sure it gave me the instance of the car.
9:35 PM - Teneksi: but the part of onUnMount that calls for onDriverLeave does the same thing
9:36 PM - Teneksi: i'm going to give your suggestion a try since it makes sense
9:36 PM - Buddy: function VehicleData::onDriverLeave(%this, %obj)
function WheeledVehicleData::onDriverLeave(%this, %obj)
9:36 PM - Teneksi: oh my god
9:37 PM - Teneksi: it's a function that calls a slightly different version of itself?
9:37 PM - Buddy: there is 4 of those functions
9:37 PM - Buddy: 5
9:37 PM - Buddy: player::
9:37 PM - Buddy: playerData::
9:37 PM - Buddy: Vehicle::
9:37 PM - Buddy: VehicleData::
9:37 PM - Teneksi: http://puu.sh/o0icI/b62da4fa02.png

« Last Edit: March 30, 2016, 11:38:41 PM by Teneksi »

If the function doesn't exist yet, don't put it in a package and don't parent it.

If the function doesn't exist yet, don't put it in a package and don't parent it.
onMount exists; I was parenting onMount, and calling onDriverEnter from it.

The problem was I was trying to do a datablock function on the car's object, rather than the datablock of the car.


so the change necessary to get it to do what was desired is, in that package,

   %vehicle.getDatablock().onDriverEnter(%vehicle);

i do not fully understand how onDriverLeave works but that's ok
« Last Edit: March 30, 2016, 11:52:42 PM by Teneksi »

No my point is not that. If I understand what you're doing correctly, the onDriverLeave and onDriverEnter should not be packaged.

No my point is not that. If I understand what you're doing correctly, the onDriverLeave and onDriverEnter should not be packaged.
Oh, you must mean the two functions specific to the car.

I think I understand: I should package functions if and only if they're parenting something?

What's the worst case scenario for packaging a function that does not parent? (and, out of curiosity, for not packaging a function that does parent?)
« Last Edit: March 31, 2016, 10:52:39 AM by Teneksi »

No my point was that you should not package functions that are not declared yet.

can you... um... explain

"declared"

and the reason I might package something

try to word it as if you're talking to someone who ... doesn't understand what you're talking about

You package a function if

1) it already does something by default and
2) you want to extend or replace its functionality


In this example, CrownVictoriaVehicle::onDriverLeave doesn't exist by default so there's no need to package it.

onDriverLeave is a function on the datablock you can use and will get executed automatically, but it does not exist until you create it.
Blockland's engine calls that function if it exists, so if someone specifically created a function called DatablockName::onDriverLeave.
Thus packaging this function (and your own function onDriverArrive) is not needed.
I am not 100% sure what the risks are of doing this anyway.

Very well! I shall move these functions out of the package.

Is it true that when writing a function CrownVictoriaVehicle::onAdd, I do not need to parent onAdd, as "CrownVictoriaVehicle::onAdd" is simply a different function, called at the same time as onAdd, and would not overwrite onAdd in any way?

You need to package that one, so it calls the original WheeledVehicleData::onAdd.