Jump to content

GUI question


DakiLLa

Recommended Posts

i have made a small gui window with buttons. How to make that key "F2" will be shutdowns the window ? in my version it only create window.

  
  
function initGui ( key, keyState) 
        if ( keyState == "down" ) then 
                MAIN_WINDOW = guiCreateWindow(833,452,183,284,"Teleport",false) 
                Liberty = guiCreateButton(0.0656,0.0739,0.8852,0.0845,"Liberty City",true,MAIN_WINDOW) 
                KickStart = guiCreateButton(0.0656,0.1866,0.8852,0.0845,"KickStart Stunt Track",true,MAIN_WINDOW) 
                Exit = guiCreateButton(0.6557,0.8838,0.2951,0.0845,"Exit",true,MAIN_WINDOW) 
                NormalWorld = guiCreateButton(0.0765,0.8873,0.4863,0.081,"Normal World",true,MAIN_WINDOW) 
                showCursor ( true ) 
        end 
end 
addEventHandler ( "onClientResourceStart", getResourceRootElement( getThisResource( ) ), initGui ) 
addEventHandler ( "onClientGUIClick", getResourceRootElement (getThisResource ()),  
        function () 
                if (source == Liberty) then 
                        setElementInterior ( getLocalPlayer (), 1, -750.80, 491.00, 1371.70  ) 
                elseif (source == KickStart) then 
                        setElementInterior ( getLocalPlayer (), 14, -1464.5360, 1557.6900, 1052.5310  ) 
                elseif (source == NormalWorld) then 
                        setElementInterior ( getLocalPlayer (), 0, 0, 0, 2.5 ) 
                elseif (source == Exit ) then 
                        guiSetVisible (MAIN_WINDOW, false) 
                        showCursor (false) 
                end 
        end 
) 
function bind ( source ) 
        bindKey ("F2", "down", initGui ) 
end 
addEventHandler ( "onClientResourceStart", getResourceRootElement( getThisResource( ) ), bind ) 
  

thank's

Edited by Guest
Link to comment

Make a new function which hides/shows the window using guiSetVisible, bind F2 to this function (in bindKey, change from initGui to that function)

function showHideWindow( key, keyState ) 
    if keyState == "down" then 
        -- change window's visibility: 
        guiSetVisible( MAIN_WINDOW, not guiGetVisible( MAIN_WINDOW ) ) 
    end 
end 
-- bindKey( "F2", "down", showHideWindow ) 

not will change the returned value to opposite, eg. if guiGetVisible returns true (window is visible) the not will make it false (window invisible) and vice versa.

Link to comment

next question.

why i cannot see my window when i join ?

  
addEventHandler ( "onClientPlayerJoin", getLocalPlayer (), 
    function () 
        NWindow = guiCreateWindow(328,201,432,351,"SERVER NEWS",false) 
        NButton = guiCreateButton(0.3472,0.8746,0.3009,0.0997,"OK",true,NWindow) 
        showCursor (true) 
    end 
) 
  
addEventHandler ( "onClientGUIClick", getResourceRootElement (getThisResource ()),  
    function () 
        if (source == NButton) then 
            guiSetVisible ( NWindow, false ) 
            showCursor (false) 
        end 
    end 
) 
  

thank's.

Edited by Guest
Link to comment

Want to know why? :D

onClientPlayerJoin is triggered for everyone but yourself. Or everyone but the person running the script.

This is what the wiki says:

It is triggered for all players other than the local player.

Thats why. :mrgreen:

I like that piece of information :P

Link to comment

and now next question :)

i need to make a marker and when anybody hits it, opens a Gui window with buttons for player. I cant understand how to use the triggerEvent can you tell and correct my mistakes in my script and then i will use this like the sample.

