Custom variables for a leaderboard?

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

AgentItay
Posts: 60
Joined: Mon Aug 24, 2015 8:54 pm
Location: Israel

Custom variables for a leaderboard?

Post by AgentItay » Sun Nov 29, 2015 9:55 am

Hello, I am trying to create a multiplayer game which is basically a trivia, at the start the players have to enter the amount of players engaging in the game, I want that as soon as they do that, the game will create a variable for each of them, for example variable number 12 for player number 12 and then everytime there is a question, the computer will choose a random player (already know and did that using the random() ), but how do I add a leaderboard and how do I make the computer read and understand the random player and assign him to his variable?

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3999
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Custom variables for a leaderboard?

Post by bn » Sun Nov 29, 2015 12:48 pm

Hi Itay,

I think an array could do what your describe. The main index would be the player number

in this script I create an array with the number of players indicated and assign each playe a random score. This score is then queried and displayed in a field

Since I put the Array into a script local variable sPlayerArray it could be accessed from any handler in the script. Of course you could also make this a global variable.

Code: Select all

local sPlayerArray
on mouseUp
   ask "how manu players?"
   if it is not a number then exit mouseUp -- check the input more thoroughly
   
   put it into tPlayers
   
   repeat with i = 1 to tPlayers
      put random (50)  into sPlayerArray[i]["score"]
   end repeat
   
   put the keys of sPlayerArray into tPlayerKeys
   put random (the number of lines of tPlayerKeys)into tRandomPlayer
   put sPlayerArray[tRandomPlayer]["score"] into tRandomPlayerScore
   put "RandomPlayer number :" & tRandomPlayer & cr & "score of player " & tRandomPlayer & ": " & tRandomPlayerScore into field 1
   
end mouseUp
I think an array is the easiest way to do this but you would have to make yourself familiar with arrays
If you don't want to go with arrays you could also use an itemized list.
i.e. make a list with a number on each line, then you could put the score into item 2 of that line

Code: Select all

global gPlayerList
on mouseUp
   ask "how manu players?"
   if it is not a number then exit mouseUp -- check the input more thoroughly
   
   put it into tPlayers
   repeat with i = 1 to tPlayers
      put i & return after gPlayerList
      put random (30) into item 2 of line i of gPlayerList 
   end repeat
   
   put random ( the number of lines of gPlayerList) into tRandomPlayer
   put item 2 of line tRandomPlayer of gPlayerList into tRandomPlayerScore
   put "RandomPlayer number :" & tRandomPlayer & cr & "score of player " & tRandomPlayer & ": " & tRandomPlayerScore into field 1
end mouseUp
this is essentially the same as the first piece of code, just using a list, and I put the list into a global variable this time.

Kind regards
Bernd

AgentItay
Posts: 60
Joined: Mon Aug 24, 2015 8:54 pm
Location: Israel

Re: Custom variables for a leaderboard?

Post by AgentItay » Thu Dec 03, 2015 9:02 am

Thanks bn, may I also know how to create a clock timer, for example:
it starts to count from 0:00 while the left stands for hour and right stands for minutes, every hour equals one minute in real life, while every minute equals one second in real life, and as soon as the clock reaches 2 hours, something happens.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3999
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Custom variables for a leaderboard?

Post by bn » Thu Dec 03, 2015 9:51 am

Hi Agent,

don't tell anybody that I gave you this code, but for doing a non-blocking timer you have to get your head around the "send "command" in xxx timeUnits" constructs. Otherwise the timer would block your game.

you also should know how a script local variable works, the three script local variables I use are declared above the handlers. There are other ways to do all this, this is one way.

Code: Select all

local sTimerIsRunning = false, sMinutes, sSeconds

on mouseUp
   put true into sTimerIsRunning
   put 0 into sMinutes
   put 0 into sSeconds
   send "startTimer" to me in 0 milliseconds
   
end mouseUp

on startTimer
   if sTimerIsRunning then
      add 1 to sSeconds
      if sSeconds = 60 then 
         put 0 into sSeconds
         add 1 to sMinutes
      end if
      if sSeconds < 10 then
         put "0" & sSeconds into tDisplaySecs
      else
         put sSeconds into tDisplaySecs
      end if
      put sMinutes & ":" & tDisplaySecs into field "fRes"
      if sMinutes = 2 then 
         put false into sTimerIsRunning
         send "bingo" to me in 0 milliseconds
         exit startTimer
      end if
      send startTimer to me in 1 second
   end if
