|
| 01 Jul 2013 12:04 AM |
So, I thought it'd be fun to start a mini-games project of the sorts, however that idea may develop.
Now I have a question, should I have a separate script for each mini-game, meaning clean looking code and easily keep things organized. Or should I stuff everything into one script, could be more efficient and I could re-use some code, though I could make a little global library.
What do. |
|
|
| Report Abuse |
|
|
Voidion
|
  |
| Joined: 01 Aug 2011 |
| Total Posts: 2668 |
|
|
| 01 Jul 2013 12:13 AM |
| I'd recommend stuffing it all in one script. I was told having too many scripts will eventually cause lag. |
|
|
| Report Abuse |
|
|
|
| 01 Jul 2013 12:19 AM |
I'm trying to think of a better way to sort individual game code than this... might have to do though, was hoping to think of a more clever way :p
function runGame(game)--string if game == "blah" then setMap(game) --game code elseif game == "har" then setMap(game) --mo' game code end end |
|
|
| Report Abuse |
|
|
Frezzit
|
  |
| Joined: 28 Jun 2013 |
| Total Posts: 91 |
|
|
| 01 Jul 2013 12:47 AM |
| Place each individual game's code within a table in the script. That way it's easier to randomly choose each game mode and fetch it's scripts. |
|
|
| Report Abuse |
|
|
|
| 01 Jul 2013 01:01 AM |
I don't think there's a nice way to store that code in a table without flattening it into one line, be nice if I could do something like this:
local games = { {"Game1", local Code = print("Running game 1") for i = 1,10 do print("random code "..i) end } } |
|
|
| Report Abuse |
|
|
Frezzit
|
  |
| Joined: 28 Jun 2013 |
| Total Posts: 91 |
|
|
| 01 Jul 2013 01:17 AM |
| You can't necessarily store blank code, but you CAN store functions in tables. |
|
|
| Report Abuse |
|
|
Frezzit
|
  |
| Joined: 28 Jun 2013 |
| Total Posts: 91 |
|
|
| 01 Jul 2013 01:19 AM |
local GameModes = { (function() --Stuff end), (function() --Stuff end) }
Sure it doesn't have string titles, however you don't necessarily need them. That can just be set up in the code of the function when the game loads. |
|
|
| Report Abuse |
|
|
Frezzit
|
  |
| Joined: 28 Jun 2013 |
| Total Posts: 91 |
|
|
| 01 Jul 2013 01:20 AM |
| If not, then you could just write each function out as a variable, then store each one inside a table. |
|
|
| Report Abuse |
|
|
|
| 01 Jul 2013 01:20 AM |
| That's true, actually I might go down that route. It cuts out the if-statements and gives me some more options. c: |
|
|
| Report Abuse |
|
|
Frezzit
|
  |
| Joined: 28 Jun 2013 |
| Total Posts: 91 |
|
|
| 01 Jul 2013 01:26 AM |
You'd just need a simple way to check when the functions are done running. That can be done by having a variable at the beginning of the script though. Example:
local midgame = false
local gamemodes = { (function() --Gamemode properties and whatnot --at end of function put: midgame = false), (function() --Gamemode properties yet again. --You know the drill... midgame = false) }
while true do print('Starting new game...') midgame = true Spawn(gamemodes[math.random(1, #gamemodes)]) repeat Wait(0.03) until midgame == false print('Game over') end |
|
|
| Report Abuse |
|
|
Frezzit
|
  |
| Joined: 28 Jun 2013 |
| Total Posts: 91 |
|
|
| 01 Jul 2013 01:27 AM |
| Might work. I'm actually not entirely sure how to call functions from a table. If anything it would be easier to independently define each variable, so as to cut down on confusion. |
|
|
| Report Abuse |
|
|
|
| 01 Jul 2013 02:08 AM |
Took me a minute to figure out how, but I came up with this. c:
function bloo(bla) print(bla) end local table = {bloo} table[1]("blop") |
|
|
| Report Abuse |
|
|
Frezzit
|
  |
| Joined: 28 Jun 2013 |
| Total Posts: 91 |
|
|
| 01 Jul 2013 02:16 AM |
I prefer doing this when storing functions as variables:
local bloo = function(bla) print(bla) end local table = {bloo} table[1]("blop")
I guess whatever works for you though. You're welcome for the suggestion! |
|
|
| Report Abuse |
|
|
|
| 01 Jul 2013 02:16 AM |
yay, roughly what I might be doing then.
function Game1(var)--parameter if i can think of a use print("starting game 1 "..var) end
function Game2(var) print("starting game 2 "..var) end
local Games = {Game1,Game2} local selected = Games[math.random(1,#Games)] selected("umg i dunno...")
|
|
|
| Report Abuse |
|
|
| |
|
|
| 01 Jul 2013 11:58 AM |
on second thought, I might do something more like you originally said. :p
table = {{ (function(var) -- test test print(var) end), Name = "Game 1", Instructions = "Try not to be too cool for school, bro.", Time = 5 }} table[1][1](table[1].Name.." | "..table[1].Instructions)
|
|
|
| Report Abuse |
|
|
Frezzit
|
  |
| Joined: 28 Jun 2013 |
| Total Posts: 91 |
|
|
| 01 Jul 2013 12:19 PM |
| A table within a table? Never thought of it for something like that. Clever. |
|
|
| Report Abuse |
|
|
|
| 01 Jul 2013 12:29 PM |
| oh yes, 2d tables can be quite handy. c: |
|
|
| Report Abuse |
|
|
Frezzit
|
  |
| Joined: 28 Jun 2013 |
| Total Posts: 91 |
|
|
| 01 Jul 2013 12:33 PM |
| That really opens my mind up. Glad we could help each other. |
|
|
| Report Abuse |
|
|