• 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 Some questions about LUA scripting.

Hermes

dziwki kola gramy w lola
Joined
Nov 17, 2007
Messages
1,867
Reaction score
14
Location
Poland
Hi there!


I think my scripting abilities have been increased since I registered here. But there are few things that I don't understand, so I made this thread to ask you few questions.


1. Are
Code:
return TRUE and return FALSE
the same like
Code:
return true and return false
?
And how about
Code:
return 1 and return 0
?

2. How to make that if something is something then script is terminated? For example:
Code:
local number = 10
local second_number = 20
if(number == 10 and second_number == 20) then
something
elseif(number ~= 10 or second_number ~= 20) then
[B]exit script (and ofcourse rest of the script won't be executed[/B]
...

3. How to make a "stop" in a script for "interval" time? For example:
Code:
local number = 10
local second_number = 20
local interval = 2000 (in ms)
if number == 10 then
[B]script waits "interval" time, and then rest of it is executed[/B]
second_number = 20 + number
...

4. Is there a way to remove summonned creatures from certain area? If it's possible then how to set names of creatures, that will be deleted from the area when script is being executed?
Code:
for arenax = 1000, 1020 do
	for arenay = 950, 1000 do
	arenapos = {x=arenax, y=arenay, z=7, stackpos=253}
	arenacreature = getThingfromPos(arenapos)
		if arenacreature.itemid > 0 then
			doPlayerSendCancel(cid,"Someone is in the area.")
			return 1
		end
	end
end
This code is for checking is ANY creature is in certain area, it may help.

5. How to execute database queries from LUA scripts? For example how to change "worldid" of character determined in param?


Thanks in advance for all the answers,
Hermes
 
1. Open constant.lua, you will find there TRUE = 1, FALSE = 0. Both TRUE and FALSE are true as they're both numbers.

2. A script is terminated when you return a value. Simply use "return TRUE" to exit the script.

3. To wait, use Colandus' "sleep" function located in Lua Functions subforum. Although you could use addEvent for it.

4. I'd suggest you to use function mapArea also located in Lua Functions subforum, yet again by Colandus. With it you will simply loop through the area and check the creatures names and remove them.

5. Executing queries depends on what you wish to do. If you only want to use UPDATE/DELETE/INSERT INTO, then you will only need to write:
Code:
db.executeQuery("UPDATE players SET worldid = 3 WHERE name = 'Lataluren'")
But note that if the player is online it will not be updated as when he logout his current info will replace the changes that you did, by the server.

To retrieve data from SQL you use:
Code:
local res = db.storeQuery("SELECT * FROM players WHERE level > 300")

-- Check if there were any hits.
if(res:getID() ~= -1) then
    repeat
        local playerName = res:getDataString "name" -- This is same as res:getDataString("name")
        local playerLevel = res:getDataInt "level"

        -- Do anything you wish.
        
    until not res:next()
    -- res:next() returns false if there is no next row.
    -- if there is a next row, res:getDataInt etc will be updated with new values from new row.

    -- usually all nobs here use to write this way, which I want you NOT to do!! NEVER!
    while(true) do
    
        -- Code

        -- This is pointless, to use an infinite loop and then this if-statement. Do as I did with repeat until!
        if(not res:next()) then
            break
        end
    end

    -- Free the results, we don't want memory leaks :)
    res:free()
else
    -- No players above level 300 :(
end
Thanks, now you are pro like me!

With greatest loyality,
Lataluren
 
Back
Top