### Implement Exec Functions for Console Commands
Source: https://context7.com/traderain/witcherscript/llms.txt
Provides examples of creating console-callable functions that can interact with the game world, such as spawning entities or displaying HUD notifications.
```ws
exec function printmystring(msg : String)
{
GetWitcherPlayer().DisplayHudMessage(msg);
}
exec function spawnEnemy(templatePath : String)
{
var template : CEntityTemplate;
var pos : Vector;
var rot : EulerAngles;
template = (CEntityTemplate)LoadResource(templatePath, true);
pos = thePlayer.GetWorldPosition();
rot = thePlayer.GetWorldRotation();
theGame.CreateEntity(template, pos, rot);
GetWitcherPlayer().DisplayHudMessage("Entity spawned!");
}
```
--------------------------------
### Importing and Using CParticleSystem Class
Source: https://github.com/traderain/witcherscript/blob/master/imports.rst
This snippet demonstrates the syntax for importing a class from the game engine and subsequently loading a resource to access its properties. It includes the import definition and an execution function to display resource data via a GUI notification.
```WitcherScript
import class CParticleSystem extends CResource
{
import var previewBackgroundColor : Color;
import var previewShowGrid : Bool;
import var visibleThroughWalls : Bool;
import var prewarmingTime : Float;
import var autoHideDistance : Float;
import var autoHideRange : Float;
import var renderingPlane : ERenderingPlane;
}
exec function w2p()
{
var particle : CParticleSystem;
particle = ( CParticleSystem )LoadResource( "characters\models\common\special\demon_horse\flies.w2p", true );
theGame.GetGuiManager().ShowNotification("Loaded: characters\models\common\special\demon_horse\flies.w2p" + "
" +
"renderingPlane: " + particle.renderingPlane + "
" +
"previewBackgroundColor: " + "
" +
" Red:" + particle.previewBackgroundColor.Red + "
" +
" Green:" + particle.previewBackgroundColor.Green + "
" +
" Blue:" + particle.previewBackgroundColor.Blue + "
" +
" Alpha:" + particle.previewBackgroundColor.Alpha + "
" +
"previewShowGrid: " + particle.previewShowGrid + "
" +
"visibleThroughWalls: " + particle.visibleThroughWalls + "
" +
"prewarmingTime: " + particle.prewarmingTime + "
" +
"autoHideDistance: " + particle.autoHideDistance + "
" +
"autoHideRange: " + particle.autoHideRange + "
" +
"renderingPlane: " + particle.renderingPlane);
}
```
--------------------------------
### Importing and Using Classes in Witcher Script
Source: https://context7.com/traderain/witcherscript/llms.txt
Demonstrates how to import class definitions from rttidump.xml to access engine internals. It shows how to declare, load, and interact with imported class properties, such as CParticleSystem, and their associated data types like Color.
```ws
// Import a class definition based on rttidump.xml
import class CParticleSystem extends CResource
{
import var previewBackgroundColor : Color;
import var previewShowGrid : Bool;
import var visibleThroughWalls : Bool;
import var prewarmingTime : Float;
import var autoHideDistance : Float;
import var autoHideRange : Float;
import var renderingPlane : ERenderingPlane;
}
// Use the imported class in an exec function
exec function w2p()
{
var particle : CParticleSystem;
particle = (CParticleSystem)LoadResource("characters\models\common\special\demon_horse\flies.w2p", true);
theGame.GetGuiManager().ShowNotification(
"Loaded: characters\models\common\special\demon_horse\flies.w2p" + "
" +
"renderingPlane: " + particle.renderingPlane + "
" +
"previewBackgroundColor: " + "
" +
" Red:" + particle.previewBackgroundColor.Red + "
" +
" Green:" + particle.previewBackgroundColor.Green + "
" +
" Blue:" + particle.previewBackgroundColor.Blue + "
" +
" Alpha:" + particle.previewBackgroundColor.Alpha + "
" +
"previewShowGrid: " + particle.previewShowGrid + "
" +
"visibleThroughWalls: " + particle.visibleThroughWalls + "
" +
"prewarmingTime: " + particle.prewarmingTime + "
" +
"autoHideDistance: " + particle.autoHideDistance + "
" +
"autoHideRange: " + particle.autoHideRange);
}
```
--------------------------------
### Access Global Game References
Source: https://context7.com/traderain/witcherscript/llms.txt
Shows how to access core game system instances like the player and UI manager using global references provided by the WitcherScript engine.
```ws
exec function showPlayerInfo()
{
var player : CR4Player;
player = thePlayer;
theGame.GetGuiManager().ShowNotification("Player loaded: " + player);
}
```
--------------------------------
### Define a State Machine Class in WitcherScript
Source: https://github.com/traderain/witcherscript/blob/master/latentfunctions.rst
Demonstrates how to define a state machine class extending CEntity, which is a prerequisite for using entry and latent functions.
```WitcherScript
statemachine class StateMachineExample extends CEntity {
public var delay: int;
default delay = 10;
public function start() {
this.GotoState("ExampleState");
}
}
```
--------------------------------
### Implement States with Entry and Latent Functions
Source: https://github.com/traderain/witcherscript/blob/master/latentfunctions.rst
Shows the structure of a state, including the OnEnterState event, an entry function, and a latent function utilizing the Sleep() command.
```WitcherScript
state ExampleState in StateMachineExample {
event OnEnterState(previous_state_name: name) {
this.entryFunctionExample();
}
entry function entryFunctionExample() {
this.latentFunctionExample();
}
latent function latentFunctionExample() {
Sleep(parent.delay);
}
}
```
--------------------------------
### Declare WitcherScript Variables and Types
Source: https://context7.com/traderain/witcherscript/llms.txt
Demonstrates the declaration and initialization of standard primitive types, game-specific 3D types, and complex class references used within the Witcher 3 engine.
```ws
var health : int;
var playerName : String;
var itemTag : name;
var speed : float;
var isAlive : bool;
var position : Vector;
var rotation : EulerAngles;
var transform : Matrix;
var inventory : array;
var enemies : array;
var entity : CEntity;
var template : CEntityTemplate;
health = 100;
playerName = "Geralt";
itemTag = 'silver_sword';
speed = 1.5f;
isAlive = true;
position = Vector(100, 200, 50, 1);
rotation = EulerAngles(0, 180, 0);
```
--------------------------------
### Implementing State Machines and Latent Functions in Witcher Script
Source: https://context7.com/traderain/witcherscript/llms.txt
Illustrates the creation and usage of state machines and latent functions in Witcher Script. This includes defining statemachine classes, states with entry functions, and latent functions that execute over multiple frames using Sleep() and SleepOneFrame().
```ws
// Statemachine class definition - must extend CEntity
statemachine class StateMachineExample extends CEntity
{
public var delay : int;
default delay = 10;
public function start()
{
this.GotoState("ExampleState");
}
}
// State definition with entry and latent functions
state ExampleState in StateMachineExample
{
event OnEnterState(previous_state_name : name)
{
this.entryFunctionExample();
}
// Entry function - can call latent functions
entry function entryFunctionExample()
{
this.latentFunctionExample();
}
// Latent function - executes over multiple frames
latent function latentFunctionExample()
{
Sleep(parent.delay); // Wait for delay seconds
// Continue execution after delay
}
}
// Example: Delayed action sequence
state DelayedActions in StateMachineExample
{
entry function runDelayedSequence()
{
this.performDelayedActions();
}
latent function performDelayedActions()
{
// First action
GetWitcherPlayer().DisplayHudMessage("Starting sequence...");
Sleep(2.0f); // Wait 2 seconds
// Second action
GetWitcherPlayer().DisplayHudMessage("Step 2...");
SleepOneFrame(); // Wait exactly one frame
// Final action
GetWitcherPlayer().DisplayHudMessage("Sequence complete!");
}
}
```
--------------------------------
### WitcherScript Exec Functions for Gameplay Modification
Source: https://context7.com/traderain/witcherscript/llms.txt
Demonstrates the use of 'exec' functions in WitcherScript for creating in-game console commands. These functions allow for direct manipulation of game elements like player position and health, useful for debugging and quick prototyping.
```ws
// Example complete mod file: modExecFunctionTest/content/scripts/local/latentfunction.ws
// Simple utility functions
exec function teleportForward(distance : float)
{
var pos : Vector;
var rot : EulerAngles;
pos = thePlayer.GetWorldPosition();
rot = thePlayer.GetWorldRotation();
// Calculate forward position based on rotation
pos.X = pos.X + (distance * CosF(rot.Yaw));
pos.Y = pos.Y + (distance * SinF(rot.Yaw));
thePlayer.TeleportWithRotation(pos, rot);
GetWitcherPlayer().DisplayHudMessage("Teleported " + distance + " units forward");
}
exec function healPlayer(amount : int)
{
var currentHealth : float;
currentHealth = thePlayer.GetStatPercents(BCS_Vitality);
thePlayer.GainStat(BCS_Vitality, amount);
GetWitcherPlayer().DisplayHudMessage("Healed for " + amount + " points");
}
```
--------------------------------
### Using Built-in Latent Functions for Non-Blocking Operations in Witcher Script
Source: https://context7.com/traderain/witcherscript/llms.txt
Explains the advantage of using built-in latent functions, such as LoadResourceAsync, over their synchronous counterparts to prevent game freezes during I/O operations. It also shows the import and usage of a latent function to kill internal state threads.
```ws
// Synchronous vs Latent resource loading
// LoadResource() - Synchronous, freezes game during disk read
// LoadResourceAsync() - Latent, loads without hiccups
state ResourceLoader in StateMachineExample
{
entry function loadResources()
{
this.loadResourcesAsync();
}
latent function loadResourcesAsync()
{
var resource : CResource;
// Latent load - won't freeze the game
resource = LoadResourceAsync("path\to\resource.w2ent");
// Continue after resource is loaded
if (resource)
{
GetWitcherPlayer().DisplayHudMessage("Resource loaded successfully!");
}
}
}
// Kill the internal state thread (LATENT and ENTRY only)
import latent function KillThread();
```
--------------------------------
### Define and Execute a Custom WitcherScript Exec Function
Source: https://github.com/traderain/witcherscript/blob/master/execfunctions.rst
This snippet demonstrates how to define an exec function that displays a message in the game's HUD. The function is triggered by typing the function name directly into the Witcher 3 console.
```WitcherScript
exec function printmystring(msg : String)
{
GetWitcherPlayer().DisplayHudMessage(msg);
}
```
```Console
printmystring("Hello from witcher script!")
```
--------------------------------
### WitcherScript Mod Folder Structure
Source: https://context7.com/traderain/witcherscript/llms.txt
Defines the standard directory hierarchy for WitcherScript mods within the game's 'Mods' folder. This structure is essential for the game to recognize and load custom scripts.
```text
The Witcher 3 Wild Hunt GOTY/
Mods/
modYourModName/
content/
scripts/
local/
yourscript.ws # Your custom scripts
anotherscript.ws
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.