• Announcement: Lua.org now officially recommends this forum as a meeting place for the Lua community
  • The forum is currently open to new registrations. The registration will close for a short period of time when reaching 500 active members, to upgrade the server resources.

how to use goto or continue on LUA? (1 Viewer)

zexus

Newcomer
Joined
Mar 8, 2021
Messages
8
Reaction score
0
I need to use continue in a loop in LUA

I tried this:
Code:
dofile(getDataDir() .. "actions/scripts/quests.lua")

function onSay(cid, words, param, channel)
    local mins = 60
    local hours = 60 * 60
    local days = 24 * 60 * 60
    print("----------")
    print(mins, hours, days)
    local time = os.time()
    local final = ''
    -- We are looping from 8000 to 8005, so those are the storages that we will check.
    for q, quest in pairs(zexus_quest_list) do
        print(q)
        local storage = quest -- In this local we get the information, for example if q is equal to 8000 and you print storage.name you will get "Sabre Rook".
        local str = ''



        -- don't show everything, only exausted one, cause there are a lot of quests
        if getPlayerStorageValue(cid, q) < 0 then
            -- str = 'You have never done '..storage.name..' quest! Time to get to it.'
            goto continue
        elseif time - getPlayerStorageValue(cid, q) >= 0 then
            -- str = 'You can now do '..storage.name..' quest again!'
            goto continue



        if getPlayerStorageValue(cid, q) - time > days then
            str = 'You must wait '.. math.ceil(((getPlayerStorageValue(cid, q)) - time) / days) ..' days to do the '..storage.name..' quest again.'
        elseif getPlayerStorageValue(cid, q) - time > hours then
            str = 'You must wait '.. math.ceil(((getPlayerStorageValue(cid, q)) - time) / hours) ..' hours to do the '..storage.name..' quest again.'
        else
            str = 'You must wait '.. math.ceil(((getPlayerStorageValue(cid, q)) - time) / mins) ..' minutes to do the '..storage.name..' quest again.'
        end
        final = final .. "[" .. storage.name .. "]: " .. str .. "\n\n"

        ::continue::
    end
    doShowTextDialog(cid, 2175, final)
    return true
end

but recive this error:
Code:
[14:19:31.152] [Error - LuaInterface::loadFile] data/talkactions/scripts/quests.lua:22: '=' expected near 'continue'
[14:19:31.152] [Warning - Event::loadScript] Cannot load script (data/talkactions/scripts/quests.lua)
[14:19:31.152] data/talkactions/scripts/quests.lua:22: '=' expected near 'continue'