Client

  
addEvent ( "ColHit", true ) 
addEventHandler ( "ColHit", getRootElement (), 
    function TRUCK_GUI () 
        TruckWindow = guiCreateWindow(378,251,287,114,"Job: Truck Driver",false) 
        TruckButtonYes = guiCreateButton(0.2265,0.693,0.2334,0.2281,"Yes!",true,TruckWindow) 
        TruckButtonNo = guiCreateButton(0.5331,0.693,0.2369,0.2281,"No!",true,TruckWindow) 
        TruckLabel = guiCreateLabel(0.2056,0.2018,0.6655,0.2895,"Want to deliver some cargo ?",true,TruckWindow) 
        guiLabelSetVerticalAlign(TruckLabel,"top") 
        guiLabelSetHorizontalAlign(TruckLabel,"left",false) 
        showCursor ( true ) 
    end 
) 
 addEventHandler ( "onClientGUIClick", getResourceRootElement (getThisResource ()),  
    function () 
        if (source == TruckButtonYes) then 
            triggerEvent ( "SPECIAL_EVENT", getRootElement () ) 
        elseif (source == TruckButtonNo) then 
                guiSetVisible (TruckWindow, false) 
                showCursor (false) 
        end 
    end 
) 
  

Server

  
local TruckCol = createColTube ( -77.1681, -1136.4044, 1.0781, 2, 2 ) 
  
function HitColShape ( thePlayer, matchingDimension ) 
    if (getElementType(thePlayer) == "player") then 
        triggerEvent ( thePlayer, "ColHit", getRootElement () ) 
    end 
end 
addEventHandler ( "onColShapeHit", TruckCol, HitColShape ) 
  
addEvent ( "SPECIAL_EVENT", true ) 
function TruckMission ( ) 
    ..... 
  

thank's

Edited by Guest
Link to comment

ok, it works, but i have another problem.

(dont want to create a new topic and i ask here)

With last scripts on this page i want to create a truck driver mission. When i hits object - opens the Gui and when you press button "Yes", starts the mission.

I have used van - spawn from ctv but when i starts the script i see 2 warnings:

Warning: SServer.lua: Bad argument @ 'addEventHandler' - Line 12

Warning: SServer.lua: Bad argument @ 'addEventHandler' - Line 64

and when i hits the marker nothing occursed.

server-side

  
local TruckCol = createColTube ( -77.1681, -1136.4044, 1.0781, 2, 2 ) 
  
function HitColShape ( thePlayer, matchingDimension ) 
    if (getElementType(thePlayer) == "player") then 
        triggerClientEvent ( thePlayer, "ColHit", getRootElement () ) 
    end 
end 
addEventHandler ( "onColShapeHit", TruckCol, HitColShape ) 
  
addEvent ( "SPECIAL_EVENT" ) 
addEventHandler ( "SPECIAL_EVENT", getRootElement (), TruckMission ) 
function TruckMission ( target ) 
    local x = getElementData ( target, "posX" ) 
    local y = getElementData ( target, "posY" ) 
    local z = getElementData ( target, "posZ" ) 
    local rx = getElementData ( target, "rotX" ) 
    local ry = getElementData ( target, "rotY" ) 
    local rz = getElementData ( target, "rotZ" ) 
    local model = getElementData ( target, "model" ) --id 
    local platetext = getElementData ( target, "plate" ) --plate text 
    local colours = getElementData ( target, "colors" ) --colours of vehicle 
    local pj = getElementData ( target, "paintjob" ) --paintjob 
    local upgrades = getElementData ( target, "upgrades" ) --paintjob 
    ---seperate the colours string and get specific colour ids 
    local col1 = gettok ( colours, 1, 44 ) 
    local col2 = gettok ( colours, 2, 44 ) 
    local col3 = gettok ( colours, 3, 44 ) 
    local col4 = gettok ( colours, 4, 44 ) 
    --if any of them are "ran", then make them into a random id 
    if col1 == "ran" or col1 == false then  col1 = randInt(0,15) end 
    if col2 == "ran" or col1 == false then  col2 = randInt(0,15) end 
    if col3 == "ran" or col1 == false then  col3 = randInt(0,15) end 
    if col4 == "ran" or col1 == false then  col4 = randInt(0,15) end 
    --if any of the rotations are not specified, make them 0 
    if rx == false then rx = 0 end 
    if ry == false then ry = 0 end 
    if rz == false then rz = 0 end 
    --finally, create the vehicle and define it as van 
    if ( platetext ) then 
        van = createVehicle ( model, x, y, z, rx, ry, rz, platetext ) 
    else 
        van = createVehicle ( model, x, y, z, rx, ry, rz ) 
    end 
    --if a paintjob was specified, add it 
    if ( pj ) then 
        setVehiclePaintjob ( van, pj ) 
    end 
    --if there were any mods 
    if ( upgrades ) then 
        --split the upgrades into a table 
        allUpgrades = split ( upgrades, 44 ) 
        for k,v in ipairs(allUpgrades) do --loop through the table 
            addVehicleUpgrade ( van, v ) --and add each upgrade 
        end 
    end 
    vanMarker = createMarker ( x, y, z, "checkpoint", 1.5, 255, 255, 255, 255 ) 
    attachElementToElement ( vanMarker, van ) 
     
    createBlipAttachedTo ( van, 55 ) 
