File size: 7,282 Bytes
24b81cb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
/**
\brief ScriptCallQueue Class provide "lazy" calls - when we don't want to execute function immediately but later during frame update (used mainly in UI)
\n usage:
@code
class Arkanoid extends Game
{
ref ScriptCallQueue m_CallQueue = new ScriptCallQueue();
ScriptCallQueue GetCallQueue() {
return m_CallQueue;
}
override void OnUpdate(float timeslice)
{
m_CallQueue.Tick(timeslice);
...
}
...
}
class MyObject
{
int m_cnt = 0;
void Hello(int p1, string p2)
{
Print("Hello( " + p1 + " , " + p2 + ")");
}
void Test()
{
Print(m_cnt);
m_cnt++;
if (m_cnt > 10)
{
ScriptCallQueue queue = GetGame().GetCallQueue();
queue.Remove(Test);
}
}
}
void Test(MyObject obj)
{
ScriptCallQueue queue = GetGame().GetCallQueue();
queue.CallLater(obj.Hello, 5000, false, 65, "world"); // adds call 'obj.Hello(65, "world")' into queue, and it will be executed once after 5s
queue.CallLater(obj.Test, 3000, true); // adds call 'obj.Test()' into queue, and it will be executed each 3s
queue.Call(obj.Hello, 72, "world 2"); // adds call 'obj.Hello(72, "world 2")' into queue, and it will be executed next frame (on next call of ScriptCallQueue.Tick)
}
@endcode
*/
class ScriptCallQueue
{
//! executes calls on queue if their time is already elapsed, if 'repeat = false' call is removed from queue
proto native void Tick(float timeslice);
//! adds call into the queue with given parameters and arguments (arguments are held in memory until the call is executed/removed or ScriptCallQueue is destroyed)
proto void Call(func fn, void param1 = NULL, void param2 = NULL, void param3 = NULL, void param4 = NULL, void param5 = NULL, void param6 = NULL, void param7 = NULL, void param8 = NULL, void param9 = NULL);
//! adds call into the queue with given parameters and arguments (arguments are held in memory until the call is executed/removed or ScriptCallQueue is destroyed)
proto void CallByName(Class obj, string fnName , Param params = NULL);
//! adds call into the queue with given parameters and arguments (arguments are held in memory until the call is executed/removed or ScriptCallQueue is destroyed)
proto void CallLater(func fn, int delay = 0, bool repeat = false, void param1 = NULL, void param2 = NULL, void param3 = NULL, void param4 = NULL, void param5 = NULL, void param6 = NULL, void param7 = NULL, void param8 = NULL, void param9 = NULL);
//! adds call into the queue with given parameters and arguments (arguments are held in memory until the call is executed/removed or ScriptCallQueue is destroyed)
proto void CallLaterByName(Class obj, string fnName, int delay = 0, bool repeat = false, Param params = NULL);
//! remove specific call from queue
proto void Remove(func fn);
//! return Remaining time to the call execution (in miliseconds)
proto int GetRemainingTime(func fn);
//! remove specific call from queue
proto void RemoveByName(Class obj, string fnName);
//! return Remaining time to the call execution (in miliseconds)
proto int GetRemainingTimeByName(Class obj, string fnName);
//! remove all calls from queue
proto native void Clear();
};
/**
\brief ScriptInvoker Class provide list of callbacks
\n usage:
@code
class Player
{
ref ScriptInvoker m_DeathInvoker = new ScriptInvoker();
void OnKilled()
{
m_DeathInvoker.Invoke(this);
}
}
void LogPlayerDeath(p)
{
Print("RIP " + p);
}
class Game
{
void RemovePlayer(Player p)
{
}
void GameOver()
{
}
}
void OnPlayerSpaned(Player p)
{
Game game = GetGame();
p.m_DeathInvoker.Insert(LogPlayerDeath);
p.m_DeathInvoker.Insert(game.RemovePlayer);
p.m_DeathInvoker.Insert(game.GameOver);
}
@endcode
*/
class ScriptInvoker
{
//! invoke call on all inserted methods with given arguments
proto void Invoke(void param1 = NULL, void param2 = NULL, void param3 = NULL, void param4 = NULL, void param5 = NULL, void param6 = NULL, void param7 = NULL, void param8 = NULL, void param9 = NULL);
//! insert method to list
proto bool Insert(func fn, int flags = EScriptInvokerInsertFlags.IMMEDIATE);
//! remove specific call from list
proto bool Remove(func fn, int flags = EScriptInvokerRemoveFlags.ALL);
//! count how many times this fn is actively present in the Invoker
proto int Count(func fn);
//! remove all calls from list
proto native void Clear();
};
enum EScriptInvokerInsertFlags
{
NONE,
/**
\brief It gets added in immediately, which means that when called while an invoker is running, it will call this newly added call in the same run
\note Default flag, as that is the original behaviour, although it might cause endless Insert chain now... (still better than undefined behaviour)
\note In case of "Possible endless Insert detected" VME, either create an exit, remove the IMMEDIATE flag or make the insert UNIQUE
\note The Endless Insert is detected by seeing if "amount of calls > initial size + 128"
*/
IMMEDIATE,
/**
\brief Only one call to this instance+method is ever expected
\note Will throw a VME when a second one is attempted to be added
\note If it was already added without the flag, it will also throw a VME and keep the first of all previously inserted
*/
UNIQUE,
}
enum EScriptInvokerRemoveFlags
{
NONE,
/**
\brief Default flag
\note Don't use this if you want it to remove only the last insert instead of all of them
*/
ALL,
}
/**
\brief Designed to hold 1 valid call
*/
class ScriptCaller
{
//! ScriptCaller is meant to be created through Create
private void ScriptCaller();
//! Creates a ScriptCaller
static proto ScriptCaller Create(func fn);
//! Replaces the current registered func with the new one, throws errors if unsuccessful
proto void Init(func fn);
//! Invoke call on the registered func, throws errors if unsuccessful
proto void Invoke(void param1 = null, void param2 = null, void param3 = null, void param4 = null, void param5 = null, void param6 = null, void param7 = null, void param8 = null, void param9 = null);
//! Checks if the ScriptCaller is valid
proto bool IsValid();
/**
\brief Compares this script caller against another script caller
\note May return true even if either one is invalid
@code
class SomeClass
{
void SomeMethod()
{
}
}
void Test()
{
SomeClass instanceA = new SomeClass();
SomeClass instanceB = new SomeClass();
ScriptCaller callerA;
ScriptCaller callerB;
//! Two methods that are to the same instance
callerA = ScriptCaller.Create(instanceA.SomeMethod);
callerB = ScriptCaller.Create(instanceA.SomeMethod);
Print(callerA.Equals(callerB)); //! "1"
Print(callerA == callerB); //! "0"
Print(callerA); //! "ScriptCaller callerA = ScriptCaller<87bc2d40>"
Print(callerB); //! "ScriptCaller callerB = ScriptCaller<87bc3600>"
//! Two methods belonging to different instances
callerA = ScriptCaller.Create(instanceA.SomeMethod);
callerB = ScriptCaller.Create(instanceB.SomeMethod);
Print(callerA.Equals(callerB)); //! "0"
Print(callerA == callerB); //! "0"
Print(callerA); //! "ScriptCaller callerA = ScriptCaller<87bc3c40>"
Print(callerB); //! "ScriptCaller callerB = ScriptCaller<87bc2d40>"
}
@endcode
*/
proto bool Equals(notnull ScriptCaller other);
}; |