Benutzer-Werkzeuge

Webseiten-Werkzeuge


scriptstack

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

Beide Seiten der vorigen Revision Vorhergehende Überarbeitung
Nächste Überarbeitung
Vorhergehende Überarbeitung
scriptstack [2025/12/15 02:06]
jango
scriptstack [2025/12/15 02:50] (aktuell)
jango [Routines]
Zeile 1: Zeile 1:
 =====Manager===== =====Manager=====
  
-The scripting system is initialised by creating one or more instances of the ScriptManager class. +Das Skriptsystem wird initialisiert, indem eine oder mehrere Instanzen der Klasse "Manager" erstellt werdenJede Instanz repräsentiert eine Skriptumgebung, in der Skripte geladen und ausgeführt werden könnenJede Instanz stellt außerdem einen globalen Variablenbereich bereit, über den die Skripte Daten austauschen können.
-Each instance represents a scripting environment where scripts can be loaded and executedEach +
-instance also provides a global variable scope that the scripts can use to share data.+
  
-<code>+<code csharp>
 Manager manager = new Manager(); Manager manager = new Manager();
 </code> </code>
Zeile 11: Zeile 9:
 =====Script===== =====Script=====
  
-Once a script manager is availablescripts can be loaded by creating instances of the Script class. +Sobald ein Manager verfügbar istkönnen Skripte durch Erstellen von Instanzen der Klasse "Script" geladen werdenDer Konstruktor des Skriptobjekts benötigt eine Referenz auf den Skriptmanager und einen Namen zur Identifizierung des SkriptsStandardmäßig entspricht dieser Name einem Dateinamen auf der Festplatte.
-The script object's constructor requires a reference to the script manager and a name to identify +
-the scriptBy default, this name corresponds to a disk filename.+
  
-<code>+<code csharp>
 Script script = new Script(manager, "script.txt"); Script script = new Script(manager, "script.txt");
 </code> </code>
  
-The script object's constructor automatically compiles the script source into Conscript byte code. +Der Konstruktor des Skriptobjekts kompiliert den Skriptquellcode automatisch in Scriptstack-BytecodeStandardmäßig wird der generierte Bytecode mithilfe eines "PeepholeAssembleroptimierers optimiert. Debug Anweisungen werden hinzugefügt, um die Zuordnung des generierten Codes zum Originalquellcode zu erleichternDiese Einstellungen können über die Methoden "Debug" und "Optimise" des "Manager" gesteuert werden.
-By default, the generated byte code is optimised using a "peepholeassembly optimiser. Debug +
-instructions are added to facilitate mapping of the generated code with the original sourceThese +
-settings may be controlled by setting the ScriptManager's properties DebugMode and +
-OptimiseCode.+
  
 =====Interpreter===== =====Interpreter=====
  
-A script object represents only the programming instructions contained within and not its +Ein Skriptobjekt repräsentiert lediglich Programmanweisungen (sog Token), nicht aber seinen AusführungszustandUm das Skript auszuführenmuss eine Instanz der Klasse "Interpreter" erstellt werdenDer Konstruktor dieser Klasse benötigt eine Referenz auf das auszuführende Skript oder eine Referenz auf eine seiner FunktionenEine Skriptreferenz bewirkt die Ausführung der "main"-FunktionDer Skriptkontext ermöglicht die Ausführungssteuerung und den Zugriff auf den Ausführungszustandeinschließlich der während der Ausführung definierten Variablender nächsten auszuführenden Anweisung uswDie Klasse "Interpreter" repräsentiert eine laufende Instanz eines SkriptsDaher können mehrere Instanzen desselben Skriptobjekts innerhalb desselben Skriptmanagers ausgeführt werden, indem mehrere Interpreter erstellt werdendie auf dasselbe Skript verweisen.
-execution stateTo execute the scriptan instance of the ScriptContext class must be createdThe +
-class' constructor requires a reference to the script to be executed or a reference to one of the +
-script's functionsA script reference implies that the main function will be executedThe script +
-context provides execution controlas well as access to the execution state in terms of the variables +
-defined during executionthe next statement to be executed and so onThe ScriptContext class +
-represents a running instance of a scriptHencemultiple instances of the same script object can be +
-executed within the same script manager by creating multiple script contexts referencing the same +
-script.+
  
-<code>+<code csharp>
 // create a context for the script's main function // create a context for the script's main function
 Interpreter interpreter = new Interpreter(script); Interpreter interpreter = new Interpreter(script);
Zeile 45: Zeile 29:
  
 // create a context for one of the script's named functions // create a context for one of the script's named functions