end 
  
  
addEventHandler ( "onVehicleExplode", getRootElement (), vehicleExplode ) 
    function vehicleExplode () 
        if source == van then 
            outputChatBox ( "You have destroyed the cargo! Go out!", source, 255, 0, 0 ) 
            endRound() 
        end 
    end 
  
function endRound () 
    destroyBlipsAttachedTo ( van ) 
    destroyElement ( van ) 
    destroyElement ( vanMarker ) 
end 
  

client script wtih gui in my previous post.

can you help ?

Link to comment

This one isn't that difficult, it's just a tricky thing to discover.

addEventHandler only works if it's BEHIND your created function. Why DOES addEventHandler("something", anything, function () blah end ) work? It CONTAINS the function instead of the one created earlier. All a big puzzle! :D

Logically, it works the same as variables: If you do:

if hello==1 then 
    ... 
end 
hello=1 

Instead of:

hello=1 
if hello==1 then 
    ... 
end 

It no work... ^^

Link to comment

i think

line 64 error a think

  
addEventHandler ( "onVehicleExplode", getRootElement (), vehicleExplode ) 
    function vehicleExplode () 
        if source == van then 
            outputChatBox ( "You have destroyed the cargo! Go out!", source, 255, 0, 0 ) 
            endRound() 
        end 
    end 
  

needs to be

  
    function vehicleExplode () 
        if source == van then 
            outputChatBox ( "You have destroyed the cargo! Go out!", source, 255, 0, 0 ) 
            endRound() 
        end 
    end 
addEventHandler ( "onVehicleExplode", getRootElement (), vehicleExplode ) 
  

and

line 12 error

  
addEvent ( "SPECIAL_EVENT" ) 
addEventHandler ( "SPECIAL_EVENT", getRootElement (), TruckMission ) 
function TruckMission ( target ) 
    local x = getElementData ( target, "posX" ) 
    local y = getElementData ( target, "posY" ) 
    local z = getElementData ( target, "posZ" ) 
    local rx = getElementData ( target, "rotX" ) 
    local ry = getElementData ( target, "rotY" ) 
    local rz = getElementData ( target, "rotZ" ) 
    local model = getElementData ( target, "model" ) --id 
    local platetext = getElementData ( target, "plate" ) --plate text 
    local colours = getElementData ( target, "colors" ) --colours of vehicle 
    local pj = getElementData ( target, "paintjob" ) --paintjob 
    local upgrades = getElementData ( target, "upgrades" ) --paintjob 
    ---seperate the colours string and get specific colour ids 
    local col1 = gettok ( colours, 1, 44 ) 
    local col2 = gettok ( colours, 2, 44 ) 
    local col3 = gettok ( colours, 3, 44 ) 
    local col4 = gettok ( colours, 4, 44 ) 
    --if any of them are "ran", then make them into a random id 
    if col1 == "ran" or col1 == false then  col1 = randInt(0,15) end 
    if col2 == "ran" or col1 == false then  col2 = randInt(0,15) end 
    if col3 == "ran" or col1 == false then  col3 = randInt(0,15) end 
    if col4 == "ran" or col1 == false then  col4 = randInt(0,15) end 
    --if any of the rotations are not specified, make them 0 
    if rx == false then rx = 0 end 
    if ry == false then ry = 0 end 
    if rz == false then rz = 0 end 
    --finally, create the vehicle and define it as van 
    if ( platetext ) then 
        van = createVehicle ( model, x, y, z, rx, ry, rz, platetext ) 
    else 
        van = createVehicle ( model, x, y, z, rx, ry, rz ) 
    end 
    --if a paintjob was specified, add it 
    if ( pj ) then 
        setVehiclePaintjob ( van, pj ) 
    end 
    --if there were any mods 
    if ( upgrades ) then 
        --split the upgrades into a table 
        allUpgrades = split ( upgrades, 44 ) 
        for k,v in ipairs(allUpgrades) do --loop through the table 
            addVehicleUpgrade ( van, v ) --and add each upgrade 
        end 
    end 
    vanMarker = createMarker ( x, y, z, "checkpoint", 1.5, 255, 255, 255, 255 ) 
    attachElementToElement ( vanMarker, van ) 
     
    createBlipAttachedTo ( van, 55 ) 
