Certainly! Below is a simple example of Verse code for Unreal Engine FN (UEFN). This script creates a basic game where players can spawn and collect items. When a player collects an item, their score increases, and the item is destroyed.
Main.verse
// Import necessary modules
using Core;
using Game;
using UI;
// Define a struct for item properties
struct ItemProperties {
Vector Position;
int ScoreValue;
}
// Define the game state
struct GameState {
array<Actor> Items;
int PlayerScore;
}
// Initialize the game state
fn InitializeGameState() -> GameState {
return GameState {
Items = [],
PlayerScore = 0
};
}
// Spawn items at random locations
fn SpawnItems(gameState &mut GameState, numItems: int) {
for i in 0..numItems {
let itemProps = ItemProperties {
Position = Vector(RandomRange(-5000, 5000), 100, RandomRange(-5000, 5000)),
ScoreValue = RandomRange(1, 10),
};
let itemActor = SpawnActor("ItemTemplate", itemProps.Position);
itemActor.SetUserProperty("ScoreValue", itemProps.ScoreValue);
gameState.Items.Push(itemActor);
}
}
// Check for item collection
fn CheckItemCollection(gameState &mut GameState, player: Actor) {
for item in gameState.Items {
if (item.DistanceTo(player) < 150) {
gameState.PlayerScore += item.GetUserProperty<int>("ScoreValue");
DestroyActor(item);
gameState.Items.Remove(item);
break;
}
}
}
// Update the UI with the current score
fn UpdateScoreUI(gameState & GameState) {
let scoreText = UI.FindWidget<TextBlock>("ScoreText");
scoreText.SetText("Score: " + gameState.PlayerScore.ToString());
}
// Main game loop
fn Tick(DeltaTime: float, gameState &mut GameState) {
// Get the player actor
let player = GetPlayerActor(0);
// Check if the player is collecting items
CheckItemCollection(gameState, player);
// Update the score UI
UpdateScoreUI(gameState);
}
// Entry point for the script
fn Start() {
// Initialise the game state
var gameState = InitializeGameState();
// Spawn initial items
SpawnItems(gameState, 10);
// Register the tick function
Event.OnTick.Add(fn() {
Tick(Event.DeltaTime, gameState);
});
}
// Start the game
Start();
Explanation:
-
Imports:
Core: Provides basic functionality.
Game: Provides game-related functions.
UI: Provides UI-related functions.
-
Structs:
ItemProperties: Stores properties for each item, like position and score value.
GameState: Stores the game's state, including the list of items and the player's score.
-
Functions:
InitializeGameState(): Initialises the game state with an empty item list and a score of 0.
SpawnItems(gameState &mut GameState, numItems: int): Spawns a specified number of items at random locations. Each item is given a random score value and added to the game state.
CheckItemCollection(gameState &mut GameState, player: Actor): Checks if the player is near any item. If so, it adds the item's score value to the player's score, destroys the item, and removes it from the game state.
UpdateScoreUI(gameState & GameState): Updates the UI to display the current player score.
Tick(DeltaTime: float, gameState &mut GameState): The main game loop. It gets the player actor, checks for item collection, and updates the score UI. This function is called every frame.
Start(): The entry point of the script. It initialises the game state, spawns initial items, and registers the Tick function to be called every frame.
Notes:
- ItemTemplate: Ensure you have an actor template named "ItemTemplate" in your UEFN project. This template will be spawned as the collectible items.
- ScoreText: Ensure you have a
TextBlock widget named "ScoreText" in your UI to display the player's score.
- GetPlayerActor(0): This function gets the first player in the game. Adjust the index if you have multiple players.
This script provides a basic framework for a collectable item game in UEFN. You can expand upon this by adding more features, such as different item types, power-ups, or enemy actors.