How do i make the game wait until a funcion is called? - function

I was creating a script and i want to know how do i make the game wait until a funcion is called
If someone awnser me,ill be thankful

Firstly do not use loops, use events!
To wait for an event to happen you can use the wait method, like so:
print("Starting to wait for touch")
workspace.Part.Touched:Wait()
print("Touched!")
This will wait for the part to be touched before it continues the script.
But ofcourse, other scripts will still run, the game is not "paused", it is just that script's execution that is suspended until the event is fired.
You can also make custom "wait for call" by using for example a BoolValue like so:
local WaitObject = Instance.new("BoolValue")
function WaitOn()
WaitObject.Changed:Wait()
end
function StopWait()
WaitObject.Value = not WaitObject.Value
end
You can also place the BoolValue in the game and do the wait and stop wait in separated scripts.
If you do it in same script, remember to use different threads

Related

Windows 8 phone save state

I am quite new to windows 8 phone and I don't know all the life cycle methods and when what is called.
My problem is the following: I have a page that loads some data from the disk and when the user exits the program ( or suspends ) the data should be saved. As far as I can tell Page doesn't have an OnSuspending method only someOnNavigatingFrom, but those are not called when you just exit the program. So I read that I should use the OnSuspending in my App.xaml.cs, but this class doesn't have this data and also shouldn't have it, maybe only for OnSuspending. But I don't know how to get the data from my page in the OnSuspending method.
The OnSuspending event is quite fragile and you cannot expect it to run and save the state for a long time. But it depends on how long it would take for you to save. It doesn't even get triggered when you hit the home key while closing the app. If you really want an easy way. Just register a background task. While your app is in the background, the state can be saved and when you open the app again things are in place.
There are certain constraints With Background task as well, you cant do heavy lifting etc...here's a link you could use.
https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh977056.aspx
Implement an observer pattern (i.e. pub/sub) for your view-models to subscribe to in the event that your app is being suspended.
Your app handles the suspended event. As a result, publish a message for your view-models to respond to within your app's method handler for the suspended event.
You can use an EventAggregator or MessageBus (that I wrote).

How to protect flash app from changing system time in runtime

Is there any way to measure time from starting flash without using system time? System time can be changed by user when flash running
I'm also not sure what exactly you mean but if you want to grab a time which cannot be altered by user you could e.g. call a webservice and get "accurate" time.
Check this website here http://www.earthtools.org/webservices.htm.
By passing parameters for a certain region to the request you will get the appropriate time.
Other than retreiving time from a server there is no way to make sure the (system)time has not been altered by user.
There is also a function called "getTimer()" which returns the number of milliseconds that have elapsed since your app started. Refer to this url:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/package.html#getTimer()
The Timer class is the interface to timers, which let you run code on a specified time sequence. Use the timer.start() method to start a timer.
Add an event listener for the timer event to set up code to be run on the timer interval.

How to get to know when qtconcurrent actually run

As mentioned in doc for QtConcurrent::run:
Note that the function may not run immediately; the function will only be run when a thread is available.
So here is a question: how to handle job running?
QFutureWatcher signal start is not suitable because
This signal is emitted when this QFutureWatcher starts watching the future set with setFuture().

WebSockets and Async/Sync parts of it

How does WebSockets exactly work? Implementing them seemed weird.
First you construct the object with the address, then you define the callbacks like onopen which is called when the connection is opened. Now how does that happen if I told the websocket to connect while constructing it? If the constructor connects in async way, is it guaranteed that my onopen will be called.
To sum it up:
1) When does the websocket decide to connect, when I declare all callbacks?
2) Is send() method async? If so, is there a way to call it sync?
EDIT: I have found out send() is async, there is a bufferedAmount attribute that returns the amount of data that is buffered to be sent. However, the second part of the second question still stands.
I've found a good way to explain how this part of an event loop works is this:
In an event loop, everything runs asynchronously, except for your code.
Consecutive statements will always be executed before the next event loop iteration happens. This means that you can safely assign event listeners to the ws object, because you know that it cannot call them before current iteration completes.
As for sending, as you noticed correctly, those values are generally buffered. However, it's probably a bad idea to send any messages before the onopen event is fired, as you're buffering messages on a not yet opened connection.
I hope this answers your question.

How should I perform an asynchronous action within an accessor?

I have a simple accessor in my class:
public function get loggedIn():Boolean
{
var loggedIn:Boolean = somePrivateMethodToCheckStatus();
return loggedIn;
}
The API I'm now working with checks login status in an asynchronous fashion:
API_Class.addEventListener(API_Class.LOGIN_STATUS,onStatusCheck);
API_Class.checkLoginStatus();
function onStatusCheck(evt:API_Event):void
{
//evt.loggedIn == true or false
}
Is there a way I can perform this asynchronous request without exiting my accessor?
Simple answer: No, there is not. You will have to set up login verification in an asynchronous fashion.
I am a bit curious: Why is there a need to repeatedly poll the login status remotely? If your user logged in from within the Flash application, the status should be known. Same goes for logging out. If login and logout is handled from outside the Flash app, why not implement a notification mechanism (via JavaScript or socket connection)?
Also, if not being logged in prevents users from performing actions on the server, you could check for authorization on the server, whenever remote calls are made, and return an error if the session has expired. This would still be more efficient than repeatedly polling status info.
Not really, no. Flash runs in a single thread, and every function has to finish before events etc will be called.
One (sort of) solution would be to return three values; "yes", "no" and "pending". If it's pending the loggedIn()-method would start a check, and the client of that method should check again in a little while.
Another way would be to have the loggedIn-method send the answer to a callback instead. Eg "getLoggedInStatus(callback:Function)"
You may be interested in http://www.as3commons.org/as3-commons-eventbus/index.html
It is a handy lib that focuses on asynchronous jobs.