guys i’m a newbie but i have experimented a lot and i’m able to change one name(string) to another name by using (object.text) but how do i change different name everytime i tap on balloon?
say i have have a list of 5 names: john, ruby, bob, rob , mika
what code or api or condition or table do i use to get a different name everytime i press balloon.
plz help
Easy, put this code in a file called shuffleBag.lua
local function shuffle = ( t, iter ) local iter = iter or 1 local n for i = 1, iter do n = #t while n \>= 2 do -- n is now the last pertinent index local k = math.random(n) -- 1 \<= k \<= n -- Quick swap t[n], t[k] = t[k], t[n] n = n - 1 end end return t end local m = {} function m.newBag( entries ) local data = {} for k,v in pairs( entries ) do data[#data+1] = v -- shuffle( data, #data ) -- local index = 1 local bag = {} function bag.get() local retVal = data[index] index = index + 1 if( index \> #data ) then shuffle(data) index = 1 end return retVal end -- return bag end return m
Now use it anywhere like this:
local shuffleBag = require "shuffleBag" local stuff= { "bob", "bill", "sue", "danny", "sarah" } local bag = shuffleBag.new( stuff } for i = 1, 10 do print( i, bag.get() ) end
Or… you could skip all that and use my more sophisticated ShuffleBag as part of SSK2.
-
Get SSK2: https://github.com/roaminggamer/SSK2
Docs: https://roaminggamer.github.io/RGDocs/pages/SSK2/
-
Install: https://roaminggamer.github.io/RGDocs/pages/SSK2/#installing-ssk2
-
Use SSK2 ShuffleBag: https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/shufflebag/
local bag = ssk.shuffleBag.new( “bob”, “bill”, “sue”, “danny” ) – also has insert() bag:insert(“sarah”) – Shuffle once bag:shuffle() – Use for i = 1, 10 do print( i, bag:get() ) end
Easy, put this code in a file called shuffleBag.lua
local function shuffle = ( t, iter ) local iter = iter or 1 local n for i = 1, iter do n = #t while n \>= 2 do -- n is now the last pertinent index local k = math.random(n) -- 1 \<= k \<= n -- Quick swap t[n], t[k] = t[k], t[n] n = n - 1 end end return t end local m = {} function m.newBag( entries ) local data = {} for k,v in pairs( entries ) do data[#data+1] = v -- shuffle( data, #data ) -- local index = 1 local bag = {} function bag.get() local retVal = data[index] index = index + 1 if( index \> #data ) then shuffle(data) index = 1 end return retVal end -- return bag end return m
Now use it anywhere like this:
local shuffleBag = require "shuffleBag" local stuff= { "bob", "bill", "sue", "danny", "sarah" } local bag = shuffleBag.new( stuff } for i = 1, 10 do print( i, bag.get() ) end
Or… you could skip all that and use my more sophisticated ShuffleBag as part of SSK2.
-
Get SSK2: https://github.com/roaminggamer/SSK2
Docs: https://roaminggamer.github.io/RGDocs/pages/SSK2/
-
Install: https://roaminggamer.github.io/RGDocs/pages/SSK2/#installing-ssk2
-
Use SSK2 ShuffleBag: https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/shufflebag/
local bag = ssk.shuffleBag.new( “bob”, “bill”, “sue”, “danny” ) – also has insert() bag:insert(“sarah”) – Shuffle once bag:shuffle() – Use for i = 1, 10 do print( i, bag:get() ) end