question

Leina Elansary avatar image
Leina Elansary asked

Cloud script -- how does it execute

Hello I have a question that I have been searching for for a while and I can’t get it : I’ve written a normal JavaScript function in the cloud script revision and called it at the beginning of the file the normal way( no handlers ) then I returned its value with a handler function so that the client can access it ... this function is supposed to run only one time when I deploy the cloud script but what happens is that every time I call the function that returns the value .... the value changes .... it means the function runs with every log in how come ?

--- what I wanna kame I want to generate a list of random numbers and all the clients can access that list but that list always changes with each run or build

ex:

var NumbersList =[];

GenerateNumbers();

function GenerateNumbers(){

// update NumbersList randomly

}

handlers.ReturnNumbersList(){

return NumbersList; // this return diiferent list each time

}

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Rick Chen avatar image
Rick Chen answered

Deploying the CloudScript does not trigger or run the function, it merely compiles the function. When the players call the CloudScript, it should execute every steps in your function independently.

If you want to make all the clients access the same list of random numbers, you could just generate it first and then put the result list as a constant in your CloudScript.

4 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Leina Elansary avatar image Leina Elansary commented ·

Thank you Rick,

Can you please explain to me what happens if I have a list and I want to always push values to it

Does it save it's data or lose it

(From testing: It's not reliable... it sends data in a random way .. sometimes they are send , most of the time no)

Ex:

var playerspos = [];

handlers.AddNewPlayerPos = function(){

playerspos.push({x:1,y:2}); // This value doesnot appear although I do call this function first

}

handlers.GetPlayersPos = function(){

playerspos.push({x:10,y:20}); // This always works and well recieved

return playerspos;

}

0 Likes 0 ·
Rick Chen avatar image Rick Chen ♦ Leina Elansary commented ·

The CloudScript is Serverless. The function calls to CloudScript is like being executed by many instances. If you declare a global variable then call a handler to modify the variable, the modified data will be kept in that instance. When you call another handler to retrieve that variable, the result will not be persistent. Normally as your calls are assigned to other instances, they don’t have the modified data. The calls carried by that instance who has executed the function that modifies data will present the modified data. Meaning other players whose call is assigned to that instance could get the modified data even if they haven’t called the function to modify data.

I recommend to declare the global variables as constants that should not be changed by functions.

0 Likes 0 ·
Leina Elansary avatar image Leina Elansary Rick Chen ♦ commented ·

Thank you Rick, So I will have to find a saving container for the data like player data

0 Likes 0 ·
Show more comments

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.