I have 1 question - what better, when I have INT variable, and it should be 0, or if greater, function should wait.
1 - Use while loop:
local Variable = 0
while Variable ~= 0 do
wait
end
2 - Or use Changed:() Event on IntValue Instance:
local Variable = Instance.new("IntValue")
while Variable.Value ~= 0 do
Variable.Changed:Wait()
end
Why not just do:
local Variable = Instance.new("IntValue")
Variable.Changed:Connect(function()
if Variable == 0 then return end
end)
This would probably be best
because function should wait, not be cancelled
local intVar = 0
while intVar == 0 do
wait()
end
If you’re using an int value, I prefer this.
repeat until Variable.Changed:Wait() == 0
Between the two, int values are better but metatables would also work.
You’re not going to want to hear this probably, but since this is a variable it means you are updating the variable yourself. If you’re updating the variable yourself, you should have that particular part of the code handle it. Here are a couple of ways to do it.
Original using a loop:
local var = 100
while var ~= 0 do wait() end
print("Zero!")
-- elsewhere:
var = x
Using a function:
local var = 100
local function UpdateVar(newVal)
var = newVal
if var == 0 then
print("Zero!")
end
end
-- elsewhere:
UpdateVar(x)
Using an inline check:
local var = 100
-- elsewhere:
var = x
if var == 0 then print("Zero!") end
Either of those latter two are the best because they both avoid loops and the extra bloat of metatables or IntValues. You’re the one changing the variable, you’re the one who has control over it. Use it to your advantage to make clean and efficient code.
Look like the best choice for me would be only metatables, which I sadly don’t know
(I’ll start learning them now)
Also, I’ll try explain what I want better:
I’m making “3-in-row” game, like Bejeweled
I have variable:
local AllowFall = 0
This variable allows functions execute their code ONLY when it equal zero:
local function MoveGem(StartPos, EndPos, AnimationTime, Yield, EasingStyle, Fall, Gem1, Gem2)
while AllowFall ~= 0 and Gem2 == nil do
wait()
end
...
end)
MoveGem()
will yield every gem from FALLING (not swapping places of 2 gems, if Gems are swapping, Gem2 will be table, not nil
), until AllowFall
is 0.
Also, AllowFall
variable can be increased with that MoveGem()
function, BUT ONLY if gems ARE SWAPPING, not FALLING. AllowFall
will decrease when swap will be finished, so gems which should fall, will fall only if EVERY swap is fully finished.
So, does metatables will be best choice for me?