Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 06/05/23 in all areas

  1. Hello dear MTA players,My name is Eren(Shady) ,today I will show you String and Strings types in a simple and short way with examples, this string formula, which I needed very much at the time, is very useful for me today, so I decided to use it and make this tutorial to learn more, I hope I was able to explain it well. Strings and String Methods In Lua, a string is a sequence of characters that represents text data. Strings are used extensively in programming for storing and manipulating text-based information. In this tutorial, we will explore the different string types in Lua and how to manipulate them. Single Quoted Strings A single-quoted string is created by enclosing a sequence of characters between two single quotes. For example: local str = 'Hello, world!' Single-quoted strings are useful for creating short, simple strings that do not contain any special characters. If you need to include a single quote character within a single-quoted string, you can escape it using a backslash: local str = 'It\'s a beautiful day!' Double Quoted Strings A double-quoted string is created by enclosing a sequence of characters between two double quotes. For example: local str = "Hello, world!" Double-quoted strings are useful for creating more complex strings that may contain special characters such as escape sequences or variables. If you need to include a double quote character within a double-quoted string, you can escape it using a backslash: local str = "She said, \"Hello!\"" Long Strings A long string is created by enclosing a sequence of characters between two square brackets. For example: local str = [[This is a long string that spans multiple lines]] Long strings are useful for creating strings that contain multiple lines of text, such as paragraphs or blocks of code. They can also be used to create strings that contain special characters without the need for escape sequences. String Concatenation You can concatenate two or more strings together using the .. operator. For example: local str1 = "Hello" local str2 = "world" local str3 = str1 .. ", " .. str2 .. "!" In this example, str3 would contain the string "Hello, world!". String Manipulation Lua provides a number of built-in functions for manipulating strings. Some of the most commonly used string functions include: string.sub(str, start, end): Returns a substring of str starting at start and ending at end. string.len(str): Returns the length of str. string.lower(str): Returns str with all letters in lowercase. string.upper(str): Returns str with all letters in uppercase. string.rep(str, n): Returns str repeated n times. Pattern Matching Lua also provides a powerful pattern matching library that can be used to search and manipulate strings based on a pattern. The string.gsub() function is particularly useful for replacing parts of a string that match a pattern with another string. Here is an example of using string.gsub() to replace all occurrences of the word "dog" with the word "cat" in a string: local str = "I have a dog, but my neighbor has two dogs." local newStr = string.gsub(str, "dog", "cat") In this example, newStr would contain the string "I have a cat, but my neighbor has two cats.". The string we are working with is "My name is John_Smith". We want to swap the order of the two names in the string, so that it becomes "My name is Smith_John". str = "My name is John_Smith" new_str = string.gsub(str, "(John)_(Smith)", "%2_%1") Here's how the code achieves this: string.gsub() is called with the original string str, and two patterns to match: (John) and (Smith). The parenthesis around "John" and "Smith" indicates that they are capture groups, which means that any match to these patterns will be stored as a variable. The replacement string "%2_%1" is passed as the third argument to gsub(). This string contains the % modifier, which is used to reference the captured groups. In this case, %2 refers to the second captured group ("Smith"), and %1 refers to the first captured group ("John"). gsub() performs the substitution on the original string, replacing the matched pattern (John)_(Smith) with the replacement string "%2_%1". The resulting string, "My name is Smith_John", is stored in the new_str variable. So essentially, this code swaps the order of two captured groups in a string using the gsub() function and the % modifier. But how to make it dynamic and so it matches even if it's another First and Lastname (like Bruce_Willis) ? Well, we can use what we call character classes. They are special "codes" to match a specific type of thing in your string. For example, %a means "any letter", %d means "any digit". Here is the full list those character classes: . all characters %a letters %c control characters %d digits %l lower case letters %p punctuation characters %s space characters %u upper case letters %w alphanumeric characters %x hexadecimal digits %z the character with representation 0 So in our previous example we can replace the pattern like so: str1 = "My name is John_Smith" str2 = "My name is Bruce_Willis" new_str1 = string.gsub(str1, "(%a+)_(%a+)", "%2_%1") new_str2 = string.gsub(str2, "(%a+)_(%a+)", "%2_%1") new_str will contain "My name is Smith_John" and new_str2 will contain "My name is Willis_Bruce". So it works as long as it finds one or more letters followed by an underscore followed by one or more letters. Here I also used a modifier (+) to tell the matcher he has to look for 1 or more repetition of a letter. Here are the list of the possible modifiers: + 1 or more repetitions * 0 or more repetitions - also 0 or more repetitions ? optional (0 or 1 occurrence) More infos here In Lua, "\n" is known as a newline character. It is used to represent a new line of text in a string. When a newline character is encountered in a string, it tells the computer to move the cursor to the beginning of the next line. For example : local myString = "Hello\nworld" print(myString) In this example, the string "Hello" is followed by "\n" which tells Lua to add a new line. The string "world" is then printed on the next line. Output : Hello world You can also use multiple "\n" characters in a row to add multiple new lines: local myString = "This is the first line.\n\nThis is the third line." print(myString) In this example, two "\n" characters are used to add two new lines between the first and third lines. Output : This is the first line. This is the third line. You can use newline characters to format your text in a more readable way. For example, if you want to print a list of items, you can use a newline character to separate each item: local myString = "List of items:\n- Item 1\n- Item 2\n- Item 3" print(myString) In this example, the string "List of items:" is followed by "\n" to add a new line, then each item is listed on a new line with a hyphen (-) to indicate a bullet point. Output : List of items: - Item 1 - Item 2 - Item 3 In summary, "\n" is a newline character in Lua that is used to add a new line in a string. It is a useful tool for formatting text and making it more readable. string.sub(str, start, end): This function returns a substring of the input string, str, starting at the start index and ending at the end index. For example: local str = "Hello, World!" local subStr = string.sub(str, 7, 12) -- This will return "World!" string.len(str): This function returns the length of the input string, str. For example: local str = "Hello, World!" local len = string.len(str) -- This will return 13 string.lower(str): This function returns a new string with all letters in the input string, str, converted to lowercase. For example: local str = "HeLLo, WoRLd!" local lowerStr = string.lower(str) -- This will return "hello, world!" string.upper(str): This function returns a new string with all letters in the input string, str, converted to uppercase. For example: local str = "HeLLo, WoRLd!" local upperStr = string.upper(str) -- This will return "HELLO, WORLD!" string.rep(str, n): This function returns a new string that is n copies of the input string, str. For example: local str = "Hello" local repStr = string.rep(str, 3) -- This will return "HelloHelloHello" string.format string.format is a Lua function that is used to format strings based on a pattern. The basic syntax of string.format is: string.format(formatstring, ...) Here, formatstring is a string that contains placeholders for values that you want to include in the formatted string. The placeholders are indicated by % symbols followed by a letter that indicates the type of value to be included. The ... is a variable argument list that contains the values to be formatted. Here are some of the most commonly used placeholders in string.format: %s: Inserts a string value %d or %i: Inserts an integer value %f: Inserts a floating-point value %%: Inserts a literal % symbol Let's see some example codes that use string.format: -- Example 1: Formatting a simple string local name = "John" local age = 25 local formattedString = string.format("My name is %s and I am %d years old.", name, age) print(formattedString) -- Output: My name is John and I am 25 years old. -- Example 2: Formatting a floating-point value with a specific precision local pi = math.pi local formattedString = string.format("The value of pi is approximately %.2f.", pi) print(formattedString) -- Output: The value of pi is approximately 3.14. -- Example 3: Formatting an integer value with leading zeros local number = 42 local formattedString = string.format("The answer is %04d.", number) print(formattedString) -- Output: The answer is 0042. In Example 1, we format a simple string with placeholders for name and age. In Example 2, we format a floating-point value for pi with a precision of two decimal places. In Example 3, we format an integer value with leading zeros to make it four digits long. Hope this helps!
    4 points
  2. You have the knowledge for these things, im tellin' u. Good job!
    1 point
  3. Please, if it worked, you can give a like to my comment.
    1 point
  4. Please, if it worked, you can give a like to my comment.
    1 point
  5. function onMarkerHit(hitElement, matchingDimension) -- Check if the element that hit the marker is a player and is in the same dimension as the marker if isElement(hitElement) and getElementType(hitElement) == "player" and matchingDimension then -- Give the player a weapon giveWeapon(hitElement, 31, 500) -- give the player an HK 417 with 500 bullets -- Output a message to the player outputChatBox("You picked up an M4 with 500 bullets.", hitElement, 0, 255, 0) end end -- Create a marker at position (x, y, z) with a size of 2 units local x, y, z = 123.45, 67.89, 10.11 -- change these values to your desired position local marker = createMarker(x, y, z, "cylinder", 2, 0, 255, 0, 150) -- Add the event handler to the marker addEventHandler("onMarkerHit", marker, onMarkerHit)
    1 point
  6. function onTeleportCommand() -- get the player's current position local x, y, z = getElementPosition(getLocalPlayer()) -- set the new position where you want to teleport the player local new_x, new_y, new_z = 123.45, 67.89, 10.11 -- these values should be changed to your desired position -- set the player's new position setElementPosition(getLocalPlayer(), new_x, new_y, new_z) end addCommandHandler("teleport", onTeleportCommand)
    1 point
  7. local marker function markerc(command, ...) local weaponIDs = {...} if #weaponIDs < 1 then return outputChatBox("You must provide at least one weapon ID.", source, 255, 0, 0) end local weapons = {} for _, weaponID in ipairs(weaponIDs) do local id = tonumber(weaponID) if not id then return outputChatBox("Invalid weapon ID: " .. tostring(weaponID), source, 255, 0, 0) end table.insert(weapons, {id = id, ammo = 100}) end if marker then destroyElement(marker) end local x, y, z = getElementPosition(source) marker = createMarker(x, y, z + 1, "cylinder", 1.5, 0, 255, 0, 127) addEventHandler("onMarkerHit", marker, function(hitElement, matchingDimension) if getElementType(hitElement) == "player" and matchingDimension then for _, weapon in ipairs(weapons) do giveWeapon(hitElement, weapon.id, weapon.ammo, true) end end end) outputChatBox("Weapon marker created!", source, 0, 255, 0) end addCommandHandler("cw", markerc) use /cw (weapon ID) for example /cw 31 to create a marker that gives 100 bullets of m4 you also can use multiple weapons /cw 31 35 and it will create a marker that gives m4 and rocket launcher
    1 point
  8. createMarker onMarkerHit giveWeapon
    1 point
  9. hello @kukimuki welcome the forum , I sent you 2 different pieces of code regarding your question, you can try both, if you want a different way, you can tell me, I can help you 1. code function onPlayerCommand(command) if command == "teleport" then -- get the player's current position local x, y, z = getElementPosition(getLocalPlayer()) -- set the new position where you want to teleport the player local new_x, new_y, new_z = 123.45, 67.89, 10.11 -- change these values to your desired position -- set the player's new position setElementPosition(getLocalPlayer(), new_x, new_y, new_z) end end addEventHandler("onClientCommand", getRootElement(), onPlayerCommand) 2. code function onPlayerCommand(command, arg1, arg2, arg3) if command == "teleport" and arg1 and arg2 and arg3 then -- get the player's local element local player = getLocalPlayer() -- get the position to teleport the player to local x, y, z = tonumber(arg1), tonumber(arg2), tonumber(arg3) -- set the player's new position setElementPosition(player, x, y, z) end end addEventHandler("onClientCommand", getRootElement(), onPlayerCommand)
    1 point
  10. JConvertor Ever wanted to put an SA map into MTA but had issues with Scene2Res and found it impossible to do manually?; well here I present Convertor (For use with J Streamer) What does this do? - Converts the map (IPL and IDE files) - Only copies needed files (If it's part of the standard SA it'll only print the position and name into the JS Placement file - Converts water (If there are too many planes then you need to manually make the water - Converts train track placement //WIP will come soon - Can seamlessly load a map MTA so long as it's converted and made correctly - Manages vegetation swaying - Automatically fades objects that shouldn't be visible during night or day - Properly assigns LODs - Gives objects proper tags such as ALPHA, CULLING, etc Usage Drag the maps Data folder into the root of the convertor resource(This handles IPLs, IDEs, Water, Etc) Drag all DFF and TXD files into the resources folder Place Collisions into their own folder Open Colleditor2 http://www.steve-m.com/downloads/tools/colleditor2/ Drag and drop all of the collisions into COL editor Select all of the collisions (In COL editor) Right click - > Export - > Single Col files -> then export into the resources folder Change name or position in config file Run the Convertor as a resource and watch the magic happen Fix any errors in Debug.txt (If any) Run again (If needed) Run the map with OBJs running https://github.com/CodyJL/J-Map-Convertor J 3Ds Max Tool Kit What does this do? - Export JSP strings - Export JSD strings - Export Meta strings - Export COLs - Export DFFs (If you wanna use it figure it out yourself) https://github.com/CodyJL/JStreamer/tree/master/Tools JStreamer (OBJs) What does this do? - Load maps - Automatically assigns IDs to objects - Tries to allow you a full range of possibilities (Using SA content along side custom content seamlessly) - Allows you to create maps in a very tidy way. https://github.com/CodyJL/JStreamer JResources Discord
    1 point
×
×
  • Create New...