Jump to content

Car Spawner GUI - Can't get GUI to show on marker hit.


UTurn

Recommended Posts

I've been working on a car spawning system, and everything in my script worked when I had it to where, if you walk into the spawn marker, it simply spawns 1 type of car with no GUI. Then, I created a GUI with the 'guieditor' resource, and fit it into my clientside script. From what I'm seeing, everything should work fine. Also, I don't get any errors or warnings whatsoever.

But no matter what I've tried I can't seem to get my GUI to show, what could be causing this?

Before I show you the main scripts, I figure for completeness' sake I'll show you the EDF that defines the spawner markers and the map code for the spawners, along with the code that generated the markers.

EDF:

<edf name="GTC"> 
    <element name="spawnpoint" friendlyname="Spawnpoint" icon="edf/spawnpoint.png"> 
        <data name="position" type="coord3d" default="0,0,0" /> 
        <data name="rotation" type="coord3d" default="0,0,0" /> 
        <ped model="292" rotation="!rotation!" /> 
        <blip position="!position!" icon="22" size="2" color="#ff0000ff" dimension="0" /> 
    </element> 
     
    <element name="vspawner" friendlyname="Car Spawner" icon="edf/vehicle.png"> 
        <data name="position" type="coord3d" default="0,0,0" /> 
        <data name="rotation" type="coord3d" default="0,0,0" /> 
        <data name="color" type="color" default="#ffffffff" /> 
        <data name="vspawnType" type="string" default="hospital" /> 
        <marker size="2" type="cylinder" color="!color!" /> 
    </element> 
</edf> 
  

The spawner line in the map file:

<vspawner id="vspawner (1)" color="#A3A3A3AD" vspawnType="hospital" posX="2041" posY="-1429.4" posZ="16.1" rotX="0" rotY="0" rotZ="270"></vspawner> 

Code that generates the markers from the map data:

local vspawners = getElementsByType("vspawner", MAP_ROOT) 
     
    -- Create the vehicle spawner blips. 
    for k,v in pairs(vspawners) do 
        local x = getElementData(v, "posX") 
        local y = getElementData(v, "posY") 
        local z = getElementData(v, "posZ") 
        local clr = getElementData(v, "color") 
        local r,g,b,a = hexToRgb(clr) 
         
        local spawnMark = createMarker(x,y,z, "cylinder", 2.0, r,g,b,a) 
        setElementData(spawnMark, "markType", "vspawner") 
        setElementData(spawnMark, "vspawnType", getElementData(v, "vspawnType")) 
        setElementData(spawnMark, "rotZ", getElementData(v, "rotZ")) 
    end 

Okay, now for the main script files.

vspawnsys_c.lua

  
VSPAWN_TYPES = { 
    ["hospital"] = { 
        534, -- Remington 
        581, -- BF-400 
        457  -- Caddy 
    } 
} 
  
GUIEditor = { 
    gridlist = {}, 
    window = {}, 
    button = {} 
} 
  
-- 
-- Creates the vehicle spawner interface. 
-- 
addEventHandler("onClientResourceStart", resourceRoot, 
    function() 
        GUIEditor.window[1] = guiCreateWindow(510, 466, 255, 250, "GTC Vehicles", false) 
        guiWindowSetSizable(GUIEditor.window[1], false) 
  
        GUIEditor.gridlist[1] = guiCreateGridList(14, 31, 227, 173, false, GUIEditor.window[1]) 
        guiGridListAddColumn(GUIEditor.gridlist[1], "Key", 0.5) 
        guiGridListAddColumn(GUIEditor.gridlist[1], "Vehicle Name", 0.5) 
        GUIEditor.button[1] = guiCreateButton(63, 214, 128, 26, "Spawn Vehicle", false, GUIEditor.window[1]) 
        addEventHandler("onClientGUIClick", GUIEditor.button[1], onVehicleSelected, false) 
         
        guiSetVisible(GUIEditor.window[1], false) 
    end 
) 
  