end startTimer

on Bingo
   put cr & "Game over" after field "fRes"
end Bingo
Kind regards
Bernd

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Custom variables for a leaderboard?

Post by Simon » Thu Dec 03, 2015 6:32 pm

Hey Everybody Look Here ^^^^
Bernd is giving away code! :shock:

Bernd helps people :shock: :shock: :shock:

Simon :)
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Custom variables for a leaderboard?

Post by dunbarx » Thu Dec 03, 2015 6:52 pm

OK, that's it. I am going to start giving away code as well.

Simon? Don't hate me.

Craig

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3999
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Custom variables for a leaderboard?

Post by bn » Thu Dec 03, 2015 7:32 pm

Bernd is giving away code!
Simon, you blew it. Now everybody knows about it. It was supposed to be a secret between agentItay and me.

Of course that was a one time raffle.

Because today is "BlackCodingThursday"

:)

Kind regards
Bernd

and because I am in a give away mood:

here is a version that syncs the timer to the system and additional bonus: it tells you how many milliseconds it is off the real second. This one is less prone to suffer delays from other stuff that is going on while the game is on and will catch on to the real second.

Code: Select all

local sTimerIsRunning = false, sMinutes, sSeconds, sStartMillisecs

on mouseUp
   put true into sTimerIsRunning
   put 0 into sMinutes
   put 0 into sSeconds
   put (1000 - char -3 to - 1 of the milliseconds) into tWhen
   send "startTimer" to me in tWhen milliseconds
   put the milliseconds - (1000 - tWhen)  into sStartMillisecs
   
end mouseUp

on startTimer
   if sTimerIsRunning then
      add 1 to sSeconds
      if sSeconds = 60 then 
         put 0 into sSeconds
         add 1 to sMinutes
      end if
      if sSeconds < 10 then
         put "0" & sSeconds into tDisplaySecs
      else
         put sSeconds into tDisplaySecs
      end if
      
      put the milliseconds - sStartMillisecs - ((sSeconds + sMinutes *60)*1000) into tDiffSec
      put sMinutes & ":" & tDisplaySecs && tDiffSec into field "fRes"
      if sMinutes = 2 then 
         put false into sTimerIsRunning
         send "bingo" to me in 0 milliseconds
         exit startTimer
      end if
      put 1000 - char -3 to - 1 of the milliseconds into tWhen
      send startTimer to me in tWhen milliseconds
   end if
end startTimer

on Bingo
   put cr & "Game over" after field "fRes"
end Bingo

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: Custom variables for a leaderboard?

Post by Simon » Thu Dec 03, 2015 10:05 pm

Great.
Just Great.

Next you two will be helping people without being paid for it, have conversations with complete strangers, be part of a community.
I miss the good old days.
Hi [Insert name here], this should help you out

Code: Select all

on mouseUp
   put base64Decode("RmlndXJlIGl0IG91dCB5b3Vyc2VsZg==")
end mouseUp
I'll go as far as to predicted that by the end of this month the pair of you will be giving gifts, signing cards to people you barely speak to all year. Spreading good will and cheer to all men.
Bah i say... Bah

Simon :)
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

AgentItay
Posts: 60
Joined: Mon Aug 24, 2015 8:54 pm
Location: Israel

Re: Custom variables for a leaderboard?

Post by AgentItay » Fri Dec 04, 2015 9:17 am

Hahaha, thanks Bernd. :D

AgentItay
Posts: 60
Joined: Mon Aug 24, 2015 8:54 pm
Location: Israel

Re: Custom variables for a leaderboard?

Post by AgentItay » Sun Dec 06, 2015 1:48 pm

I need a bit more help (even though its not BlackCodingThursday :P :D).
1. I want the timer to do the following: If it is higher than 1:00, than it will display "It is now 1:00" and if it is higher than 2:00 it will display "it is now 2:00", when I did that it created a loop because 2:00+ is still higher than 1:00, so it just messes everything up.
2. I want to make some things with the grab option, I already did that, but I want to set the users to only be able to grab the object within a limited area in the screen, for example:
-------- - - - - -- - |--------------------------
| USERS CAN |
| GRAB HERE | CANT HERE
|--------- - - - -- -- -----------------------------

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3999
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Custom variables for a leaderboard?

Post by bn » Mon Dec 07, 2015 8:38 pm