end 
  

 

needs to be

 

  
function TruckMission ( target ) 
    local x = getElementData ( target, "posX" ) 
    local y = getElementData ( target, "posY" ) 
    local z = getElementData ( target, "posZ" ) 
    local rx = getElementData ( target, "rotX" ) 
    local ry = getElementData ( target, "rotY" ) 
    local rz = getElementData ( target, "rotZ" ) 
    local model = getElementData ( target, "model" ) --id 
    local platetext = getElementData ( target, "plate" ) --plate text 
    local colours = getElementData ( target, "colors" ) --colours of vehicle 
    local pj = getElementData ( target, "paintjob" ) --paintjob 
    local upgrades = getElementData ( target, "upgrades" ) --paintjob 
    ---seperate the colours string and get specific colour ids 
    local col1 = gettok ( colours, 1, 44 ) 
    local col2 = gettok ( colours, 2, 44 ) 
    local col3 = gettok ( colours, 3, 44 ) 
    local col4 = gettok ( colours, 4, 44 ) 
    --if any of them are "ran", then make them into a random id 
    if col1 == "ran" or col1 == false then  col1 = randInt(0,15) end 
    if col2 == "ran" or col1 == false then  col2 = randInt(0,15) end 
    if col3 == "ran" or col1 == false then  col3 = randInt(0,15) end 
    if col4 == "ran" or col1 == false then  col4 = randInt(0,15) end 
    --if any of the rotations are not specified, make them 0 
    if rx == false then rx = 0 end 
    if ry == false then ry = 0 end 
    if rz == false then rz = 0 end 
    --finally, create the vehicle and define it as van 
    if ( platetext ) then 
        van = createVehicle ( model, x, y, z, rx, ry, rz, platetext ) 
    else 
        van = createVehicle ( model, x, y, z, rx, ry, rz ) 
    end 
    --if a paintjob was specified, add it 
    if ( pj ) then 
        setVehiclePaintjob ( van, pj ) 
    end 
    --if there were any mods 
    if ( upgrades ) then 
        --split the upgrades into a table 
        allUpgrades = split ( upgrades, 44 ) 
        for k,v in ipairs(allUpgrades) do --loop through the table 
            addVehicleUpgrade ( van, v ) --and add each upgrade 
        end 
    end 
    vanMarker = createMarker ( x, y, z, "checkpoint", 1.5, 255, 255, 255, 255 ) 
    attachElementToElement ( vanMarker, van ) 
     
    createBlipAttachedTo ( van, 55 ) 
end 
addEvent ( "SPECIAL_EVENT" ) 
addEventHandler ( "SPECIAL_EVENT", getRootElement (), TruckMission ) 
  

i think but am usually wrong

Link to comment

you think right, bart, it works :D

but another problem: when i hiting the marker nothing not occurses.

  
function HitMarker ( TruckMarker, matchingDimension ) 
    if ( getMarkerType ( TruckMarker ) == "cylinder" ) then 
        triggerClientEvent ( thePlayer, "ColHit", getRootElement () ) -- may be in this line mistake ?... 
    end 
