Uh oh.... I think I assumed too much!

Need help with SmartFoxServer? You didn't find an answer in our documentation? Please, post your questions here!

Moderators: Lapo, Bax

Sparticus
Posts: 227
Joined: 27 Feb 2006, 17:44
Location: Canada
Contact:

Uh oh.... I think I assumed too much!

Postby Sparticus » 07 Mar 2006, 23:10

I don't know why, but I had stupidly assumed that when a user connects to the server and starts playing a game, they get their own "instance" of an extension.

I had global variables set in my instance and every users was able to change it... and thus screwing up everyone elses game.

Hmm... now that I think of it... I hav eheard you use the word "user variables".... I'm guessing that's my solution....

Off to read the docs again :)
Sparticus
Posts: 227
Joined: 27 Feb 2006, 17:44
Location: Canada
Contact:

Postby Sparticus » 07 Mar 2006, 23:30

Hmm... I can't be correct with what I think I was reading....

if I had a dart game... where each time the user throws a dart it sends the dart score to the server (ignore the security issue there).... is this how I'd have to write the code to remember the users total score so far :

Code: Select all

uVars.totalscore = user.getVariable("totalscore");
uVars.totalscore = uVars.totalscore + dartscore;
_server.setUserVariables([user],uVars);


that seems ugly... each time I want to update their score, I need to get their score, increment it, then set it again?

Idealy I'd like something like :

Code: Select all

totalscore = totalscore + dartscore


and that's it.... no need to retreive it, no need to re-save it...

this possible?

I hope there is some way.... it wouldn't be horribel for darts... but for a game where there is allot of changing of variables, this could really slow things down.

Thanx for the help in advance!!
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 08 Mar 2006, 06:24

Of course you don't need to use that method.
You can just store any game information/variable in a list or object and then send the update back to the client.

Suppose your game is handled by a Zone-Level estension, you will probably have a list of active rooms/games

Code: Select all

gameList = []


when a new game is started, you catch the event and initialize a new generic object for it

Code: Select all

gameList[roomId] = {}
gameList[roomId].score = 0
gameList[roomId].bestScore = 0


Once you receive a new game update, you just use those variables and then send an update to all clients in the game room.
This also allows you to use the raw-protocol and optimize your bandwidth.

:)
Lapo
--
gotoAndPlay()
...addicted to flash games
Sparticus
Posts: 227
Joined: 27 Feb 2006, 17:44
Location: Canada
Contact:

Postby Sparticus » 13 Mar 2006, 16:00

Lapo, so if I understand everything correctly, If I wanted the server to keep track of every users score for a game... I could do something like :

scores = [] // put this in the init for the room-level extension

then when a user plays a game add this at the start of the code :

scores[username].score = 0;
scores[bestscore].score = 0;
etc

so, I think that all makes sense... but how to I free up this memory whne the user is done their game?

scores[username] = null;

?
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 13 Mar 2006, 16:03

yep, I think it's okay.
A better way would be:

Code: Select all

delete scores[username]
Lapo

--

gotoAndPlay()

...addicted to flash games
Sparticus
Posts: 227
Joined: 27 Feb 2006, 17:44
Location: Canada
Contact:

Postby Sparticus » 13 Mar 2006, 16:32

cool.... last question on this topic....

If I want to initialize their score to zero.... what is the best way? Is there any way within the init whne the extensiob is first loaded to do somethign like :

scores[].score = 0;

or something like that so when i do this :

scores[username].score += 10;

the score was initialized to zero.... and then 10 was added to it.,..

I know I could write a function to set their score to zero... but that requires the client to send 1 more bit of data saying that they they are about to play the game... and that's one more thing I need to have error checking to see if everything worked..

if there were a way to initialize it automattically that would be ideal..

any ideas?
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 13 Mar 2006, 16:41

Since you're using a string as the key for your array the best to way achieve that is to simply check for the existence of the object inside the array

Code: Select all

// Check that the value was initialized
if (scores[username] == undefined)
   scores[username] = 0
   
scores[username].score += 10
Lapo

--

gotoAndPlay()

...addicted to flash games
Sparticus
Posts: 227
Joined: 27 Feb 2006, 17:44
Location: Canada
Contact:

Postby Sparticus » 13 Mar 2006, 17:02

Ok, I lied... here's a follow up question :

can an extension call a function within another extension?

for example, I made a custom login extension... could that extension call a function within my game extension and thus initialize my user variables?

The suggestion you gave to check to see if the user's object has been defined is a good one, but since allot of data is sent back and forth, i don't want to check if it is defined each and every time the server receives a message.
User avatar
Lapo
Site Admin
Posts: 23025
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 13 Mar 2006, 17:48

can an extension call a function within another extension?


Generally speaking no.
It could be don in Java, and yes it could be done also in AS 1 but we discourage doing so.

Using more extensions for the same task can lead to problems with events etc... It's usually better to always one extension only, at Zone level.

The suggestion you gave to check to see if the user's object has been defined is a good one, but since allot of data is sent back and forth, i don't want to check if it is defined each and every time the server receives a message.


I didn't think of a much simpler way: when you catch the event of the user entering the room / zone just initialize the object. This way you will only need to do it once.

:)
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SmartFoxServer 1.x Discussions and Help”

Who is online

Users browsing this forum: No registered users and 54 guests