• 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!

How can I run a script once per "server cycle" ?

Pteryx

Member
Joined
Mar 5, 2014
Messages
196
Reaction score
20
Yesterday I asked about the possibility of using a Lua script to control a summon's movement, combat etc, but got no answer, so I suppose there no standard API for that.

Another way to achieve the same result would be to run a script once per "server tick", and find pet-owning players and their pet via a getByGUIDxx() call or by using shared storage to communicate between scripts.

So the question ... is there any existing event/callback that can be made to run once per server cycle?

It doesn't matter what is used to call the script - it could be an autonomous item-related event like decay, or player-related, npc-related, tile-related, creature-related, etc. All that matters is that it's a Lua script that's called once per cycle.


Note: I'm aware this alone doesn't necessarily give me the "hooks" I'm looking for to control creatures, but I have other requirements for the same thing, so it would be a reasonable place to start if there isn't a full creature-control API available.
 
post error here
Do you mean my post wasn't appropriate here?

If you meant that literally, I don't have an error. I just want to interact with the OT server in a way that's certainly theoretically possible, but for which I can't find any examples.
 
I need the callback every server cycle, or as close to that as possible.

Your suggestion would work of course, and I'll definitely try it if nothing else turns up: I can run a script that schedules e.g. 10 minutes worth of events, then schedules itself to run again in 10 minutes ...
... but I think this might cause performance issues.

I'll need at least 10 events per second, and preferably something like 250 per second (though naturally it can't be faster than the frequency at which the server can execute a callback).

I read a post a couple of weeks ago that suggested scheduling a few 10's of events per second via Lua can slow down a server. OTOH the the numbers for active players, NPC's and creatures mentioned here suggest (but OFC don' prove) that the C++ code runs a good bit faster than that So I'm hoping there's a lightweight callback mediated by C++ code that I can "hijack" :)
 
Code:
<globalevent name="init" type="startup" event="script" value="init.lua"/>
Code:
function onStartup()
>>>>CODEHERE<<<<
return true
end

I need the callback every server cycle, or as close to that as possible.

Your suggestion would work of course, and I'll definitely try it if nothing else turns up: I can run a script that schedules e.g. 10 minutes worth of events, then schedules itself to run again in 10 minutes ...
... but I think this might cause performance issues.

I'll need at least 10 events per second, and preferably something like 250 per second (though naturally it can't be faster than the frequency at which the server can execute a callback).

I read a post a couple of weeks ago that suggested scheduling a few 10's of events per second via Lua can slow down a server. OTOH the the numbers for active players, NPC's and creatures mentioned here suggest (but OFC don' prove) that the C++ code runs a good bit faster than that So I'm hoping there's a lightweight callback mediated by C++ code that I can "hijack" :)

and running a globalevents script that happens on a quick interval is okay as long as you have a decent processing speed and ram, this doesn't require a stronger internet connection being that these are done serverside before updating the players client.
 
Last edited by a moderator:
and running a globalevents script that happens on a quick interval is okay as long as you have a decent processing speed and ram, this doesn't require a stronger internet connection being that these are done serverside before updating the players client.
Thanks for the info!

It also raises a couple more questions of course. :)

1. Can I keep the same Lua script running continuously if I want to (as opposed to getting very frequent callbacks and exiting the script after every processing cycle)?
I can do this in Java without stealing all the CPU cycles, but OFC Java is much more powerful than Lua for that kind of thing.​
2. What's the smallest time I should schedule for queuing my callback events? Is there a practical way for me to dynamically measure the current event queue processing rate for the server so I can adjust my callback frequency based on server load?
It could get messy if I ever add events faster than the server can process them :)
 
I don't really understand the first question and as for the second I'm not really positive, I would just go based on CPU usage, if your CPU usage seems to spike a lot when having the script then the time should be reduced. If you can figure out how to compile in 64 bit that will allow for smoother and faster execution. My events all check every 100 milliseconds for the server host time to call when to start the event and it doesn't stress the server at all. But the script is very simple, it checks a table of time values and compares them with current time. If they match then it calls the event functions.
 
Thanks again.

Would you mind posting your script? I'm sure I can learn from it.

Question one might have some bad assumptions built in, so I'll make an overly detailed version of it. Sorry in advance if it's boring reading :)

When I've worked with real.time servers before (never a game server), there have been two options (more or less) for interacting with the server environment.

1. The server calls my code (effectively sending an event, but this is often implemented as a callback).
In this case, my code cycles through "Gather information, Analyze, Decide, Act" one time, them terminates.

2. My code is started at server-startup, and never terminates.
Instead it has an internal timer that "wakes it up".
On waking up it queries the server, and the same kind of cycle I described above occurs, except rather than terminating and waiting for a callback, the program sets the timer itself and "sleeps" until the timer pops.


In practice it doesn't make that much difference to an addon, but it's always best to go with the way the server is designed.
Except, of course, if you have to "cheat" a little to do what you want :)

One thing I found out about Lua is that it's not that good at multi-threading. It seems to have been built as a "cooperative multitasking" runtime. I'm used to Java, which has preemptive multi-threading (a much better technology IMO) and I'd rather not build anything complex on the other approach :)

The "server callback" / "Event-Driven Architecture" style of server (corresponding to (1) above) works much better with a weak threading model (e.g Lua) than (2) does, so I'm hoping for server callback :)
 
Unfortunately as you said there is many limitations, your best net is to let it check interval, for a status such as storage, then execute based on that storage value, or if you want to get crazy just make c++ additions =d

What script my interv script?
 
[...]
My events all check every 100 milliseconds for the server host time to call when to start the event and it doesn't stress the server at all. But the script is very simple, it checks a table of time values and compares them with current time. If they match then it calls the event functions.
I meant the script you refer to in that post.

I'm a Java programmer, so C++ is off the table :)

My preliminary plan is about what you described above: OT "Storage" to communicate between contexts, Global variables within each Lua domain (they seem to be faster), and try to avoid saving to a database.

The good news for me is that given what you've explained so far I can already do some very interesting things.

If OTOH I want to do something really good but it's not currently possible with OT, I'll try to write an irresistable requirement for the development team, in the hope they will modify the C++ for me :)
 
Back
Top