Jump to content

Question about events


jyrno42

Recommended Posts

How can i pause the clientside script until the serverside event has been finished?

Lets say i have a client side code which needs to check some thing serverside before continuing its work.

local thePlayer = getLocalPlayer( )
 
triggerServerEvent( "SampleEvent", getRootElement( ), thePlayer )
outputChatBox( "Event complete", thePlayer )
-- continue work

And Serverside

function SampleFunction( thePlayer )
 
outputChatBox( "Doing Sample Function", thePlayer )
-- do something useful
return 1
 
end
 
addEvent( "SampleEvent", true )
addEventHandler( "SampleEvent", getRootElement( ), SampleFunction )

I've managed to do this one way put its "bad-coding" IMO.

Client:

local thePlayer = getLocalPlayer( )
 
triggerServerEvent( "SampleEvent", getRootElement( ), thePlayer )
 
while true do
 
if getElementData( getLocalPlayer( ), "sampleEventDone" ) == "true" then break end 
 
end
 
outputChatBox( "Event complete", thePlayer )
-- continue work

Server:

function SampleFunction( thePlayer )
 
outputChatBox( "Doing Sample Function", thePlayer )
-- do something useful
setElementData( thePlayer, "sampleEventDone", "true", true )
return 1
 
end
 
addEvent( "SampleEvent", true )
addEventHandler( "SampleEvent", getRootElement( ), SampleFunction )

Any better solutions?

Link to comment

A better way would be to just add another event clientside that the server triggers when 'SampleFunction' is done.

Client:

triggerServerEvent("SampleEvent", getLocalPlayer())
 
function callComplete ()
--continue
end
 
addEvent( "SampleFunctionDone", true )
addEventHandler( "SampleFunctionDone", getRootElement(), callComplete)

Server:

function SampleFunction( )
outputChatBox( "Doing Sample Function", source )
-- do something useful
triggerClientEvent( source, "SampleFunctionDone", source)
end
 
addEvent( "SampleEvent", true )
addEventHandler( "SampleEvent", getRootElement( ), SampleFunction )

Link to comment

You should never loop like that in scripts - it won't work. Scripts are run in the same thread as the rest of the game logic, so nothing will happen (your event packet won't get sent and you won't get any element data returned). MTA will prevent you doing this too, by noticing that your script has been running for too long and terminating it.

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