Skip to content

itIsMaku/maku_grid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

4 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Grid System

A sophisticated grid-based content management system for FiveM that enables efficient loading and unloading of game content based on player proximity. This system divides the game world into grids and dynamically manages content (NPCs, objects, bigdata, etc.) within each grid.

๐Ÿ“‹ Table of Contents

๐ŸŽฏ Overview

Grid System provides a lightweight framework for managing game content efficiently by:

  • Dividing the world into grid cells
  • Loading content only when players are in surrounding cells
  • Automatically unloading content when players leave the area
  • Supporting custom content types with different behaviors
  • Providing persistence options for permanent content

โœจ Features

  • Dynamic Grid Management: Automatic player tracking across grid cells
  • Content Type System: Flexible content registration with customizable policies
  • Client/Server Logic: Content can be processed on either client or server side
  • Persistence Options: Temporary or database-stored content
  • Debug Visualization: Visual grid boundaries and content information
  • Permission System: Owner-only deletion and server-only content handling
  • ImGUI Integration: Built-in debug interface using itIsMaku/five-imgui

๐Ÿš€ Installation

  1. Download and place the resource in your FiveM resources folder
  2. Ensure you have mysql-async or oxmysql installed and configured
  3. Add ensure maku_grid to your server.cfg

Dependencies

  • mysql-async or oxmysql - For database operations
  • FXServer with Lua 5.4 support

โš™๏ธ Configuration

Edit config.lua to customize the grid system:

Config = {
    Debug = false,        -- Enable debug mode with visual grid display
    GridSize = 350.0,     -- Size of each grid cell in game units
}

๐Ÿ—บ๏ธ Grid System

How Grids Work

The world is divided into square grid cells of configurable size (default 350 units). Each grid is identified by coordinates (e.g., "0:0", "1:-1").

Grid Functions

-- Get grid coordinates from world position
local gridX, gridY = exports.maku_grid:getGridCoordinates(coordsX, coordsY)

-- Get grid ID from coordinates
local gridId = exports.maku_grid:getGridId(gridX, gridY)

-- Get grid ID directly from position
local gridId = exports.maku_grid:getGridIdFromPosition(coordsX, coordsY)

-- Get surrounding grid IDs (3x3 area)
local surroundingGrids = exports.maku_grid:getSurroundingGridIds(gridX, gridY)

๐Ÿ“ฆ Content System

Content Types

Content types define how content will be handled after creating or deleting. Each type has specific policies that control its behavior.

Registering Content Types

-- Register a new content type
exports.maku_grid:registerContentType('your_content_type', {
    POLICIES.LOGIC.CLIENT,           -- Process on client
    POLICIES.PERSISTENCE.STORAGE,    -- Store in database
    POLICIES.HANDLE.NONE             -- Allow both client and server manipulate with data through events
})

Content Logic

Define how content is loaded and unloaded:

-- Client-side loading logic
exports.maku_grid:setContentLoadLogic('your_content_type', function(gridId, contentType, content, playerId)
    -- Your loading code here
    local entity = CreatePed(...)

    return true, { entity = entity } -- Return success and data
end)

-- Client-side unloading logic
exports.maku_grid:setContentUnloadLogic('your_content_type', function(gridId, contentType, content, playerId)
    -- Your unloading code here
    if content.data and content.data.entity then
        DeleteEntity(content.data.entity)
        return true
    end

    return false
end)

๐Ÿ” Policies

Policies control how content types behave:

Logic Policies

  • POLICIES.LOGIC.SERVER - Content logic runs on server
  • POLICIES.LOGIC.CLIENT - Content logic runs on client

Handle Policies

  • POLICIES.HANDLE.NONE - Both client and server can manage content
  • POLICIES.HANDLE.SERVER_ONLY - Only server can add/remove content

Persistence Policies

  • POLICIES.PERSISTENCE.STORAGE - Content is saved to database
  • POLICIES.PERSISTENCE.TEMPORARY - Content is disappears on resource restart

Removal Policies

  • POLICIES.REMOVE.OWNER_ONLY - Only content owner can delete it

๐Ÿ“š Examples

๐Ÿ”ง API Reference

Server Events

-- Add content to a grid
TriggerEvent('maku_grid:addContent', gridId, contentType, content)

-- Remove content from a grid
TriggerEvent('maku_grid:removeContent', gridId, contentType, contentId)

Client Events

-- Triggered when player moves to new grid
RegisterNetEvent('maku_grid:updatePlayerGrid', function(gridId, content)
    -- Handle grid change
end)