-Function scriptFunction = script.Functions["WanderAround"];+Function scriptFunction = script.Functions["Just looking..."];
 Interpreter interpreter = new Interpreter(scriptFunction); Interpreter interpreter = new Interpreter(scriptFunction);
 </code> </code>
  
-The script context object allows execution of the referenced script via the three variants of its +Das Interpreter-Objekt ermöglicht die Ausführung des referenzierten Skripts über drei Varianten seiner "Interpret()"-Methodeunbegrenzte Ausführung, Ausführung innerhalb eines bestimmten Zeitintervalls oder Ausführung bis zu einer maximalen Anzahl ausgeführter AnweisungenDie erste Variante erlaubt die unbegrenzte Ausführung der referenzierten Skriptfunktion oder deren BeendigungEnthält das Skript eine Endlosschleife, blockiert diese Methode unbegrenztsofern keine Unterbrechung ausgelöst wirdDie "Interpret()"-Methode gibt die Gesamtzahl der seit ihrem Aufruf ausgeführten Anweisungen zurück.
-Execute method. These areexecution of scripts for an indefinite amount of time; execution of +
-scripts for a given time interval or execution of scripts for up to a maximum number of executed +
-statements. +
-The first method variant allows the referenced script function to execute indefinitely or until the +
-end of the function is reachedIf the script contains an infinite loopthis method will block +
-indefinitely unless an interrupt is generatedThe Execute method returns the total number of +
-statements executed since its invocation.+
  
-<code>+<code csharp>
 // execute indefinitely, or until termination, or a script interrupt is generated // execute indefinitely, or until termination, or a script interrupt is generated
 interpreter.Interpret(); interpreter.Interpret();
 </code> </code>
  
-The second variant of the Execute method allows the script context to execute up to a given +Die zweite Variante der "Interpret()"-Methode ermöglicht es dem Interpreter, bis zu einer vorgegebenen maximalen Anzahl von Anweisungen auszuführenDer Interpreter kann die Ausführung abbrechen, bevor das Maximum erreicht ist, wenn keine weiteren Anweisungen zu verarbeiten sind oder eine Unterbrechung ausgelöst wird.
-maximum number of statementsThe script context may break out of execution before the +
-maximum is reached if there are no more statements to process or if an interrupt is generated.+
  
-<code>+<code csharp>
 // execute up to a maximumum of 10 statements // execute up to a maximumum of 10 statements
 interpreter.Interpret(10); interpreter.Interpret(10);
 </code> </code>
  
-The third variant of the Execute method accepts a TimeSpan defining the maximum time interval +Die dritte Variante der "Interpret()"-Methode akzeptiert einen TimeSpan, der das maximal zulässige Zeitintervall für die Skriptausführung definiertDie Methode kann die Ausführung vor Ablauf des angegebenen Intervalls abbrechen, wenn keine weiteren Anweisungen zu verarbeiten sind oder eine Unterbrechung ausgelöst wirdBei einem Skript mit einer ausgewogenen Anzahl verschiedener Anweisungen kann diese Methode beispielsweise verwendet werdenum die Geschwindigkeit des Skriptsystems in der Zielumgebung in Bezug auf die pro Sekunde ausgeführten Anweisungen zu ermitteln.
-allowed for script executionThe method may break out of execution earlier than the given interval +
-if there are no more statements to process or an interrupt is generatedGiven a script with a good +
-balance of different statementsa possible use of this method is to determine the speed of the +
-scripting system on the target environment in terms of statements executed per second.+
  
-<code>+<code csharp>
 // execute for up to 10 milliseconds // execute for up to 10 milliseconds
 TimeSpan tsInterval = new TimeSpan(0, 0, 0, 0, 10); TimeSpan tsInterval = new TimeSpan(0, 0, 0, 0, 10);
Zeile 84: Zeile 55:
 </code> </code>
  
-The second and third variants of Execute may be used to implement a virtual multi-threaded 
-scripting environment. Global variables may be used as semaphores to synchronise concurrently 
-running scripts. 
  
-A script context will normally execute its referenced script function indefinitelyfor a given time +Ein Interpreter führt seine referenzierte Skriptfunktion normalerweise unbegrenzt für ein bestimmtes Zeitintervall ausbis eine maximale Anzahl von Anweisungen ausgeführt wurde oder keine weiteren Anweisungen mehr zu verarbeiten sind. In manchen Fällen ist es jedoch wünschenswertdie Ausführung vorzeitig abzubrechenbeispielsweise um die Kontrolle an den Host zurückzugeben, sobald bestimmte Anweisungen ausgeführt wurden, oder weil ein Skript zu rechenintensiv ist, um es in einem Durchgang auszuführen. 
-interval, until a given maximum number of statements are executed or until there are no more +
-statements to process. In some cases it is desirable to break execution prematurelysuch as to +
-return control to the host when specific statements are executedor because a script is too +
-computationally intensive to execute in one go.+
 Conscript provides two ways for generating script interrupts: Conscript provides two ways for generating script interrupts:
  
