• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua Can you make this script shorter?

Xikini

I whore myself out for likes
Senator
Premium User
Joined
Nov 17, 2010
Messages
6,756
Solutions
578
Reaction score
5,305
Basically the script is trying to push the character in a random direction, when they enter/spawn/teleport onto this tile.

My question is how do we get rid of the table with a quick line of code?

Basically we need 2 random numbers ranging from -1 - 1, but both of the random numbers cannot be 0.
They can be 1 & 1, -1 & -1, but not 0 & 0.

This is the current code, posted in this thread.

Code:
local positions = {
    {-1, -1},
    {-1, 0},
    {-1, 1},
    {1, -1},
    {1, 0},
    {1, 1},
    {0, -1},
    {0, 1},
}

function onStepIn(cid, item, position, fromPosition)
    local rand = math.random(8)
    local new_pos = {x = position.x + positions[rand][1], y = position.y + positions[rand][2], z = position.z}
    doTeleportThing(cid, new_pos, true)
    return true
end

The only idea I had was to do..
Code:
local a, b = (math.random(3) - 2), (math.random(3) - 2)
but I don't know how to exclude 0's..
Unless I do it like this?
Code:
local rand_a, rand_b = (math.random(3) - 2), (math.random(3) - 2)
if rand_a == 0 and rand_b == 0 then
    repeat
        rand_b = (math.random(3) - 2)
    until rand_b ~= 0
end
But then it's almost as convoluted to look at as the table. xD

Does anyone have a better idea?

Cheers,

Xikini
 
Solution

I honestly can't fathom why you would avoid using a simple table lookup in this instance. The content is already minimal, and what you possibly gain/lose performance-wise (we are talking about nanoseconds now) is simply not worth the trouble.
maybe smth like:

Code:
for i=-1,1 do
   for j=-1,1 do
      if i~=0 and j~=0 then

      //do ur content

     end
   end
end
 
maybe smth like:

Code:
for i=-1,1 do
   for j=-1,1 do
      if i~=0 and j~=0 then

      //do ur content

     end
   end
end
But then it wouldn't be random.
You'd always be teleported -1, -1.

Thanks for the input though!
 
a yeah sry my bad

If it need to be random the solution I see is: draw the numbers as long theyre diffrent than {0,0}. For small amount of possible values, the table is better solution.
 

I honestly can't fathom why you would avoid using a simple table lookup in this instance. The content is already minimal, and what you possibly gain/lose performance-wise (we are talking about nanoseconds now) is simply not worth the trouble.
 
Solution
I honestly can't fathom why you would avoid using a simple table lookup in this instance. The content is already minimal, and what you possibly gain/lose performance-wise (we are talking about nanoseconds now) is simply not worth the trouble.
I like to improve and learn.
I just wanted to know if there was a better way to go about it, or if this was the optimal and/or cleanest way to accomplish this particular goal.

Welp, let's just mark as solved.
xP
 
Last edited by a moderator:
Without any loops/tables you would have to use code with a lot of IF statements or something bizarre like this:
Lua:
local a = math.random(-1,1)
local b = a ~= 0 and (math.random(-1,1) or math.random(2) == 2 and -1 or 1)
The chances aren't even correct for this one(some tiles have higher chance to be picked).

But as Ninja said there is no reason not to use lookup table in this case. Not only the chances will be correct, but also it should be faster and more readable.
 

Similar threads

Back
Top