1. I want the timer to do the following: If it is higher than 1:00, than it will display "It is now 1:00" and if it is higher than 2:00 it will display "it is now 2:00", when I did that it created a loop because 2:00+ is still higher than 1:00, so it just messes everything up.
You would have to post an example of your code to illustrate what you are doing. Do you know that you can restrict to a range of values in a conditional statement?

if x >= 1 and x < 2 then

if x >= 2 then
2. I want to make some things with the grab option, I already did that, but I want to set the users to only be able to grab the object within a limited area in the screen, for example:
I suppose you put "grab me" into a mouseDown handler, before "grab me" you can test for a location of the control and exit mouseDown if the location is not within you predetermed boundaries.

Kind regards
Bernd

AgentItay
Posts: 60
Joined: Mon Aug 24, 2015 8:54 pm
Location: Israel

Re: Custom variables for a leaderboard?

Post by AgentItay » Tue Dec 08, 2015 10:14 am

bn wrote:
1. I want the timer to do the following: If it is higher than 1:00, than it will display "It is now 1:00" and if it is higher than 2:00 it will display "it is now 2:00", when I did that it created a loop because 2:00+ is still higher than 1:00, so it just messes everything up.
You would have to post an example of your code to illustrate what you are doing. Do you know that you can restrict to a range of values in a conditional statement?

if x >= 1 and x < 2 then

if x >= 2 then
2. I want to make some things with the grab option, I already did that, but I want to set the users to only be able to grab the object within a limited area in the screen, for example:
I suppose you put "grab me" into a mouseDown handler, before "grab me" you can test for a location of the control and exit mouseDown if the location is not within you predetermed boundaries.

Kind regards
Bernd
Thanks.
And about the second one, I put "grab me" into a mouseStillDown handler, and before the "grab me" I put a "if intersect(me, X) blablabla."
and it is working, but if I move it too fast, it will eventually let me move it out of the given location.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3999
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Custom variables for a leaderboard?

Post by bn » Tue Dec 08, 2015 10:43 am

Hi,

I think to put the test on a mouseStillDown message is too slow, mouseStilldown is only sent on idle, see idleRate in the dictionary.
By default it is sent every 200 milliseconds. You could lower the idleRate but at idle a lot of things happen that you really probably would not have to happen quite so often (LC does cleaning up on idle)

By contrast mouseMove would be a good message to do your checks.

Try this with a button or graphic or whatever.

Code: Select all

local sMoving
local sLastMS = 0

on mouseDown
   put true into sMoving
   grab me
end mouseDown

on mouseMove
   if not sMoving then exit mouseMove
   put the milliseconds - sLastMS into field "fRes"
   put the milliseconds into sLastMS
end mouseMove

on mouseUp
   put false into sMoving
end mouseUp

on mouseRelease
   mouseUp
end mouseRelease
you can see the milliseconds it takes for the roundtrip while moving. Here you could do your conditionals.

Kind regards
Bernd

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3999
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Custom variables for a leaderboard?

Post by bn » Tue Dec 08, 2015 2:05 pm

Hi Itay,

here is a stack that constrains an object to an arbitrary boundary when it is dragged.

This is not "intersect", just stop at the border.

Have a look at the code, it uses mouseMove. It does not use "grab me". This way you have better control over locating the object within boundaries.

Kind regards

Bernd
Attachments
constrainObject.livecode.zip
(1.78 KiB) Downloaded 179 times

AgentItay
Posts: 60
Joined: Mon Aug 24, 2015 8:54 pm
Location: Israel

Re: Custom variables for a leaderboard?

Post by AgentItay » Tue Dec 08, 2015 10:23 pm

Thank you very much Bernd.
And last thing, I am planning to release something I developed using Livecode community, what should I include in my .zip file? Only my .exe file (of course :P) my .livecode file, this thing of the GNU license (edited to my needs of course)

Code: Select all

 <one line to give the program's name and a brief idea of what it does.>
    Copyright (C) <year>  <name of author>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
And that's it? (Also, I can see that it says " and each file should have at least the “copyright” line and a pointer to where the full notice is found." Should I direct them to this? (http://www.gnu.org/licenses/gpl-3.0.en.html) or should I include another text file in my .zip folder including the entire text here (http://www.gnu.org/licenses/gpl-3.0.en.html)? Thanks in advance and sorry for all of those questions, that's my first time to release an application, and licenses issues are the last thing I want to run into. :P)

Post Reply

Return to “Talking LiveCode”