Zeile 100: Zeile 65:
 =====Scanner===== =====Scanner=====
  
-To allow for loading of scripts from other sources -- such as an archive filenetwork or database -- +Um das Laden von Skripten aus anderen Quellen wie Archivdateiendem Netzwerk oder Datenbanken zu ermöglichen, kann ein benutzerdefinierter Scanner an den Manager gebunden werdenDieser Scanner kann jede Klasse sein, die das Interface `Scanner` implementiert
-a custom script loader class can be developed and bound to the script manager to be used for +
-loading the scriptThe script loader class may be any class that implements the ScriptLoader +
-interface. The loader is used to retrieve the script specified in the Script class constructor and also +
-any additional include scripts defined within the original script.+
  
-<code>+<code csharp>
 // custom script loader class // custom script loader class
 public class MyScanner : Scanner public class MyScanner : Scanner
Zeile 124: Zeile 85:
 =====Variablen===== =====Variablen=====
  
-One approach for allowing a script to communicate with or control the host application entails the +Eine Möglichkeit, einem Skript die Kommunikation mit der Hostanwendung oder deren Steuerung zu ermöglichen, besteht darin, dass die Anwendung die lokalen Variablen des zugehörigen Interpreters und die globalen Variablen des zugehörigen Managers abfragtDies geschieht durch Abfragen der "LocalMemory"-Eigenschaft des "Interpreter"-Objektsder "ScriptMemory"-Eigenschaft des "Script"-Objekts oder der "GlobalMemory"-Eigenschaft des "Manager"-Objekts.
-application polling the local variables of the associated script context and global variables of the +
-associated script managerIt does this by querying the LocalDictionary property of the +
-ScriptContext objectthe ScriptDictionary property of the Script object or the +
-GlobalDicitonary property of the ScriptManager object.+
  
-<code>+<code csharp>
 // get value of local variable // get value of local variable
 int i = (int)interpreter.LocalMemory["variableName"]; int i = (int)interpreter.LocalMemory["variableName"];
Zeile 138: Zeile 95:
  
 // get value of global variable // get value of global variable
-bool bNewQuest = (bool)manager.SharedMemory["variableName"];+int i = (int)manager.SharedMemory["variableName"];
 </code> </code>
  
Zeile 153: Zeile 110:
 functions registered beforehand. functions registered beforehand.
  
-<code> +<code csharp
-// define host function to move player +// define route 
-HostFunctionPrototype hostFunctionPrototype = new HostFunctionPrototype( +Routine routine = new Routine((Type)null, "Print", (Type)null"A function to print text");
- null, "Player_MoveTo", typeof(int), typeof(int));+
 </code> </code>
  
Zeile 163: Zeile 119:
 List<Type>, is also provided: List<Type>, is also provided:
  
-<code>+<code csharp>
 // prepare parameter type list // prepare parameter type list
 List<Type> listParameterTypes = new List<Type>(); List<Type> listParameterTypes = new List<Type>();
-listParameterTypes.Add(typeof(String));+listParameterTypes.Add(typeof(string));
 listParameterTypes.Add(typeof(int)); listParameterTypes.Add(typeof(int));
 listParameterTypes.Add(typeof(int)); listParameterTypes.Add(typeof(int));
 listParameterTypes.Add(typeof(bool)); listParameterTypes.Add(typeof(bool));
-listParameterTypes.Add(typeof(AssociativeArray)); +listParameterTypes.Add(typeof(ArrayList)); 
-// define function prototype with many parameters + 
-HostFunctionPrototype hostFunctionPrototype = new HostFunctionPrototype( +// define routine with many parameters 
- typeof(float), "FuncWithManyParams", listParameterTypes);+Routine routine = new Routine((Type)null, "Print", listParameterTypes);
 </code> </code>
  
Zeile 182: Zeile 138:
 prototype without a handler: prototype without a handler:
  
-<code>+<code csharp>
 // register host function // register host function
-m_scriptManager.RegisterHostFunction(hostFunctionPrototype);+manager.Register(routine);
 </code> </code>
  
Zeile 194: Zeile 150:
 A.I. scripts in a game: A.I. scripts in a game:
  
