Jump to content

Getting vehicle menu from Freeroam gamemode

Guest Gamer99

Recommended Posts

Guide on doing this on your own:

Way 1: The friendliest way:

Try to remake it in this way: GUI tutorial, GUI classes resource and GUI scripting functions.

Way 2: The lessfriendlybutstillquitefriendly way:

Look up in the resource what you need and type it over, for better understanding.

Way 3: The notsofriendly way:

Copy it.


And to remember: We're not here to chew food for you to swallow. You need to start chewing it, and we might help chewing. (sounds pretty grouse though... :D)

Link to comment

Yep, I can't script to save my life.

I'm not sure why that first link is useful but the second would be.... if I could actually learn and pick up stuff although I'm not really willing to - I just want to have some fun.

So far I've come up with this...

addEventHandler('onClientResourceStart', g_ResRoot, 
        bindKey('f1', 'down', toggleFRWindow) 
function toggleFRWindow() 
    if isWindowOpen(wndCreateVehicle) then 
wndCreateVehicle = { 
    text = 'Create vehicle', 
    width = 300, 
    controls = { 
                {text='Vehicle', attr='name'} 
            rows={xml='vehicles.xml', attrs={'id', 'name'}}, 
        {'btn', id='create', onclick=createSelectedVehicle}, 
        {'btn', id='close', closeswindow=true} 
function createSelectedVehicle(leaf) 
    if not leaf then 
        leaf = getSelectedGridListLeaf(wndCreateVehicle, 'vehicles') 
        if not leaf then 
function createVehicleCommand(cmd, ...) 
    local vehID 
    local vehiclesToCreate = {} 
    local args = { ... } 
    for i,vehID in ipairs(args) do 
        vehID = tonumber(args[i]) 
        if not vehID then 
            vehID = getVehicleIDFromName(args[i]) 
        if vehID then 
            table.insert(vehiclesToCreate, vehID) 
addCommandHandler('createvehicle', createVehicleCommand) 
addCommandHandler('cv', createVehicleCommand) 

Who the hell knows if it's right.

Edit: All I want is a menu that opens and closes by pressing F1 to let people spawn a vehicle from a list.

Link to comment

This is a very simple script (not exactly the same as the one in freeroam) made with GUI Classes. To make it work, just download those classes and replace what's inside "test.lua" with this:

addEventHandler( "onClientResourceStart", getResourceRootElement( getThisResource() ), 
    function( ) 
        local screenSize = { guiGetScreenSize( ) } 
        local numberOfVehicles = 212; -- there is 212 vehicles in GTA (inc. trailers) 
        local gridList = GridList:Create( screenSize[ 1 ] - 210, screenSize[ 2 ] / 2 - 200, 200, 400 ); 
        gridList:Visible( false ); 
        gridList:AddColumn( "ID", .22 ); 
        gridList:AddColumn( "Name", .6 ); 
        for i = 0, numberOfVehicles do 
            local tempID = i + 400; -- first vehicle ID is 400 (Landstalker) 
            local vehName = getVehicleNameFromID( tempID ); 
            gridList:AddRow( tostring( tempID ), vehName ); 
        gridList:AddOnClick( -- when you click the gridlist a car will be created 
            function(  ) 
                local x, y, z = getElementPosition( getLocalPlayer( ) ); 
                local rowID = gridList:SelectedItem(); 
                createVehicle( tonumber( gridList:ItemText( rowID, 1 ) ), x + 5, y, z ); 
        bindKey( "F1", "down", 
            function( )  
                showCursor( not isCursorShowing( ) ); 
                gridList:Visible( not gridList:Visible() ); 

NOTE: THIS CREATES VEHICLES CLIENT-SIDE. YOU CANNOT ENTER VEHICLES CREATED CLIENT-SIDE! Maybe someone here will make another piece of code for you that creates the cars server-side... It's just a few more lines of code.

Link to comment

Wow - thank you!

That is very good and close to what I was looking for.

Is it possible to have the cursor on when the menu is available? And server side use would be muchly appreciated if someone could chip in.

Link to comment

OK, as I said it's not long code so I can do it... I always say "I was born to help other people", but some people are so lazy that I don't even want to help them...

Here, make a new file and paste this code into it:

addEvent( "test_ClientCreateVehicle", true ) 
addEventHandler( "test_ClientCreateVehicle", getResourceRootElement( getThisResource() ), 
    function( vehID ) 
        --"client" is a hidden player that triggered server side event 
        local x, y, z = getElementPosition( client ); -- here's the client 
        createVehicle( vehID, x + 5, y, z ); 

Add new line to meta.xml:

<script src="THE_FILE_NAME_YOU_JUST_CREATED.lua" />  

And change line where you created car client-side with this:

triggerServerEvent( "test_ClientCreateVehicle", getRootElement(), tonumber( gridList:ItemText( rowID, 1 ) ) ); 

That's all you need. Basically, when you used addEvent() in server-side script you will have a new event that client can trigger and send some data... What you wanted to do was create a car server-side, right? Right, so, you just send an id of the vehicle you want to create (which is taken from the Grid List) and create it with createVehicle().

If you want to know more (it will be useful in the future if you want to extend server's gameplay) about events go to: http://development.mtasa.com/index.php? ... ent_system

Link to comment

Thanks alot for all the time and effort you put into this, it works brilliantly (using admin panel to bypass the following) EXCEPT a cursor doesn't appear when the menu is open, and only appears when it is closed - meaning you can't play the game or select a vehicle!

EDIT: Fixed.

Replaced gridlist statement with

gridList:Visible ( isCursorShowing() )

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...