end 
addEventHandler ( "onPlayerMarkerHit", TruckMarker, HitMarker )  
  

  
addEventHandler ( "ColHit", getResourceRootElement (getThisResource() ), -- ...or in this? 
    function TRUCK_GUI () 
        TruckWindow = guiCreateWindow(378,251,287,114,"Job: Truck Driver",false) 
        TruckButtonYes = guiCreateButton(0.2265,0.693,0.2334,0.2281,"Yes!",true,TruckWindow) 
        TruckButtonNo = guiCreateButton(0.5331,0.693,0.2369,0.2281,"No!",true,TruckWindow) 
        TruckLabel = guiCreateLabel(0.2056,0.2018,0.6655,0.2895,"Want to deliver some cargo ?",true,TruckWindow) 
        guiLabelSetVerticalAlign(TruckLabel,"top") 
        guiLabelSetHorizontalAlign(TruckLabel,"left",false) 
        showCursor ( true ) 
    end 
) 
addEvent ( "ColHit" ) 
  

  
addEventHandler ( "onClientGUIClick", getResourceRootElement (getThisResource ()), 
    function () 
        if (source == TruckButtonYes) then 
            triggerEvent ( "SPECIAL_EVENT", getRootElement () ) -- and this ? 
            guiSetVisible (TruckWindow, false) 
            showCursor (false) 
        elseif (source == TruckButtonNo) then 
            guiSetVisible (TruckWindow, false) 
            showCursor (false) 
        end 
    end 
) 
  

resource works correctly, wtihout warnings and errors but triggering events not works..but why...

Link to comment

i think this is wrong

  
addEventHandler ( "ColHit", getResourceRootElement (getThisResource() ), -- ...or in this? 
    function TRUCK_GUI () 
        TruckWindow = guiCreateWindow(378,251,287,114,"Job: Truck Driver",false) 
        TruckButtonYes = guiCreateButton(0.2265,0.693,0.2334,0.2281,"Yes!",true,TruckWindow) 
        TruckButtonNo = guiCreateButton(0.5331,0.693,0.2369,0.2281,"No!",true,TruckWindow) 
        TruckLabel = guiCreateLabel(0.2056,0.2018,0.6655,0.2895,"Want to deliver some cargo ?",true,TruckWindow) 
        guiLabelSetVerticalAlign(TruckLabel,"top") 
        guiLabelSetHorizontalAlign(TruckLabel,"left",false) 
        showCursor ( true ) 
    end 
) 
addEvent ( "ColHit" ) 
  

i think it needs to be

  
addEvent ( "ColHit" ) 
addEventHandler ( "ColHit", getResourceRootElement (getThisResource() ), -- ...or in this? 
    function TRUCK_GUI () 
        TruckWindow = guiCreateWindow(378,251,287,114,"Job: Truck Driver",false) 
        TruckButtonYes = guiCreateButton(0.2265,0.693,0.2334,0.2281,"Yes!",true,TruckWindow) 
        TruckButtonNo = guiCreateButton(0.5331,0.693,0.2369,0.2281,"No!",true,TruckWindow) 
        TruckLabel = guiCreateLabel(0.2056,0.2018,0.6655,0.2895,"Want to deliver some cargo ?",true,TruckWindow) 
        guiLabelSetVerticalAlign(TruckLabel,"top") 
        guiLabelSetHorizontalAlign(TruckLabel,"left",false) 
        showCursor ( true ) 
    end 
) 
  

but im not certain, does it create the gui just for one person ?

Link to comment

uh..lol. right..

it works and when i hit marker -

Warning: bad argument @ triggerClientEvent

what elements i need to put into trigger ?

  
function MarkerHit ( MyMarker, matchingDimension ) 
    if (getMarkerType(MyMarker) == "cylinder") then 
        triggerClientEvent ( "onHit", .... ) -- something there but i dont know what.. ? 
    end 
end 
addEventHandler ( "onPlayerMarkerHit", getRootElement (), MarkerHit ) 
  

Link to comment
uh..lol. right..

it works and when i hit marker -

Warning: bad argument @ triggerClientEvent

what elements i need to put into trigger ?

  
function MarkerHit ( MyMarker, matchingDimension ) 
    if (getMarkerType(MyMarker) == "cylinder") then 
        triggerClientEvent ( "onHit", .... ) -- something there but i dont know what.. ? 
    end 