-- 
-- Spawns the currently selected vehicle. 
-- 
function onVehicleSelected() 
    local vspawnMark = getElementData(localPlayer, "vspawnMark") 
     
    if vspawnMark then 
        local selection = guiGridListSetSelectedItem(GUIEditor.gridlist[1]) 
  
        if not selection then selection = 1 end 
        local validCars = VSPAWN_TYPES[getElementData(vspawnMark, "vspawnType")] 
         
        if validCars then 
            local x,y,z = getElementPosition(vspawnMark) 
            triggerServerEvent("vspawnCreateVehicle", root, validCars[selection], x,y,z, getElementData(vspawnMark, "rotZ")) 
        end 
    end 
     
    hideSpawnerWindow() 
end 
  
-- 
-- Pulls up the vspawn selection window, loading the vehicles from the given vspawn type. 
-- 
function showSpawnerWindow(spawnerType) 
    if (GUIEditor.window[1] ~= nil) then 
        guiSetVisible(GUIEditor.window[1], true) 
    else 
        outputChatBox("An unexpected error has occurred and the spawn menu has not been created.") 
    end  
     
    showCursor(true) 
    guiSetInputEnabled(true) 
     
    local validCars = VSPAWN_TYPES[spawnerType] 
     
    if validCars then 
        -- Add all valid cars for the spawner type to the grid list. 
        for i = 1, #validCars do 
            local row = guiGridListAddRow(GUIEditor.gridlist[1]) 
            guiGridListSetItemText(GUIEditor.gridlist[1], row, 1, tostring(i), false, true) -- Key 
            guiGridListSetItemText(GUIEditor.gridlist[1], row, 2, getVehicleName(validCars[i]), false, false) -- Vehicle Name 
        end 
    end 
end 
  
-- 
-- Hides the vspawn selection window and removes all grid list entries. 
-- 
function hideSpawnerWindow() 
    guiGridListClear(GUIEditor.gridlist[1]) 
    guiSetVisible(GUI_vehicleListWnd, false) 
    showCursor(false) 
    guiSetInputEnabled(false) 
end 
  
-- 
-- Bring up the v-spawner GUI when a player enters a spawn marker. 
-- 
addEventHandler("onClientMarkerHit", root, 
    function (hitPlayer, dimensionMatch) 
        -- If the player walks into a vehicle spawner 
        if dimensionMatch and (getElementData(source, "markType") == "vspawner") then 
            -- Make sure this is the first moment he's walked into the marker. 
            if (getElementData(hitPlayer, "vspawnEntered") ~= true) then 
                -- If isn't in a car, bring up the spawner menu. 
                if (getPedOccupiedVehicle(hitPlayer) == false) then 
                    setElementData(hitPlayer, "vspawnEntered", true) 
                    setElementData(hitPlayer, "vspawnMark", source) -- Save the current spawn marker for use in 'onVehicleSelect()' 
                    showSpawnerWindow(getElementData(source, "vspawnType")) 
                end 
            end 
        end 
    end 
) 
  
-- 
-- Reset the "vspawnEntered" player data when a vspawner marker has been left. 
-- 
addEventHandler("onClientMarkerLeave", root, 
    function (leftPlayer, dimensionMatch) 
        if dimensionMatch and (getElementData(source, "markType") == "vspawner") then 
            setElementData(leftPlayer, "vspawnEntered", false) 
            setElementData(hitPlayer, "vspawnMark", false) 
             
            if (guiGetVisible(GUIEditor.window[1]) == true) 
                hideSpawnerWindow() 
            end 
        end 
    end 
) 
  

And the serverside...

vspawnsys_s.lua

  
addEvent("vspawnCreateVehicle", true) 
  
