News: Welcome back to Bullworth! If you haven't already, you will need to reset your password..


Author Topic: On Screen Typing [LUA]  (Read 1616 times)

0 Members and 1 Guest are viewing this topic.

Offline ilovekittens

  • Jr. Member
  • **
  • Posts: 16
  • Gender: Female
    • View Profile
    • youtube
On Screen Typing [LUA]
« on: December 23, 2021, 02:47:55 AM »
This is my first modding related post in many years, but I'm very excited to show you guys this on-screen-typing system (or whatever you wanna call it)

It's not as good as being able to just type with your keyboard, but it's probably the best way to go about this completely in LUA script modding without creating any scripthooks or anything fancy like that.

Video demo:
https://youtu.be/9k7CZZMrZ14

Source:
Code: [Select]
--[[
--control scheme--
dpad up/down: scroll through letters
left/right weapon wheel: change case
dpad left: backspace
dpad right: next character
grapple: insert an underscore (_)
sprint: confirm text
]]

function main()
Wait(1000)
while true do
if IsButtonBeingPressed(3, 0) and not gTyping then
typing_typedStuff = F_OnScreenTyping("text: ") -- no args are necessary here, but you can optionally put a string of text here to display before the typing string
TextPrintString("typing finished", 2, 1)
elseif IsButtonBeingPressed(1, 0) and not gTyping and typing_typedStuff ~= nil then
TextPrintString("You typed: "..typing_typedStuff, 4, 1)
end
Wait(0)
end
end

typing_chars = {
{"a", "A"},
{"b", "B"},
{"c", "C"},
{"d", "D"},
{"e", "E"},
{"f", "F"},
{"g", "G"},
{"h", "H"},
{"i", "I"},
{"j", "J"},
{"k", "K"},
{"l", "L"},
{"m", "M"},
{"n", "N"},
{"o", "O"},
{"p", "P"},
{"q", "Q"},
{"r", "R"},
{"s", "S"},
{"t", "T"},
{"u", "U"},
{"v", "V"},
{"w", "W"},
{"r", "R"},
{"x", "X"},
{"y", "Y"},
{"z", "Z"},
{"1", "1"},
{"2", "2"},
{"3", "3"},
{"4", "4"},
{"5", "5"},
{"6", "6"},
{"7", "7"},
{"8", "8"},
{"9", "9"},
{"0", "0"},
}
typing_chars.n = table.getn(typing_chars)
gTyping = false
function F_OnScreenTyping(txt)
gTyping = true
typing_case = 1
typing_typed = {}
typing_typed.n = table.getn(typing_typed)
typing_index = 1
table.insert(typing_typed, typing_chars[typing_index][typing_case])
typing_str = typing_typed[1]
PlayerWeaponHudLock(true) -- Optional, but I recommend this because the weapon wheel buttons will be used to change the letter case
repeat
F_PreventDPadUpFromZoomingIn()

-- Change case:
if IsButtonBeingPressed(11, 0) then
typing_case = 1
table.remove(typing_typed, typing_typed.n)
table.insert(typing_typed, typing_chars[typing_index][typing_case])
typing_conc()
elseif IsButtonBeingPressed(13, 0) then
typing_case = 2
table.remove(typing_typed, typing_typed.n)
table.insert(typing_typed, typing_chars[typing_index][typing_case])
typing_conc()
end

-- Select character:
if IsButtonBeingPressed(1, 0) then
table.insert(typing_typed, typing_chars[typing_index][typing_case])
typing_conc()
elseif IsButtonBeingPressed(0, 0) and typing_typed.n > 1 then
table.remove(typing_typed, typing_typed.n)
typing_conc()
end

-- Change character:
if F_IsButtonBeingPressed(2, 0) then
typing_index = typing_index - 1
if typing_index < 1 then
typing_index = typing_chars.n
end
table.remove(typing_typed, typing_typed.n)
table.insert(typing_typed, typing_chars[typing_index][typing_case])
typing_conc()
elseif F_IsButtonBeingPressed(3, 0) then
typing_index = typing_index + 1
if typing_index > typing_chars.n then
typing_index = 1
end
table.remove(typing_typed, typing_typed.n)
table.insert(typing_typed, typing_chars[typing_index][typing_case])
typing_conc()
end

-- Insert an underscore:
if IsButtonBeingPressed(9, 0) then
table.insert(typing_typed, "_")
table.insert(typing_typed, typing_chars[typing_index][typing_case])
typing_conc()
end

-- Display text:
TextPrintString((txt and txt or "")..typing_str.."<", 1, 1)
Wait(0)
until IsButtonBeingPressed(7, 0)
gTyping = false
PlayerWeaponHudLock(false)
return typing_str
end

-- Concatenate strings from table:
function typing_conc()
typing_str = nil
for i = 1, typing_typed.n do
if typing_str == nil then
typing_str = typing_typed[1]
else
typing_str = typing_str..typing_typed[i]
end
end
end

function F_PreventDPadUpFromZoomingIn()
if IsButtonBeingReleased(2, 0) then
CameraAllowChange(false)
Wait(0)
CameraAllowChange(true)
end
end

gButtons = {}
function F_IsButtonBeingPressed(b, c)
if IsButtonBeingPressed(b, c) then
gButtons[b] = GetTimer()
return true
elseif IsButtonPressed(b, c) and gButtons[b] ~= nil then
return GetTimer() - gButtons[b] > 500
else
gButtons[b] = nil
return false
end
end

I commented just about everything I felt needed explaining. This example script isn't really intended to be run alone, but my intention of this post for you to be able to use the functions in it in your own script mods if you know what you're doing.
When you call the F_OnScreenTyping function, you should set a variable to be equal to it like I did in the script.
Code: [Select]
typedStuff = F_OnScreenTyping()After this line, typedStuff will hold a string of whatever you typed.

You can add more characters to the "typing_chars" table, as many as you want. I didn't include any characters other than numbers and letters because I originally scripted this system to type out file names to save bike races and spawned objects to in my trainer.

If you have any questions, leave a reply.  :cool:
« Last Edit: December 23, 2021, 03:15:44 AM by ilovekittens »

Offline Razc01na

  • Lord Slayer
  • Full Member
  • ***
  • Posts: 156
  • Gender: Male
  • Doom is life
    • View Profile
Re: On Screen Typing [LUA]
« Reply #1 on: December 23, 2021, 05:53:05 AM »
Awesome work  ;D