• 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 Question about loops

Extort

New Member
Joined
Apr 14, 2011
Messages
31
Reaction score
0
I was wondering how i can add a delay or something to a while or for loop? Using TFS 0.3.6pl1
Code:
		while (i == 0) do
		doCreatureAddMana(cid, mana2)
		doSendAnimatedText(getCreaturePosition(cid), '+'..(mana2), COLOR_BLUE)
		i = i + 1

This is just a small part of the script, but i would like that loop to repeat with a delay or in intervals. The way i had it set up (not the example) it would add mana2 together x amount of times and give the mana in 1 big chunk. I would like it to give it in intervals of like 1 second. Any help would be great, i do not understand a lot about loops in C++ as i just started learning C.
 
Last edited:
I was just reading about those, i don't quite understand how to do that. Could you give me an example on how it would work with this kind of script? Thank you tho :) now i know i can do it.
 
Okay after reading it, i am as confused as ever >.< i wish i understood this more, i really do. I get it a bit just don't know how to use it and how it will make a delay. :( god i feel stupid lol.

edit:Anything you can tell me that will help understand how to use these?

edit: Yeah i just started to mess around with the addEvent and all i got was a headache and a lot of errors, i have no clue what i am doing >.>
 
Last edited:
Could someone explain to me how i would use addEvent in this kind of situation? I tried looking online for stuff but i didn't find anything i could use or understood. Any info would be useful. Thank you.
 
I tried to do that, since i do not understand it, it is hard to edit it and make it work. Also if i just edit something and don't understand it i won't learn much so i wont know what to do next time i need to use addEvent. I just need a simple explanation i think as i am still reading posts about it.
 
Lua:
local combat = createCombatObject()
setCombatParam(combat, COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)
setCombatParam(combat, COMBAT_PARAM_EFFECT, CONST_ME_HITAREA)
setCombatArea(combat, createCombatArea(AREA_SQUARE1X1))

local t = {} -- table used to store event IDs for each player
local mana2 = 25

local function f(cid) -- loop function
	if isPlayer(cid) then
		-- player exists
		doCreatureAddMana(cid, mana2)
		doSendAnimatedText(getThingPos(cid), '+'..(mana2), COLOR_BLUE)
		t[cid] = addEvent(f, 1000, cid) -- schedule the event again in 1 second
	else
		-- player disconnected, remove table value
		t[cid] = nil
	end
end

-- main function
function onCastSpell(cid, var)
	if t[cid] then
		-- event is already initiated, stop it
		stopEvent(t[cid]) -- killing the event
		t[cid] = nil -- freeing table value
		doSendMagicEffect(getThingPos(cid), CONST_ME_MAGIC_RED) -- "turn off" effect
	else
		-- not initiated yet
		t[cid] = addEvent(f, 1000, cid) -- init event for player after 1 second
		doCombat(cid, combat, var) -- effects
	end
	return true
end
 
Thank you, yeah the way i had it was way different because i didn't understand anything about addEvent :( sorry for taking your time like this and thank you so much. Testing it now.

edit: It worked, kind of :p. It is doing exactly what the script is telling it to. But it is not exactly what i wanted. But with this i should be able to finish it, thank you again. If i have some problems i can't figure out i will post here.
 
Last edited:
Is there a way to set it up so it will work like in this pic? I can enter, and it turns the event on and i can leave via portal and it turns event off. But the door wont turn event off (without turning it on first)
pic.png

Code:
local t = {} -- table used to store event IDs for each player
local mana2 = 500

local function f(cid) -- loop function
	if isPlayer(cid) then
		-- player exists
		doCreatureAddMana(cid, mana2)
		doSendAnimatedText(getThingPos(cid), '+'..(mana2), COLOR_BLUE)
		t[cid] = addEvent(f, 1500, cid) -- schedule the event again in 1 second
	else
		-- player disconnected, remove table value
		t[cid] = nil
	end
end
 
-- main function
function onStepIn(cid, item, pos)
	if t[cid] then
		-- event is already initiated, stop it
		stopEvent(t[cid]) -- killing the event
		t[cid] = nil -- freeing table value
		doSendMagicEffect(getThingPos(cid), CONST_ME_MAGIC_RED) -- "turn off" effect
	else
		-- not initiated yet
		t[cid] = addEvent(f, 1500, cid) -- init event for player after 1 second
	end
	return true
end

function onStepOut(cid, item, pos)
	if t[cid] then
		stopEvent(t[cid]) 
		t[cid] = nil 
		doSendMagicEffect(getThingPos(cid), CONST_ME_MAGIC_RED) 
	end
	return true
end
 
Back
Top