-- 
-- Spawns a player into the given vehicle at his position. 
-- 
function vspawnCreateVehicle(vehID, x,y,z, rot) 
    if isElement(client) and vehID then 
        -- Check if the user already has a spawned vehicle, if so, unspawn it. 
        local vspawnSlot = getElementData(client, "vspawnSlot") 
         
        if (vspawnSlot ~= false) then 
            destroyElement(vspawnSlot) 
        end 
         
        -- Create the vehicle and spawn the player inside of it. 
        local veh = createVehicle(412, x,y,z + 1.5, 0,0,360 - rot) 
        setVehicleDamageProof(veh, false) 
        setVehicleFuelTankExplodable(veh, false) 
        warpPedIntoVehicle(client, veh) 
         
        -- Add the car to the players vspawn slot (players can have only 1 vspawn car). 
        setElementData(client, "vspawnSlot", veh) 
    end 
end 
  
addEventHandler("vspawnCreateVehicle", root, vspawnCreateVehicle) 
  

I'm sure it's some simple mistake I'm overlooking, but after almost an hour just trying to figure out how to get a window to show, I want to figure this out so I can finally move on.

Link to comment

1. You don't need to check it it's ~= nil, skip it

  
function showSpawnerWindow(spawnerType) 
        guiSetVisible(GUIEditor.window[1], true) 
... 
  

2. Try to remove if dimensionMatch, just check elementData

  
if (getElementData(source, "markType") == "vspawner") then 
  

That's as far as I can help, the problem must be with onClientMarkerHit, as you say there are no errors and the marker is created.

Link to comment

Okay, I discovered the 'debugscript' command and found out the problems. Which there were alot of. Small ones. So I can't really explain what I did wrong but I'm posting the corrected code here for anybody who might benefit from it.

[EDIT] Mr.gSub I started to think that at first about the marker collision but I had already tested it before creating the GUI and it worked fine. As for the other changes, my script works now, but doesn't hurt to add those changes. Accounted for in the below code. And thanks for replying.

vspawnsys_c.lua [Client]

--[[ 
    vspawnsys_c.lua 
        - Manages the GUI for the vehicle spawning system. 
--]] 
  
-- Defines what vehicles can be spawned by different vspawn types. 
VSPAWN_TYPES = { 
    ["hospital"] = { 
        534, -- Remington 
        581, -- BF-400 
        457  -- Caddy 
    } 
} 
  
GUIEditor = { 
    gridlist = {}, 
    window = {}, 
    button = {} 
} 
  
-- 
-- Creates the vehicle spawn menu and hides it for later. 
-- 
addEventHandler("onClientResourceStart", resourceRoot, 
    function() 
        GUIEditor.window[1] = guiCreateWindow(510, 466, 255, 250, "GTC Vehicles", false) 
        guiWindowSetSizable(GUIEditor.window[1], false) 
  
        GUIEditor.gridlist[1] = guiCreateGridList(14, 31, 227, 173, false, GUIEditor.window[1]) 
        guiGridListAddColumn(GUIEditor.gridlist[1], "Key", 0.15) 
        guiGridListAddColumn(GUIEditor.gridlist[1], "Vehicle Name", 0.85) 
        GUIEditor.button[1] = guiCreateButton(63, 214, 128, 26, "Spawn Vehicle", false, GUIEditor.window[1]) 
        addEventHandler("onClientGUIClick", GUIEditor.button[1], onVehicleSelected, false) 
         
        guiSetVisible(GUIEditor.window[1], false) 
    end 
) 
  
-- 
-- Spawns the currently selected vehicle. 
-- 
function onVehicleSelected() 
    local vspawnMark = getElementData(localPlayer, "vspawnMark") 
     
    if vspawnMark then 
        local selection = guiGridListGetSelectedItem(GUIEditor.gridlist[1]) 
  
        if (selection == -1) then selection = 1 end 
        local validCars = VSPAWN_TYPES[getElementData(vspawnMark, "vspawnType")] 
         
        if validCars then 
            local x,y,z = getElementPosition(vspawnMark) 
            triggerServerEvent("vspawnCreateVehicle", root, validCars[selection], x,y,z, getElementData(vspawnMark, "rotZ")) 
        end 
    end 
     
    hideSpawnerWindow() 