what should i do? :(
 

Maksumić

Administrator
Staff member
Joined
Jan 6, 2020
Messages
31
Reaction score
10
I need to use continue in a loop in LUA

I tried this:
Code:
dofile(getDataDir() .. "actions/scripts/quests.lua")

function onSay(cid, words, param, channel)
    local mins = 60
    local hours = 60 * 60
    local days = 24 * 60 * 60
    print("----------")
    print(mins, hours, days)
    local time = os.time()
    local final = ''
    -- We are looping from 8000 to 8005, so those are the storages that we will check.
    for q, quest in pairs(zexus_quest_list) do
        print(q)
        local storage = quest -- In this local we get the information, for example if q is equal to 8000 and you print storage.name you will get "Sabre Rook".
        local str = ''



        -- don't show everything, only exausted one, cause there are a lot of quests
        if getPlayerStorageValue(cid, q) < 0 then
            -- str = 'You have never done '..storage.name..' quest! Time to get to it.'
            goto continue
        elseif time - getPlayerStorageValue(cid, q) >= 0 then
            -- str = 'You can now do '..storage.name..' quest again!'
            goto continue



        if getPlayerStorageValue(cid, q) - time > days then
            str = 'You must wait '.. math.ceil(((getPlayerStorageValue(cid, q)) - time) / days) ..' days to do the '..storage.name..' quest again.'
        elseif getPlayerStorageValue(cid, q) - time > hours then
            str = 'You must wait '.. math.ceil(((getPlayerStorageValue(cid, q)) - time) / hours) ..' hours to do the '..storage.name..' quest again.'
        else
            str = 'You must wait '.. math.ceil(((getPlayerStorageValue(cid, q)) - time) / mins) ..' minutes to do the '..storage.name..' quest again.'
        end
        final = final .. "[" .. storage.name .. "]: " .. str .. "\n\n"

        ::continue::
    end
    doShowTextDialog(cid, 2175, final)
    return true
end

but recive this error:
Code:
[14:19:31.152] [Error - LuaInterface::loadFile] data/talkactions/scripts/quests.lua:22: '=' expected near 'continue'
[14:19:31.152] [Warning - Event::loadScript] Cannot load script (data/talkactions/scripts/quests.lua)
[14:19:31.152] data/talkactions/scripts/quests.lua:22: '=' expected near 'continue'

what should i do? :(
You're just missing the end keyword after the last goto continue
 

zexus

Newcomer
Joined
Mar 8, 2021
Messages
8
Reaction score
0
u mean to use this?
Code:
dofile(getDataDir() .. "actions/scripts/quests.lua")

function onSay(cid, words, param, channel)
    local mins = 60
    local hours = 60 * 60
    local days = 24 * 60 * 60
    print("----------")
    print(mins, hours, days)
    local time = os.time()
    local final = ''
    -- We are looping from 8000 to 8005, so those are the storages that we will check.
    for q, quest in pairs(zexus_quest_list) do
        print(q)
        local storage = quest -- In this local we get the information, for example if q is equal to 8000 and you print storage.name you will get "Sabre Rook".
        local str = ''



        -- don't show everything, only exausted one, cause there are a lot of quests
        if getPlayerStorageValue(cid, q) < 0 then
            -- str = 'You have never done '..storage.name..' quest! Time to get to it.'
            goto continue
        elseif time - getPlayerStorageValue(cid, q) >= 0 then
            -- str = 'You can now do '..storage.name..' quest again!'
            goto continue
        end


        if getPlayerStorageValue(cid, q) - time > days then
            str = 'You must wait '.. math.ceil(((getPlayerStorageValue(cid, q)) - time) / days) ..' days to do the '..storage.name..' quest again.'
        elseif getPlayerStorageValue(cid, q) - time > hours then
            str = 'You must wait '.. math.ceil(((getPlayerStorageValue(cid, q)) - time) / hours) ..' hours to do the '..storage.name..' quest again.'
        else
            str = 'You must wait '.. math.ceil(((getPlayerStorageValue(cid, q)) - time) / mins) ..' minutes to do the '..storage.name..' quest again.'
        end
        final = final .. "[" .. storage.name .. "]: " .. str .. "\n\n"

        ::continue::
    end
    doShowTextDialog(cid, 2175, final)
    return true
end

because still the problem on line 22:

Code:
[16:26:17.343] [Error - LuaInterface::loadFile] data/talkactions/scripts/quests.lua:22: '=' expected near 'continue'
[16:26:17.343] [Warning - Event::loadScript] Cannot load script (data/talkactions/scripts/quests.lua)
[16:26:17.343] data/talkactions/scripts/quests.lua:22: '=' expected near 'continue'
 

stetre

Member
Rank: I
Joined
Jan 8, 2020
Messages
45
Reaction score
28
Location
Italy
Website
github.com
The error message tells you that the error is in script named quests.lua.
Your code loads and executes a quest.lua script, and then defines a function.

Are you sure that you are looking for the error in the right script?
 

zexus

Newcomer
Joined
Mar 8, 2021
Messages
8
Reaction score
0
The error message tells you that the error is in script named quests.lua.
Your code loads and executes a quest.lua script, and then defines a function.

Are you sure that you are looking for the error in the right script?

Yes, im 100%
The error is in data/talkactions/scripts/quests.lua:22
the other script i load is in data/actions/scripts/quests.lua
 

stetre

Member
Rank: I
Joined
Jan 8, 2020
Messages
45
Reaction score
28
Location
Italy
Website
github.com
Yes, im 100%
The error is in data/talkactions/scripts/quests.lua:22
the other script i load is in data/actions/scripts/quests.lua

Ah, ok. I assume that line 22 is where the first 'goto continue' is, then. I can't see any syntax error around there.
Which version of Lua are you using?
 

zexus

Newcomer
Joined
Mar 8, 2021
Messages
8
Reaction score
0
Ah, ok. I assume that line 22 is where the first 'goto continue' is, then. I can't see any syntax error around there.
Which version of Lua are you using?

print(_VERSION)

prints:

Lua 5.1
 

stetre

Member
Rank: I
Joined
Jan 8, 2020
Messages
45
Reaction score
28
Location
Italy
Website
github.com
print(_VERSION)

prints:

Lua 5.1

According to the Lua history it seems that the goto statement was introduced in version 5.2.
The error message is not clear at all, but the problem is most likely this.

Perhaps the interpreter assumes that 'goto' is a variable name and infers that the statement 'goto continue' is
an assignment where the '=' is missing (as in 'goto=continue'), hence the strange error message.
 

zexus

Newcomer
Joined
Mar 8, 2021
Messages
8
Reaction score
0
how can i fix this?

i was using 5.1

i tried to sudo apt-get install lua5.2


and change my Makefile editing every lua5.1 to lua5.2:

Code:
SRC = actions.cpp admin.cpp allocator.cpp baseevents.cpp beds.cpp chat.cpp\
      combat.cpp condition.cpp configmanager.cpp connection.cpp container.cpp\
      creature.cpp creatureevent.cpp cylinder.cpp database.cpp\
      databasemanager.cpp databasemysql.cpp databasesqlite.cpp depot.cpp\
      dispatcher.cpp exception.cpp fileloader.cpp game.cpp gameservers.cpp\
      globalevent.cpp group.cpp house.cpp housetile.cpp ioban.cpp ioguild.cpp\
      iologindata.cpp iomap.cpp iomapserialize.cpp itemattributes.cpp item.cpp\
      items.cpp luascript.cpp mailbox.cpp manager.cpp map.cpp monster.cpp\
      monsters.cpp movement.cpp networkmessage.cpp npc.cpp otserv.cpp\
      outfit.cpp outputmessage.cpp party.cpp player.cpp position.cpp\
      protocol.cpp protocolgame.cpp protocolhttp.cpp protocollogin.cpp\
      protocolold.cpp quests.cpp raids.cpp rsa.cpp scheduler.cpp\
      scriptmanager.cpp server.cpp spawn.cpp spells.cpp status.cpp\
      talkaction.cpp teleport.cpp textlogger.cpp thing.cpp tile.cpp tools.cpp\
      trashholder.cpp vocation.cpp waitlist.cpp weapons.cpp

OBJ = ${SRC:.cpp=.o}

CC = g++
INCS = -I/usr/include/libxml2 -I/usr/include/lua5.2 -I.
CPPFLAGS = -DHAVE_CONFIG_H -D__USE_MYSQL__ -D__ENABLE_SERVER_DIAGNOSTIC__ -D__ROOT_PERMISSION__ -D_THREAD_SAFE -D_REENTRANT
CXXFLAGS = -std=c++03 -Os -fomit-frame-pointer -Wall -Wextra -Wno-strict-aliasing -Wno-unused-parameter -Wno-array-bounds -pipe ${INCS} ${CPPFLAGS}
LDFLAGS = -s -llua5.2 -lmariadbclient -lcrypto -lboost_filesystem -lboost_date_time -lboost_system -lboost_regex -lboost_thread -lz -lgmp -lxml2 -pthread
all: options tfs

options:
    @echo tfs build options:
    @echo "CXXFLAGS = ${CXXFLAGS}"
    @echo "LDFLAGS  = ${LDFLAGS}"
    @echo "CC       = ${CC}"

.cpp.o:
    @echo '${CC} $<'
    @${CC} -c ${CXXFLAGS} $<

tfs: ${OBJ}
    @echo CC -o [email protected]
    @${CC} -o [email protected] ${OBJ} ${LDFLAGS}

clean:
    @echo cleaning
    @rm -f tfs ${OBJ}

.PHONY: all clean options

but when i try to compile the project show this errors on console:

make -j`nproc`
Code:
CC -o tfs
tfs build options:
CXXFLAGS = -std=c++03 -Os -fomit-frame-pointer -Wall -Wextra -Wno-strict-aliasing -Wno-unused-parameter -Wno-array-bounds -pipe -I/usr/include/libxml2 -I/usr/include/lua5.2 -I.   -DHAVE_CONFIG_H -D__USE_MYSQL__ -D__ENABLE_SERVER_DIAGNOSTIC__ -D__ROOT_PERMISSION__ -D_THREAD_SAFE -D_REENTRANT
LDFLAGS  = -s -llua5.2 -lmariadbclient -lcrypto -lboost_filesystem -lboost_date_time -lboost_system -lboost_regex -lboost_thread -lz -lgmp -lxml2 -pthread
CC       = g++
combat.o: In function `ValueCallback::getMinMaxValues(Player*, int&, int&, bool) const':
combat.cpp:(.text+0xcc4): undefined reference to `lua_pcall'
combat.o: In function `TargetCallback::onTargetCombat(Creature*, Creature*) const':
combat.cpp:(.text+0x1560): undefined reference to `lua_pcall'
configmanager.o: In function `ConfigManager::load()':
configmanager.cpp:(.text+0x258): undefined reference to `luaL_loadfile'
configmanager.cpp:(.text+0x26c): undefined reference to `lua_pcall'
luascript.o: In function `LuaInterface::registerFunctions()':
luascript.cpp:(.text+0x37e0): undefined reference to `luaL_register'
luascript.cpp:(.text+0x37f7): undefined reference to `luaL_register'
luascript.cpp:(.text+0x380e): undefined reference to `luaL_register'
luascript.cpp:(.text+0x3825): undefined reference to `luaL_register'
luascript.o: In function `LuaInterface::handleFunction(lua_State*)':
luascript.cpp:(.text+0x39a3): undefined reference to `lua_call'
luascript.o: In function `LuaInterface::popNumber(lua_State*)':
luascript.cpp:(.text+0x4d43): undefined reference to `lua_tonumber'
luascript.o: In function `LuaInterface::callFunction(unsigned int)':
luascript.cpp:(.text+0x74b6): undefined reference to `lua_pcall'
luascript.o: In function `LuaInterface::getField(lua_State*, char const*)':
luascript.cpp:(.text+0x9db5): undefined reference to `lua_tonumber'
luascript.o: In function `LuaInterface::getFieldUnsigned(lua_State*, char const*)':
luascript.cpp:(.text+0xa6ad): undefined reference to `lua_tonumber'
luascript.o: In function `LuaInterface::getGlobalString(lua_State*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
luascript.cpp:(.text+0xaa26): undefined reference to `lua_objlen'
luascript.o: In function `LuaInterface::getGlobalDouble(lua_State*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, double)':
luascript.cpp:(.text+0xaba5): undefined reference to `lua_tonumber'
luascript.o: In function `LuaInterface::moveValue(lua_State*, lua_State*)':
luascript.cpp:(.text+0xac3b): undefined reference to `lua_tonumber'
luascript.o: In function `LuaInterface::getArea(lua_State*, std::__cxx11::list<unsigned int, std::allocator<unsigned int> >&, unsigned int&)':
luascript.cpp:(.text+0xadaf): undefined reference to `lua_tonumber'
luascript.o: In function `LuaInterface::luaL_domodlib(lua_State*)':
luascript.cpp:(.text+0xaf8d): undefined reference to `lua_pcall'
luascript.o: In function `LuaInterface::loadBuffer(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Npc*)':
luascript.cpp:(.text+0xc850): undefined reference to `luaL_loadbuffer'
luascript.cpp:(.text+0xc8d3): undefined reference to `lua_pcall'
luascript.o: In function `LuaInterface::loadFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Npc*)':
luascript.cpp:(.text+0xc947): undefined reference to `luaL_loadfile'
luascript.cpp:(.text+0xc9e6): undefined reference to `lua_pcall'
luascript.o: In function `LuaInterface::registerFunctions()':
luascript.cpp:(.text+0x383d): undefined reference to `luaL_register'
luascript.o: In function `LuaInterface::popFloatNumber(lua_State*)':
luascript.cpp:(.text+0x5ac5): undefined reference to `lua_tonumber'
collect2: error: ld returned 1 exit status
Makefile:37: recipe for target 'tfs' failed
make: *** [tfs] Error 1
 
Last edited:

zexus

Newcomer
Joined
Mar 8, 2021
Messages
8
Reaction score
0
nvm i gonna go without continue...
thank you all guys!
 
Top