gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
How to shuffle all strings that were loaded with getstring() in Love2D?
- User
How to shuffle all strings that were loaded with getstring() in Love2D?
- Assistant
You can shuffle an array containing all the strings loaded with
love.filesystem.loadusing themath.randomfunction in Lua. Here is an example:codefunction shuffleStrings(strings) for i = #strings, 2, -1 do local j = math.random(i) strings[i], strings[j] = strings[j], strings[i] end end -- Example usage: local strings = {} strings[1] = love.filesystem.load("string1.lua")() strings[2] = love.filesystem.load("string2.lua")() strings[3] = love.filesystem.load("string3.lua")() shuffleStrings(strings) -- Now the strings table is shuffledNote that this assumes that the strings are loaded using
love.filesystem.loadand notlove.filesystem.getString. If you're usinglove.filesystem.getString, you can split the loaded string into an array of lines usingstring.gmatch, shuffle the array, and then join the lines back into a single string.