end 
  
-- 
-- Pulls up the vspawn selection window, loading the vehicles from the given vspawn type. 
-- 
function showSpawnerWindow(spawnerType) 
    guiSetVisible(GUIEditor.window[1], true) 
     
    local validCars = VSPAWN_TYPES["hospital"] 
     
    if validCars then 
        -- Add all valid cars for the spawner type to the grid list. 
        for i = 1, #validCars do 
            local row = guiGridListAddRow(GUIEditor.gridlist[1]) 
            guiGridListSetItemText(GUIEditor.gridlist[1], row, 1, tostring(i), false, true) -- Key 
            guiGridListSetItemText(GUIEditor.gridlist[1], row, 2, getVehicleNameFromModel(validCars[i]), false, false) -- Vehicle Name 
        end 
    end 
end 
  
-- 
-- Hides the vspawn selection window and removes all grid list entries. 
-- 
function hideSpawnerWindow() 
    guiGridListClear(GUIEditor.gridlist[1]) 
    guiSetVisible(GUIEditor.window[1], false) 
    showCursor(false) 
    guiSetInputEnabled(false) 
end 
  
-- 
-- Bring up the v-spawner GUI when a player enters a spawn marker. 
-- 
addEventHandler("onClientMarkerHit", root, 
    function (hitPlayer, dimensionMatch) 
        -- If the player walks into a vehicle spawner 
        if (getElementData(source, "markType") == "vspawner") then 
            -- Make sure this is the first moment he's walked into the marker. 
            if (getElementData(hitPlayer, "vspawnEntered") ~= true) then 
                -- If isn't in a car, bring up the spawner menu. 
                if (getPedOccupiedVehicle(hitPlayer) == false) then 
                    setElementData(hitPlayer, "vspawnEntered", true) 
                    setElementData(hitPlayer, "vspawnMark", source) -- Save the current spawn marker for use in 'onVehicleSelect()' 
                    showSpawnerWindow(getElementData(source, "vspawnType")) 
                end 
            end 
        end 
    end 
) 
  
-- 
-- Reset the "vspawnEntered" player data when a vspawner marker has been left. 
-- 
addEventHandler("onClientMarkerLeave", root, 
    function (leftPlayer, dimensionMatch) 
        if dimensionMatch and (getElementData(source, "markType") == "vspawner") then 
            setElementData(leftPlayer, "vspawnEntered", false) 
            setElementData(leftPlayer, "vspawnMark", false) 
            hideSpawnerWindow() 
        end 
    end 
) 
  

vspawnsys_s.lua [server]

--[[ 
    vspawnsys_s.lua 
        - Manages the serverside vehicle spawning logic. 
--]] 
  
addEvent("vspawnCreateVehicle", true) 
  
-- 
-- Spawns a player into the given vehicle at his position. 
-- 
function vspawnCreateVehicle(vehID, x,y,z, rot) 
    if isElement(client) and vehID then 
        -- Check if the user already has a spawned vehicle, if so, unspawn it. 
        local vspawnSlot = getElementData(client, "vspawnSlot") 
         
        if (vspawnSlot ~= false) then 
            destroyElement(vspawnSlot) 
        end 
         
        -- Create the vehicle and spawn the player inside of it. 
        local veh = createVehicle(vehID, x,y,z + 1.5, 0,0,360 - rot) 
        setVehicleDamageProof(veh, false) 
        setVehicleFuelTankExplodable(veh, false) 
        warpPedIntoVehicle(client, veh) 
         
        -- Add the car to the players vspawn slot (players can have only 1 vspawn car). 
        setElementData(client, "vspawnSlot", veh) 
    end 
end 
  
addEventHandler("vspawnCreateVehicle", root, vspawnCreateVehicle) 
  

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...