Jump to content
Sign in to follow this  

Passing an objects variable to a script (ExecVM help)

Recommended Posts

What i'm trying to do is determine the speed of a vehicle and then implement whatever code dependent on the value returned. I tried lots of different stuff but eventually just dropped all my code in order to just output hints, as i couldn't even get them working. I finally figured out which part was breaking my brain... and i think it's how i'm retrieving information....


So in the editor i place down a civilian car, give it no variable name, and call a script using EXecVm.

_this = execVM "riggedcar.sqf"

In RiggedCar.sqf i wanted to first find the car object so i used

_car = nearestObject "Vehicle";

if (speed _car >= 50) then {hint "You're going too fast!"};

So in order to get the speed of the car, do i need to pass the speed from the execVM to the script, is "speed _car" recognised within the script or should i have defined the variable within the script itself... something like _carspeed = speed; I tried Velocity too, at least i got that to output 0,0,0 but it wouldn't change when the car moved.


Also, as this is just an If statement and i want it to run over time should i be using a while loop, or an eventHandler in order to test the cars speed every second or so?


Any help would be appreciated, even if it' to point me in the right direction, examples of passing information/variables or calling properties of a object etc.


Share this post

Link to post
Share on other sites

Uh uh, my first suggestion is to go here:




And see what it states about every command (execVM, nearestObject) and how to use it, because you are making a wrong use on them.


Passing the car object to the script. On car init field put: [this] execVM "ridgeCar.sqf"


So the car object will be passed to the script. Now lets go to the script:


_car = _this select 0;//this assigns to the local variable _car the arguments you passed to the script.

while {alive _car} do // we use this to loop, but we don't want the loop forever, for example, finish it when the car is destroyed
	_speed = speed _car;// we check car's speed and assign it to the _speed local variable
	hint format ["Car speed: %1",_speed];//a hint will show current car speed and its very useful for debugging purposes, as it will tell me weird things if I made something wrong
	if (_speed >= 50) then {hint "Fast fast fast"};// this if statement checks if speed is more than 50 and overrides the previous hint
	sleep 5; //we only want to make this check every five seconds
//from here the car has been destroyed and the loop has exit. We can do other things with the car, for example delete it:
deleteVehicle _car;


Share this post

Link to post
Share on other sites

thanks for such a thorough explanation. I needed it.


Share this post

Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in

Sign In Now
Sign in to follow this