Community Forum

Switching vehicles

Forum Overview >> Scripting

CategoryScripting
Created08.01.2011 18:21


Edmund Eddie (Unknown) 08.01.2011 18:34
I'm working on a vehicle quickdial. I have it working, but there is a problem in MP.

I made a vehicle = g_currentMission.controlledVehicle.

I use g_currentMission:onEnterVehicle(vehicle) and it works. I can switch between the saved steerables. Everything works as expected.

I test the vehicle.isEntered, but I still end up with two players in the same vehicle. When one player exits the vehicle, the seat is empty, but the one that stays can move the wheels, turn on the light an honk. But he can't drive it.

I guess vehicle.isEntered is wrong. Is there something else I could test for if a player is driving a vehicle?

Edmund Eddie (Unknown) 08.01.2011 21:08
I have got the tip to try vehicle.isControlled. That works, somehow.

Example 1:
Vehicle1 taken by Player1 (via TAB or entered by E button).
Player2 has Vehicle1 bookmarked. Tries jumping to Vehicle1. Player2 gets isControlled=true.

Example 2:
Player1 and Player2 have Vehicle1 bookmarked.
Player1 jumps to Vehicle1 via bookmark.
Player2 attempts jumping to Vehicle1. Success. Because Vehicle1.isControlled=false, you get 2 players in Vehicle1.

Example 2:
Player1 and Player2 have Vehicle1 bookmarked.
Player1 jumps to Vehicle1 via bookmark.
Player1 exits and enters via E button.
Player2 attempts jumping to Vehicle1. Fail. Because Vehicle1.isControlled=true.

:(

Stefan Geiger - GIANTS Software 10.01.2011 17:02
Correct, isControlled is the variable you are looking for. This is set to true if any player is controling the vehicle.

isEntered is only true on the player that really is controlling the game.

So if isControlled is true, someone is controlling it (maybe also me).
If isEntered is true, I am the one controlling it.

You should not call onEnterVehicle directly.
Use the VehicleEnterRequestEvent. This ensures that not two players can enter the same players at the same time. And it also notifies all clients, that a new player is sitting inside such that they can show the player inside and show the correct nickname on top.

If you want the player that is running your code to enter a vehicle, you can use this code:
g_client:getServerConnection():sendEvent(VehicleEnterRequestEvent:new(vehicle, g_settingsNickname));

Edmund Eddie (Unknown) 11.01.2011 13:04
Thank you very much.

Two more question. Do the vehicles have any id property?
Something like g_currentMission.steerables[1].uniqueid, by which I could uniquely identify the vehicle inside a game session.
This way my quickdial script wouldn't get shifted when user sells a vehicle.

And how can i test if a game is multiplayer or singleplayer?

Thanks again.

Stefan Geiger - GIANTS Software 11.01.2011 17:19
There is a unique id for each network object (Vehicles, Bales, and other classes which inherit from Object). You can get the id is by calling
local id = networkGetObjectId(object)

and you can get the object from an id with
local object = networkGetObject(id)

An id is an 32bit integer. Thus you should use streamWriteInt32/streamReadInt32 to write the id to a network stream.

Edmund Eddie (Unknown) 14.01.2011 10:56
Sorry, already found what I was looking for... :)


Note: Log in to post. Create a new account here.