-<code>   +<code csharp>   
-public class Enemy +public class Program Host
- HostFunctionHandler+
 { {
- // class implementation + 
- private ScriptContext m_scriptContext +    private Interpreter interpreter; 
- public Enemy(..., Script script, ...+ 
- { +    public Main(script script
- // create script context +    
- m_scriptContext = new ScriptContext(script); +        interpreter = new Interpreter(script); 
- // assign self as host function handler +        interpreter.Handler = this; 
- m_scriptContext.Handler = this; +    
- } +  
- // HostFunctionHandler implementation +    public object Invoke(String functionName, List<object> parameters
- public object OnHostFunctionCall(String strFunctionName +    
- List<object> listParameters+        if (functionName == "Print") 
- { +        
- // handle random number generator +            string str = (int)parameters[0]; 
- if (strFunctionName == "GetRandom") +            Console.WriteLine(str); 
- { +            return; 
- int iMin = (int)listParameters[0]; +        
- int iMax = (int)listParameters[1]; +        return null; 
- return s_random.Next(iMin, iMax); +    
- +     
- // handle enemy movement +}
- else if (strFunctionName == "Move"+
- { +
- int iDeltaX = (int)listParameters[0]; +
- int iDeltaY = (int)listParameters[1]; +
- Move(iDeltaX, iDeltaY); +
- } +
- // handle enemy direction +
- else if (strFunctionName == "SetDirection"+
- { +
- Direction = (EntityDirection)(int)listParameters[0]; +
- } +
- // handle enemy direction flag +
- else if (strFunctionName == "SetAutomaticDirection"+
- { +
- AutomaticDirection = (bool)listParameters[0]; +
- } +
- // handle enemy position query +
- else if (strFunctionName == "GetPosition"+
- { +
- AssociativeArray associativeArrayPosition =  +
- new AssociativeArray(); +
- associativeArrayPosition["X"] = this.X; +
- associativeArrayPosition["Y"] = this.Y; +
- return associativeArrayPosition+
- } +
- // handle player position query +
- else if (strFunctionName == "GetPlayerPosition"+
- { +
- AssociativeArray associativeArrayPosition =  +
- new AssociativeArray(); +
- associativeArrayPosition["X"] = m_player.X; +
- associativeArrayPosition["Y"] = m_player.Y; +
- return associativeArrayPosition; +
- } +
- // handle enemy fire +
- else if (strFunctionName == "CastSpell"+
- { +
- Image imageSpell = Properties.Resources.EnemySpell; +
- int iDamage = m_enemyType == EnemyType.Wizard ? 40 : 20; +
- Spell spell =  +
- new Spell(m_enemies.Room.Spells,  +
- imageSpell, false, iDamage, Direction); +
- spell.X = X; +
- spell.Y = Y - 30; +
- m_enemies.Room.Spells.Add(spell); +
- return null; +
- } +
- return null; +
- }+
 </code> </code>
  
Zeile 278: Zeile 184:
 Alternatively, a host function prototype may be registered with an associated handler directly: Alternatively, a host function prototype may be registered with an associated handler directly:
  
-<code>+<code csharp>
 // register global Sine function // register global Sine function
-HostFunctionPrototype hostFunctionPrototype = new HostFunctionPrototype( +Routine routine = new Routine(typeof(float), "Print", typeof(float)); 
- typeof(float), "Sin", typeof(float)); +manager.Register(routineprintHandler); 
-m_scriptManager.RegisterHostFunction(hostFunctionPrototypetrigHandler);+
 // register global Cosine function // register global Cosine function
-HostFunctionPrototype hostFunctionPrototype = new HostFunctionPrototype( +routine = new Routine(typeof(float), "Read", typeof(float)); 
- typeof(float), "Cos", typeof(float)); +manager.Register(routinereadHandler);
-m_scriptManager.RegisterHostFunction(hostFunctionPrototype, trigHandler); +
-// register global Tangent function +
-HostFunctionPrototype hostFunctionPrototype = new HostFunctionPrototype( +
- typeof(float), "Tan", typeof(float)); +
-m_scriptManager.RegisterHostFunction(hostFunctionPrototypetrigHandler);+
 </code> </code>
  
Zeile 307: Zeile 208:
 illustrates an alternative implementation of the earlier trigonometry functions example: illustrates an alternative implementation of the earlier trigonometry functions example:
  
-<code>  +<code csharp>  
 public class TrigonometryModule public class TrigonometryModule
  : HostModule  : HostModule
Zeile 366: Zeile 267:
 Host module registration is very similar to individual function registration: Host module registration is very similar to individual function registration:
  
-<code>+<code csharp>
 // create module instance // create module instance
 TrigonometryModule trigonometryModule = new TrigonometryModule(); TrigonometryModule trigonometryModule = new TrigonometryModule();
scriptstack.1765760789.txt.gz · Zuletzt geändert: 2025/12/15 02:06 von jango