• Announcement: Lua.org now officially recommends this forum as a meeting place for the Lua community

Weight Table issue... (1 Viewer)

Kaiome

Newcomer
Joined
Sep 9, 2021
Messages
3
Reaction score
0
Unsure of what's wrong here, non-executable and it seems to expect } near the ='s and some form of expression following. it's just a weighttable for generation of items.

See:

function decide (weighttable)
local weighttotal = 0
local weightranges = {}
for z,p in pairs
do table.insert(weightranges,{Item = z.Min = weighttotal,+1Max = weighttotal + p})
end
local match = nil
if weighttoal >0 then
local num =
math.random(1,weighttotal)
for _,p in pairs(weightranges) do if num >p.Min and num < p.Max or num == p.Min or num == p.Max then
match = p.Item
end
end
end
return match
end

(I have a separate table with weight values.)
 

Herly Quijano

Newcomer
Joined
Mar 19, 2021
Messages
77
Reaction score
10
Please use the code option in the more options menu, your code should look like this:
Lua:
function decide(weighttable)
    local weighttotal = 0
    local weightranges = {}
    for z,p in pairs do
        table.insert(weightranges,{Item = z.Min = weighttotal,+1Max = weighttotal + p})
    end
    local match = nil
    if weighttoal > 0 then
        local num = math.random(1,weighttotal)
        for _,p in pairs(weightranges) do
            if num > p.Min and num < p.Max or num == p.Min or num == p.Max then
            match = p.Item
            end
        end
    end
    return match
end
Notice how in the first for loop you use the function "pairs" without an input and in the body
Lua:
{Item = z.Min = weighttotal,+1Max = weighttotal + p}
You missed "weighttotal,+1Max" there.
 

Kaiome

Newcomer
Joined
Sep 9, 2021
Messages
3
Reaction score
0
Hm. could you possibly show me where, and why? I don't follow necessarily, and my main language is not english so please be patient haha.
 

Herly Quijano

Newcomer
Joined
Mar 19, 2021
Messages
77
Reaction score
10
@Kaiome do you wanna me being even more specific? well,
Look the line numbers of the code I posted before

In the line 4:
Lua:
for z,p in pairs do
The functions pairs need an input, like
Lua:
pairs(some_table)
and I think you already now that.

In the line 5:
Lua:
table.insert(weightranges,{Item = z.Min = weighttotal,+1Max = weighttotal + p})
the part of "Item = z.Min = weighttotal,+1Max = weighttotal + p" is bad written,
first "Item = z.Min = weighttotal" you put a dot instead a comma
second "weighttotal,+1Max" I think you wanted write "weighttotal+1,Max"
 

Kaiome

Newcomer
Joined
Sep 9, 2021
Messages
3
Reaction score
0
@Kaiome do you wanna me being even more specific? well,
Look the line numbers of the code I posted before

In the line 4:
Lua:
for z,p in pairs do
The functions pairs need an input, like
Lua:
pairs(some_table)
and I think you already now that.

In the line 5:
Lua:
table.insert(weightranges,{Item = z.Min = weighttotal,+1Max = weighttotal + p})
the part of "Item = z.Min = weighttotal,+1Max = weighttotal + p" is bad written,
first "Item = z.Min = weighttotal" you put a dot instead a comma
second "weighttotal,+1Max" I think you wanted write "weighttotal+1,Max"
Lua:
function decide (weighttable)
    local weighttotal = 0
    local weightranges = {}
    for z,p in pairs(weighttable) do
    table.insert(weightranges,{Item =} z,Min =} {weighttotal+1,Max} = weighttotal + p})
end
    local match = nil
    if weighttoal >0 then
            local num =
        math.random(1,weighttotal)
        for _,p in pairs(weightranges) do if num >p.Min and num < p.Max or num == p.Min or num == p.Max then
                    match = p.Item
                end
            end
        end
        return match
    end

My line 5 is still iffy, as you said I fixed what you had said, but missed the comma (thank you!) if you can figure out why my line 5 is wanting to be so iffy and such that would be nice. This is one of the few things keeping me from finishing my current project. As it is a main functioning aspect of everything. (line 5 needs an expression, which I don't see?) I believe it should be fine without one, but lua is deciding to not be nice.
 

stetre

Member
Rank: II
Joined
Jan 8, 2020
Messages
103
Reaction score
58
Location
Italy
Website
github.com
Lua:
...
    table.insert(weightranges,{Item =} z,Min =} {weighttotal+1,Max} = weighttotal + p})
...

As the name says, the Lua interpreter is an interpreter, not an oracle. You need an oracle to make sense of expressions like this one:

{Item =} z,Min =} {weighttotal+1,Max} = weighttotal + p}

Try replacing it with a well-formed expression, and possibly one that results in the value you actually want to insert in the table.
 
Top