end 
addEventHandler ( "onPlayerMarkerHit", getRootElement (), MarkerHit ) 
  

Ok, I'll explain:

triggerClientEvent ( "onHit", .... ) -- something there but i dont know what.. ? 

If we get the wiki page, we can see in the Syntax part:

bool triggerClientEvent ( [element triggerFor=getRootElement()], string name, element theElement, [arguments...] )

--element triggerFor: Do we need it? I think so, you don't want to trigger it for everyone I guess! xD

--string name: That'd be "onHit"

--element theElement: The source of the event. It's necessary! This is so you can call source in your function after the event. If it doesn't apply, use getRootElement(), but I would use MyMarker. Only a suggestion though.

--arguments: This is if you have extra arguments to your script. Works the same as the parameters for any other event.

Hope it helps ;)

P.S. If you need more info or an example, I recommend you to read the triggerClientEvent article. ;)

Link to comment

questions not ended :)

how to make visible gui window only for player who hits the marker?

  
addEvent ("onHit", true ) 
addEventHandler ("onHit", getResourceRootElement (getThisResource () ),  -- ? 
    function () 
        ...guiCreateWindow.... 
    end 
) 
  

and how to check the vehicle without a driver in marker such as gas-trailer ?

  
local cargoTrailer = createVehicle ( 584, -2244.660, -2569.4152, 33.003, 0, 0, 75 ) 
local FinishMarker = createMarker ( -56.6132, -1135.7636, 1.0781, "cylinder", 2.0, 0, 255, 0, 150 ) 
  
function TruckMarkerHit ( cargoTrailer, mathcingDimension ) 
    if cargoTrailer == FinishMarker then 
    destroyElement ( cargoTrailer ) 
  
    end 
end 
addEventHandler ( "onMarkerHit", FinishMarker, TruckMarkerHit ) 
  

..not works.

Link to comment
...
bool triggerClientEvent ( [element triggerFor=getRootElement()], string name, element theElement, [arguments...] )

--element triggerFor: Do we need it? I think so, you don't want to trigger it for everyone I guess! xD

--string name: That'd be "onHit"

--element theElement: The source of the event. It's necessary! This is so you can call source in your function after the event. If it doesn't apply, use getRootElement(), but I would use MyMarker. Only a suggestion though.

--arguments: This is if you have extra arguments to your script. Works the same as the parameters for any other event.

...

I persume triggerFor might help? :P If you create a GUI for that 1 player, it'll only show up for him by the way. ;)

And I don't understand your 2nd question... What I do see, is that... The code is a little... unusual... xD

Nah I think I know what you mean now and I'm going to explain what's wrong with your script.

If we take a look at line 5, we see the function puts the element that triggered the event in cargoTrailer. But, cargoTrailer was already assigned to the trailer! Which means that a NEW cargoTrailer variable is being created, a local one. This would mean that, even if the object is your own truck you're in, if you destroy cargoTrailer, it'll destroy your truck.

Might sound a little difficult, but later you'll see what I mean.

Now we move on to line 6. We see something different going on! Think about you being in a truck, and your truck hits the marker. local cargoTrailer will get the element of your truck. Now we look at line six:

if cargoTrailer == FinishMarker then 

In the case of my example, that would become:

if [your truck] == [the finish marker] then 

So I think you see what I mean?

These are the biggest errors I found. I think this might solve the problem:

local cargoTrailer = createVehicle ( 584, -2244.660, -2569.4152, 33.003, 0, 0, 75 ) 
local FinishMarker = createMarker ( -56.6132, -1135.7636, 1.0781, "cylinder", 2.0, 0, 255, 0, 150 ) 
  
function TruckMarkerHit ( dontPutAnExistingVariableHereItsConfusingXD, mathcingDimension ) 
    if dontPutAnExistingVariableHereItsConfusingXD == FinishMarker then 
        destroyElement ( dontPutAnExistingVariableHereItsConfusingXD ) 
    end 
end 
addEventHandler ( "onMarkerHit", FinishMarker, TruckMarkerHit ) 

;)

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