-- Triggered when content is added to nearby grid
RegisterNetEvent('maku_grid:addContent', function(gridId, contentType, content)
    -- Handle content addition
end)

-- Triggered when content is removed from nearby grid
RegisterNetEvent('maku_grid:removeContent', function(gridId, contentType, contentId)
    -- Handle content removal
end)

-- Add content to a grid
TriggerServerEvent('maku_grid:addContent', gridId, contentType, content)

-- Remove content from a grid
TriggerServerEvent('maku_grid:removeContent', gridId, contentType, contentId)

Exports

-- Server exports
exports.maku_grid:addContentToGrid(gridId, contentType, content)
exports.maku_grid:removeContentFromGrid(gridId, contentType, contentId)
exports.maku_grid:getGridContent(gridId, contentType)
exports.maku_grid:getPlayersInGrid(gridId)

-- Shared exports
exports.maku_grid:registerContentType(contentType, policies)
exports.maku_grid:setContentLoadLogic(contentType, logic)
exports.maku_grid:setContentUnloadLogic(contentType, logic)
exports.maku_grid:getRegisteredContent(contentType)

Utility Functions

-- Get local internal client data for specific content
local data = exports.maku_grid:getLocalInternalData(gridId, contentType, contentId)

-- Get current player grid (server)
local gridId = exports.maku_grid:getPlayerGrid(playerId)

๐Ÿ› Debug Features

Visual Debug Mode

Set Config.Debug = true to enable:

  • Visual grid boundaries drawn in the world
  • Current grid highlighting in green
  • Debug GUI showing grid information

Debug Commands

  • Z key - Toggle itIsMaku/five-imgui focus mode for GUI interaction
  • When debug is enabled, a GUI window shows current grid ID and data

Debug GUI

The debug interface displays:

  • Current grid ID
  • Grid data in JSON format
  • Real-time updates as you move between grids

๐Ÿ—„๏ธ Database Structure

The system creates a grid table with the following structure:

CREATE TABLE `grid` (
    `grid_id` VARCHAR(50) NOT NULL,
    `content_id` INT NOT NULL AUTO_INCREMENT,
    `content_type` VARCHAR(50) NOT NULL,
    `content` LONGTEXT NOT NULL,
    PRIMARY KEY (`content_id`)
);

๐ŸŽฎ five-imgui Integration

The system includes itIsMaku/five-imgui for debug interfaces:

  • Built-in GUI components (text, buttons, inputs, etc.)
  • Window management
  • Real-time updates
  • Mouse and keyboard interaction

๐Ÿ”„ How It Works

  1. Player Tracking: Server continuously monitors player positions
  2. Grid Assignment: Players are assigned to grids based on their coordinates
  3. Content Loading: When entering a new grid, surrounding content is loaded
  4. Content Unloading: When leaving a grid, content is unloaded for player
  5. Persistence: Content marked for storage is saved to/loaded from database
  6. Synchronization: Client and server stay synchronized through events

๐Ÿ—๏ธ Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Client Side   โ”‚    โ”‚   Server Side   โ”‚    โ”‚    Database     โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค    โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค    โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ โ€ข Grid Logic    โ”‚โ—„โ”€โ”€โ–บโ”‚ โ€ข Player Track  โ”‚โ—„โ”€โ”€โ–บโ”‚ โ€ข Content Store โ”‚
โ”‚ โ€ข Content Load  โ”‚    โ”‚ โ€ข Grid Manage   โ”‚    โ”‚ โ€ข Persistence   โ”‚
โ”‚ โ€ข ImGUI Debug   โ”‚    โ”‚ โ€ข Content Sync  โ”‚    โ”‚                 โ”‚
โ”‚                 โ”‚    โ”‚ โ€ข Database I/O  โ”‚    โ”‚                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“„ License

This project is licensed under the Creative Commons Attribution-NonCommercial 4.0 International License (CC BY-NC 4.0).

You are free to:

  • Use, copy, modify, and distribute this project and derivative works

Under the following conditions:

  1. Attribution: Credit the original author (Adam Volkman) and repository
  2. Public Availability: Modified versions must remain publicly accessible
  3. License Notice: Include this license in copies

For more details, see the LICENSE file.

๐Ÿ‘ค Author

Adam Volkman (itismaku)

  • Discord: itismaku (maku#5434)
  • GitHub: itIsMaku

About

๐ŸŸฆ utility for handling data & entities across the GTA 5 map based on a squared grid

Topics

Resources

License

Stars

Watchers

Forks