Prompt
Answer
Lua Script for Partner Selection and Animation Interaction in SAMP
This Lua script demonstrates how to create a partner selection menu with images and trigger an animation in San Andreas Multiplayer (SAMP) using MoonLoader. The script will ensure you can interact with a selected partner and perform certain animations.
Necessary Libraries
Make sure you have the following libraries:
- SAMPFunctions
- MoonLoader
These libraries are essential for creating menus and handling animations.
Overall Structure
- Initialize libraries and variables
- Create a partner selection menu
- Handle menu interactions
- Trigger animations
Code Implementation
Define Initial Setup and Imports
-- Import essential libraries
require 'sampfuncs'
require 'moonloader'
-- Initialize partner images and animations
local partners = {
{ name = "Partner1", image = "path/to/image1.png", animation = {lib = "ANIM_LIB1", name = "ANIM1"} },
{ name = "Partner2", image = "path/to/image2.png", animation = {lib = "ANIM_LIB2", name = "ANIM2"} },
-- Add more partners as needed
}
-- Create a variable to store the selected partner
local selectedPartner = nil
-- Function to initialize the plugin
function main()
if not isSampAvailable() then return end
while not isSampAvailable() do wait(100) end
sampAddChatMessage("Partner selection script loaded. Press 'P' to open the menu.", 0xFFFFFF)
-- Main loop to handle user input
while true do
if isKeyJustPressed(VK_P) then
openPartnerMenu()
end
wait(0)
end
end
Create Partner Selection Menu
-- Function to initialize and display partner selection menu
function openPartnerMenu()
local menu = sampCreateMenu("Select Partner", 0, 0, 150)
for i, partner in ipairs(partners) do
sampAddMenuItem(menu, partner.name)
end
-- Event handler for menu selection
sampSetMenuSelectCallback(menu, function(menu, item, model)
selectedPartner = partners[item + 1]
sampDestroyMenu(menu)
sampAddChatMessage("Selected: " .. selectedPartner.name, 0x00FF00)
-- Open partner image menu here
openPartnerImageMenu()
end)
sampShowMenu(menu)
end
-- Function to show partner image
function openPartnerImageMenu()
local menu = sampCreateMenu("View Partner", 0, 0, 150)
sampAddMenuItem(menu, "Close")
-- Event handler for closing the image
sampSetMenuSelectCallback(menu, function(menu, item, model)
sampDestroyMenu(menu)
end)
-- Function to draw the image on screen
function onDrawImage()
local screenX, screenY = getScreenResolution()
renderDrawBox(screenX / 2 - 128, screenY / 2 - 128, 256, 256, 0xFFFFFFFF)
renderDrawTexture(selectedPartner.image, screenX / 2 - 128, screenY / 2 - 128, 256, 256)
end
addEventHandler("onScriptRender", onDrawImage)
sampShowMenu(menu)
end
Trigger Animation
-- Function to start animation with selected partner
function triggerAnimation()
if selectedPartner then
local animLib = selectedPartner.animation.lib
local animName = selectedPartner.animation.name
requestAnimation(animLib)
while not hasAnimationLoaded(animLib) do wait(0) end
startAnimation(animLib, animName, -1, false, false, false, false)
sampAddChatMessage("Animation started with " .. selectedPartner.name, 0x00FF00)
removeAnimation(animLib)
else
sampAddChatMessage("No partner selected.", 0xFF0000)
end
end
Example Usage
-- Main Loop Example
function main()
if not isSampAvailable() then return end
while not isSampAvailable() do wait(100) end
sampAddChatMessage("Partner selection script loaded. Press 'P' to open the menu. Press 'O' to start animation.", 0xFFFFFF)
while true do
if isKeyJustPressed(VK_P) then
openPartnerMenu()
end
if isKeyJustPressed(VK_O) then
triggerAnimation()
end
wait(0)
end
end
Key Points
- Initialization: Import necessary libraries and set up initial variables.
- Menu Creation: Open menus to select a partner and view their image.
- Animation Handling: Trigger animations with the selected partner.
- Usage Example: Demonstrate how to use the script in the game environment with key presses.
This outline ensures that the script operates effectively within the specified constraints while demonstrating best practices in Lua programming. For further learning and advanced techniques, consider exploring resources on the Enterprise DNA platform.
Description
This Lua script for San Andreas Multiplayer (SAMP) enables players to select a partner from a customizable menu, view their images, and trigger unique animations. It utilizes MoonLoader and SAMPFunctions for an interactive gaming experience.