Skip to content
Kiminaze edited this page Jun 9, 2021 · 6 revisions

Working with Sprite in the ContextMenu

Most Items can have a Sprite attached to them. The tick box of the CheckboxItem also counts as a Sprite. You can also use this class to draw your own sprites / images to the screen.

Creating a new Sprite

To create a new sprite you can use the Sprite(dict, name, position, size, heading, color) function. This will result in a new sprite being created.

local mySprite = Sprite("commonmenu", "shop_box_tick", vector2(0.5, 0.5), vector2(0.02, 0.02), 0, Colors.White)

-- only the dictionary and name have to be set when creating
local mySprite = Sprite("commonmenu", "shop_box_tick")

Sprite variables

A sprite provides 6 distinct variables.

local mySprite = Sprite("commonmenu", "shop_box_tick")

-- access or change the texture dictionary
mySprite.dict = "commonmenu"
-- access or change the texture name
mySprite.name = "shop_box_tick"

-- change the position on the screen
-- vector2(0.0, 0.0) is left top corner of the safezone
-- vector2(1.0, 1.0) is right bottom corner of the safezone
mySprite.position = vector2(0.5, 0.5)

-- change the heading (rotation) of the sprite
mySprite.heading = 90.0

-- change the size of the sprite
mySprite.size = vector2(1.0, 1.0)

-- change the color of the sprite
mySprite.color = Colors.Green

Drawing the Sprite on the screen

Drawing sprites must be done every frame and can be achieved using the Draw() function of the Sprite. Example:

Citizen.CreateThread(function()
    local mySprite = Sprite("commonmenu", "shop_box_tick")
    
    while (true) do
        Citizen.Wait(0)
        
        mySprite:Draw()
    end
end)

Displaying custom sprites / images / textures using a TextureDictionary

You can simply create a new texture dictionary by using the program OpenIV.

  1. Open OpenIV.

  2. Switch to "Edit Mode" top right corner.

  3. Click "New"

  4. (optional) Create a new folder.

  5. Create a Texture Dictionary (.ytd).

  6. Open the newly created Texture Dictionary.

  7. Click "Import" in the top left corner.

  8. You can now select most image formats (like png, jpg, etc) to import.

  9. (optional) Import more images.

  10. Press "Save" in the bottom right.

  11. Create a "stream" folder in your resource.

  12. Put the newly created texture dictionary in the "stream" folder.

  13. To use them ingame, just follow the instructions in Drawing the Sprite on the screen and use the filename of the dictionary as "dict" and the name of the added images as "name".

local customSprite = Sprite("newTextDict", "LogoTebex")

Clone this wiki locally