ContextActionService

Artık kullanılmayanları göster

*Bu içerik, yapay zekâ (beta) kullanılarak çevrildi ve hatalar içerebilir. Sayfayı İngilizce görüntülemek için buraya tıkla.

Oluşturulamaz
Hizmet

Bir deneyimin kullanıcı girişini belirli bir koşul veya süre altında etkinleştirilen işlevlere veya sadece bir süre için etkinleştirilen işlevlere bağlamasına izin verir.Örneğin, bir oyuncunun yalnızca yakında bir kapıyı açmasına izin vermek.kod, bir eylem sadece hizmet tarafından benzersiz eylemler arasında ayırt etmek için kullanılan bir dize (aksiyonadı) dır.Eylem dizesi diğer üye işlevleri arasında BindAction ve UnbindAction sağlanır, diğer üye işlevleri arasında.Eğer iki eylem aynı girişe bağlıysa, en yakın zamanda bağlanan öncelikli olacaktır.En yeni eylem bağlantıdan koparıldığında, bunun önce bağlı olanı yeniden kontrol alır.Bu hizmet kullanıcı girişiyle uğraştığından, sadece istemci tarafında kullanabilirsiniz LocalScripts .

Konteks ve Eylem

Bir konteks basitçe bir oyuncunun bir aksiyongerçekleştirebileceği bir koşuldur.Bazı örnekler araba içinde bir tutmak, bir kapının yanında durmak veya bir kapının yanında durmak gibi şeyleri içerir.Durum ne olursa olsun, kontekst girildiğinde çağrılması ve kontekst bırakıldığında sana kalmıştır.

Bir eylem basitçe o sırada oyuncu tarafından yapılabilecek bir giriştir.Böyle bir eylem bazı menüleri açabilir/kapatabilir, ikincil bir araç eylemini tetikleyebilir veya RemoteFunction:InvokeServer() kullanarak sunucuya bir istek gönderebilir.Bir eylem, her ikisinin de ilk parametresi olarak benzersiz bir dize tarafından tanımlanır BindAction ve UnbindAction .Dize her şey olabilir, ancak yapılan eylemi yansıtmalı, kullanılan giriş değil .Örneğin, "KeyH" adını bir eylem adı olarak kullanmayın - bunun yerine "CarHorn" kullanın.Eylemlerinizi kodunuzun en üstünde sabit olarak tanımlamanız en iyisidir, çünkü kodunuzda en az üç farklı yerde kullanacaksınız.

Eylemleri Bağlantılı Olarak Süreçsel Olarak

Çoğu durum için ContextActionService'in BindAction yerine UserInputService.InputBegan kullanmak daha iyidir.For UserInputService.InputBegan için, bağlı işleviniz oyuncunun yapılan eylemin konteynında olup olmadığını kontrol etmesi gerekecektir.Çoğu durumda, bir işlev çağrısı yapmaktan daha zordur, bir bağlantı noktası girildiğinde/ bırakıldığında.Örneğin, H anahtarının oyuncunun üzerinde otururken bir araba klakson sesi tetiklemesini istiyorsanız, oyuncu sohbette "merhaba" yazabilir veya başka bir şey için H anahtarını kullanabilir.Başka bir şeyin H anahtarını kullandığını belirlemek daha zordur (mesajlaşma gibi) - araba oyuncunun istemediğinde çalabilir.Oyuncu arabaya girdiğinde/gittiğinde BindAction ve UnbindAction yerine kullanırsanız, ContextActionService anahtarların sadece en yakın zamanda bağlandığı eylemi tetiklediğinden emin olacaktır, çünkü H anahtarları sadece en yakın zamanda bağlandığı eylemi tetikler.Başka bir şey (örneğin sohbet) kontrolü alırsa, bunu kontrol etmek için endişelenmenize gerek kalmayacaktır.

Sınırlı Eylemleri İnceleme

Eylemlerin ve bağlı girişlerin bir listesini görmek için, Geliştirici Konsolundaki "Eylem Bağlantıları" sekmesini kontrol edebilirsiniz (oyun sırasında F9).Bu, Roblox çekirdek kodları ve varsayılan kamera/kontrol kodları tarafından bağlanan tüm bağlantıları gösterir.Bu, eylemleriniz doğru sürelerde bağlanıp bağlanmadığını kontrol etmek veya başka bir eylemin eylemlerinizden giriş çalması durumunda yararlıdır.Örneğin, WASD bağlamaya çalışıyorsanız, varsayılan karakter hareketi senaryolarının aynı anahtarlara bağlandığı durum olabilir.Benzer şekilde, kamera kontrol kodu seninkinden sonra çalışırsa sağ tıklama girişini çalabilir.

Klavyesiz Giriş

Bu hizmet özellikle oyun kolu ve dokunma girişini desteklemek için yararlıdır.Oyun kolu girişi için, B düğmesini başka bir menüye girdiğinde kullanıcıyı önceki menüye geri döndüren bir eyleme bağlamayı seçebilirsiniz.Dokunma için, anahtar basımlarının yerine ekran üzeri dokunma düğmeleri kullanılabilir: bu düğmeler sadece eylem bağlı olduğunda görüntülenir ve bu düğmelerin konumu, metni ve/veya görüntüleri bu hizmet aracılığıyla yapılandırılabilir.Bu hizmet tarafından sağlanan özelleştirmenin miktarında biraz sınırlılar; genellikle kendi ekran düğmelerinizi ImageButton veya TextButton kullanarak kendiniz yapmak daha iyi bir fikirdir.

Kod Örnekleri

This example properly shows how to use ContextActionService in binding user input to a contextual action. The context is the tool being equipped; the action is reloading some weapon. Test this code sample by placing it in a LocalScript parented to a Tool. When the Tool is equipped, a "Reload" action is bound, and when the Tool is unequipped the "Reload" action is unbound. When the player presses R with the Tool equipped, the message "Reloading!" will appear.

ContextActionService Tool Reload

local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Reload"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Reloading!")
end
end
tool.Equipped:Connect(function()
ContextActionService:BindAction(ACTION_RELOAD, handleAction, true, Enum.KeyCode.R)
end)
tool.Unequipped:Connect(function()
ContextActionService:UnbindAction(ACTION_RELOAD)
end)

Özet

Yöntemler

Etkinlikler

Özellikler

Yöntemler

BindAction

()

Bir eylemi bir eylem işleme işlevine verilen kullanıcı girişine bağlayın.Eşleşen bir giriş gerçekleştirildikten sonra, aksiyon işlemci işlevi takip edilenargümanlarla çağrılacaktır.Geçerli giriş sayısal değerleri aşağıdakiler arasındakileri içerir: Enum.KeyCode , Enum.UserInputType veya Enum.PlayerActions .Bir oyuncu bir eylemin yapılabileceği konuya girerken bu işlevi çağırın, bir eylem gerçekleştirilebilir.Oyuncu bu kontextten ayrıldığında, aynı UnbindAction ile çağrı yapın actionName .Eylemin eylem işleme işlevini CallFunction kullanarak manuel olarak çağırabilirsiniz.

Aşağıdaki kod örneği, bir anahtar ( ), oyun kolu düğmesi veya dokunma ekranı düğmesi basıldığında bir nasıl olabileceğini gösterir.


local ContextActionService = game:GetService("ContextActionService")
-- Bir araba boynuz sesi
local honkSound = Instance.new("Sound", workspace)
honkSound.Looped = true
honkSound.SoundId = "rbxassetid://9120386436"
local function handleAction(actionName, inputState, inputObject)
if actionName == "HonkHorn" then
if inputState == Enum.UserInputState.Begin then
honkSound:Play()
else
honkSound:Pause()
end
end
end
-- Oyuncu araca oturduğunda:
ContextActionService:BindAction("HonkHorn", handleAction, true, Enum.KeyCode.H, Enum.KeyCode.ButtonY)
-- Oyuncu çıktığında:
ContextActionService:UnbindAction("HonkHorn")

Eylem İşleyici Parametleri

Eylem işlemcisi işlevleri aşağıdaki parametarlarla çağrılır:


<th>Tür</th>
<th>Açıklama</th>
</tr>
<tr>
<td>1</td>
<td><code>dizi</code></td>
<td>Başlangıçta BindAction'a geçen aynı dize†</td>
</tr>
<tr>
<td>2</td>
<td><code>Enum.UserInputState'ı</code></td>
<td>Girişin durumu (Başlat, Değiştir, Sonlandır veya İptal)*</td>
</tr>
<tr>
<td>3</td>
<td><code>Giriş Nesnesi</code></td>
<td>Giriş hakkında bilgileri içeren bir nesne (KullanıcıGirişTürüne bağlı olarak değişir)</td>
</tr>
#

† Bu, gerekirse bir işlevin aynı anda birden fazla eylemi ele almasına izin verir.Bazı girişler devam ediyorsa veya devam eden giriş üzerinde başka bir eylem bağlandıysa veya devam eden bağlı eylem unbound ise, iptal gönderilir.

Eylem Bağlantıları Yığını

Eylem bağları bir yığın gibi davranır: eğer iki eylem aynı kullanıcı girişine bağlıysa, en son bağlanan eylem işleyici kullanılır.Bir eylem işlemcisi Enum.ContextActionResult.Pass döndürürse, bir sonraki en yakın bağlı eylem işlemcisi çağrılır ve böylece bir işlemci girişi sarsana kadar (nil veya Enum.ContextActionResult.Sink geri döndürerek).UnbindAction çağrıldığında, eylem işlemcisi yığından kaldırılır.Bu yığın davranışı, BindActionAtPriority kullanarak değiştirilebilir, böylece createTouchButton sonra ek bir öncelik parametresi, eylemlerin bağlandığı sırayı (daha yüksek önce daha düşük) geçebilir.

Dokunma Düğmeleri

Giriş türlerine ek olarak, bu işlevin üçüncü parametresi bir düğmenin TouchEnabled cihazlar için oluşturulup oluşturulmadığını kontrol eder.İlk dokunuş düğmesinin yaratımardından, ScreenGui adlı "ContextActionGui" adlı bir düğme PlayerGui eklenir.Ekran arayüzünün içinde "KontextButtonFrame" adlı bir Frame eklendi.Bu çerçevede bağlı eylemler için ImageButtons bağlanır; özelleştirme için bu düğmeleri geri almak için GetButton kullanabilirsiniz.

Parametreler

actionName: string

Yapılan eylemi temsil eden bir dize (örneğin "HonkHorn" veya "OpenDoor").

Varsayılan değer: ""
functionToBind: function

Bağlı girişler tetiklendiğinde aşağıdaki parametarlarla çağrılan eylem işleme işlevi: dize (eylem adı), Enum.UserInputState ve bir InputObject.

Varsayılan değer: ""
createTouchButton: boolean

Dokunma giriş cihazlarındaki eylem için bir GUI düğmesi oluşturulmalı mı.

Varsayılan değer: ""
inputTypes: Tuple

aksiyonbağlanacak girişleri temsil eden herhangi bir sayı Enum.KeyCode veya Enum.UserInputType .

Varsayılan değer: ""

Dönüşler

()

Kod Örnekleri

This example properly shows how to use ContextActionService in binding user input to a contextual action. The context is the tool being equipped; the action is reloading some weapon. Test this code sample by placing it in a LocalScript parented to a Tool. When the Tool is equipped, a "Reload" action is bound, and when the Tool is unequipped the "Reload" action is unbound. When the player presses R with the Tool equipped, the message "Reloading!" will appear.

ContextActionService Tool Reload

local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Reload"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Reloading!")
end
end
tool.Equipped:Connect(function()
ContextActionService:BindAction(ACTION_RELOAD, handleAction, true, Enum.KeyCode.R)
end)
tool.Unequipped:Connect(function()
ContextActionService:UnbindAction(ACTION_RELOAD)
end)

This code sample uses ContextActionService to bind an action named "BoundAction" to a general action handler function on the F key. Place this in a LocalScript inside StarterPlayerScripts and press F to see the message "Handling action: BoundAction".

General Action Handler

local ContextActionService = game:GetService("ContextActionService")
local function handleAction(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("Handling action: " .. actionName)
print(inputObj.UserInputType)
end
-- Since this function does not return anything, this handler will
-- "sink" the input and no other action handlers will be called after
-- this one.
end
ContextActionService:BindAction("BoundAction", handleAction, false, Enum.KeyCode.F)

This code sample demonstrates how BindAction acts like a stack. It binds two actions, FirstAction (Z, X, and C keys) and SecondAction (Z and X keys) to two action handling functions. The second one will pass on a certain input (the X key).

Both actions use the Z and X keys, however the second handler will pass input only if X is pressed. So, when X is pressed, the second handler is called and then the first. The first action is also bound to the C key, and can be triggered even though the other two inputs are "covered" by the second action.

Test this code out by pasting it into a LocalScript within StarterPlayerScripts, then pressing Z, X and C. Observe which action handlers are called with what actions.

Stacked Action Handlers

local ContextActionService = game:GetService("ContextActionService")
-- Define an action handler for FirstAction
local function actionHandlerOne(actionName, inputState, _inputObj)
if inputState == Enum.UserInputState.Begin then
print("Action Handler One: " .. actionName)
end
-- This action handler returns nil, so it is assumed that
-- it properly handles the action.
end
-- Binding the action FirstAction (it's on the bottom of the stack)
ContextActionService:BindAction("FirstAction", actionHandlerOne, false, Enum.KeyCode.Z, Enum.KeyCode.X, Enum.KeyCode.C)
-- Define an action handler for SecondAction
local function actionHandlerTwo(actionName, inputState, inputObj)
if inputState == Enum.UserInputState.Begin then
print("Action Handler Two: " .. actionName)
end
if inputObj.KeyCode == Enum.KeyCode.X then
return Enum.ContextActionResult.Pass
else
-- Returning nil implicitly Sinks inputs
return Enum.ContextActionResult.Sink
end
end
-- Binding SecondAction over the first action (since it bound more recently, it is on the top of the stack)
-- Note that SecondAction uses the same keys as
ContextActionService:BindAction("SecondAction", actionHandlerTwo, false, Enum.KeyCode.Z, Enum.KeyCode.X)

BindActionAtPriority

()

BindActionAtPriority, BindAction gibi davranır, ancak bağlı aksiyonbir öncelik atanmasına da izin verir.Birden fazla eylem aynı girişe bağlıysa, eylemlerin bağlandığı sıraya bakılmaksızın daha yüksek öncelikli işlev çağrılır.Diğer bir deyişle, bu işlev BindAction'un normal "yerleştirme" davranışını geçersiz kılar.

Parametreler

actionName: string

Yapılan eylemi temsil eden bir dize (örneğin "HonkHorn" veya "OpenDoor").

Varsayılan değer: ""
functionToBind: function

Bağlı girişler tetiklendiğinde aşağıdaki parametarlarla çağrılan eylem işleme işlevi: dize (eylem adı), Enum.UserInputState ve bir InputObject.

Varsayılan değer: ""
createTouchButton: boolean

Dokunma giriş cihazlarındaki eylem için bir GUI düğmesi oluşturulmalı mı.

Varsayılan değer: ""
priorityLevel: number

Eylemin bağlanması gereken öncelik seviyesi (daha düşükten önce daha yüksek düşünülür).

Varsayılan değer: ""
inputTypes: Tuple

aksiyonbağlanacak girişleri temsil eden herhangi bir sayıda Enum.KeyCode veya Enum.UserInputType.

Varsayılan değer: ""

Dönüşler

()

Kod Örnekleri

This code sample demonstrates how ContextActionService:BindActionAtPriority() can be used to bind actions out of order yet still have the same priority levels. Normally, BindAction() would operate on order (last bound action has highest priority), but priority levels override this. You can test this code by pasting it into a Script with RunContext = Client in ReplicatedStorage.

ContextActionService BindAction Priorities

local ContextActionService = game:GetService("ContextActionService")
local INPUT_KEY1 = Enum.KeyCode.Q
local INPUT_KEY2 = Enum.KeyCode.E
local function handleThrow(actionName: string, inputState: Enum.UserInputState, inputObject: InputObject)
if inputState ~= Enum.UserInputState.Begin then
return Enum.ContextActionResult.Pass
end
print(`Action [{actionName}] occurred. KeyCode [{inputObject.KeyCode}] pressed.`)
return Enum.ContextActionResult.Sink
end
local function handlePunch(actionName: string, inputState: Enum.UserInputState, inputObject: InputObject)
if inputState ~= Enum.UserInputState.Begin then
return Enum.ContextActionResult.Pass
end
print(`Action [{actionName}] occurred. KeyCode [{inputObject.KeyCode}] pressed.`)
return Enum.ContextActionResult.Sink
end
-- Without specifying priority, the most recently bound action is called first,
-- so pressing INPUT_KEY1 prints "Punch" and then sinks the input.
ContextActionService:BindAction("DefaultThrow", handleThrow, false, INPUT_KEY1)
ContextActionService:BindAction("DefaultPunch", handlePunch, false, INPUT_KEY1)
-- Here we bind both functions in the same order as above, but with explicitly swapped priorities.
-- That is, we give "Throw" a higher priority of 2 so it will be called first,
-- despite "Punch" still being bound more recently.
-- Pressing INPUT_KEY2 prints "Throw" and then sinks the input.
ContextActionService:BindActionAtPriority("PriorityThrow", handleThrow, false, 2, INPUT_KEY2)
ContextActionService:BindActionAtPriority("PriorityPunch", handlePunch, false, 1, INPUT_KEY2)

BindActivate

()

Etkinleştirmek için bir Enum.KeyCode ile birlikte kullanılabilecek bir Enum.UserInputType , ClickDetector ve Tools olaylarını etkinleştirmek için bir GuiButtons kullanın.Verilen anahtar/düğme basıldığında, Mouse.Button1Down mouse'a gönderilen olayı ateşler Tool.Equipped .Bu da Tool.Activated etkinliğini ateş eder, eğer Tool.ManualActivationOnly false olarak ayarlanmazsa.Oyun kolu girişi için, bu işlev, ButtonR2 Enum.KeyCode 'ı bağlamak için varsayılan kontrol senaryoları tarafından çağrılır.

Belirtilen ın geçerli olması için veya olması gerektiğini unutmayın, böylece geçerli olur.

Parametreler

userInputTypeForActivation: Enum.UserInputType

Gamepad8 aracılığıyla Klavye veya Gamepad1 olmalıdır.

Varsayılan değer: ""
keyCodesForActivation: Tuple
Varsayılan değer: ""

Dönüşler

()

GetAllBoundActionInfo

GetAllBoundActioninfo, tüm eylemlerin adlarını (özgün olarak BindAction ) bir tabloya dönüştüren bir tabloyu döndürür, eylem adıyla çağrıldığında GetBoundActionInfo tarafından döndürülen tablo.Bu işlevi kullanarak, mevcut olarak bağlı tüm eylemleri inceleyebilirsiniz.Öncelik seviyelerini veya yığın emirlerini depurarken bu yararlıdır.


Dönüşler

GetBoundActionInfo

GetBoundActionInfo, adına verilen bağlı bir eylemi tanımlayan aşağıdaki anahtarlarla bir tablo döndürür.Tek seferde tüm eylemler için aynı bilgileri almak için GetAllBoundActionInfo kullanın.


<th>Tür</th>
<th>Açıklama</th>
</tr>
<tr>
<td><code>yığınmaSırası</code></td><td>sayı</td>
<td>
Yığındaki eylemin indeksini tanımlar (artış)
</td>
</tr>
<tr>
<td><code>öncelikSeviyesi</code> \*</td><td>sayı</td>
<td>
Eylemin <code>Class.ContextActionService:BindActionAtPriority()|priority</code> seviyesini tanımlar
</td>
</tr>
<tr>
<td><code>dokunma düğmesi oluştur</code></td><td>bool</td>
<td>
Bir dokunma düğmesinin <code>Class.UserInputService.TouchEnabled|TouchEnabled</code> cihazlarda oluşturulması gerektiğini tanımlar
</td>
</tr>
<tr>
<td><code>girişTürleri</code></td><td>tablo</td>
<td>
Bu eylem tetiklenecek <code>Class.ContextActionService:BindAction()|BindAction</code> için geçen giriş türleri
</td>
</tr>
<tr>
<td><code>tanım</code> †</td><td>dize</td>
<td>
Eylem tarafından ayarlanan aksiyonun açıklaması <code>Class.ContextActionService:SetDescription()|SetDescription</code>
</td>
</tr>
<tr>
<td><code>başlık</code> †</td><td>dize</td>
<td>
Eylem seti tarafından <code>Class.ContextActionService:SetTitle()|SetTitle</code> tarafından belirlenen eylemin başlığı
</td>
</tr>
<tr>
<td><code>görüntü</code> †</td><td>dize</td>
<td>
aksiyondokunma düğmesi tarafından ayarlanan görüntü <code>Class.ContextActionService:SetImage()|SetImage</code> tarafından
</td>
</tr>
Adı

* Öncelik seviyesi hala BindActionAtPriority kullanılmadıysa bile dahil edilecektir - varsayılan olarak 2000 olacaktır.

† Bu alanın, eşleşen yöntem verilen aksiyoniçin çağrılmadığında nil.

Parametreler

actionName: string
Varsayılan değer: ""

Dönüşler

GetCurrentLocalToolIcon

GetCurrentLocalToolIcon, şu anda BackpackItem.TextureId bir Tool tarafından equipped ile dönecek Player veya nil eğer böyle bir Araç yoksa veya oyuncu bir Character eksikse.


Dönüşler

Araç'ın TextureId'inden bir içerik dizesi veya bulunamıyorsa nil if one could not be found.

SetDescription

()

Açıklama ayarlanacak SetDescription, eylem tarafından bağlanan açıklamayı ayarlayacaktır BindAction .Mevcut eylemlerin bir listesinde, bu verilen aksiyontanımlayan metin olurdu.

Adın bu yöntemin onları oluşturan eylemler için dokunma düğmesini özelleştiren işlev ailesiyle ilgili olduğunu göstermesine rağmen, bu yöntem böyle bir düğmeyi etkilemez.Bu yöntem sadece bir aksiyonmetin açıklamasını ayarlar ve daha fazlası yoktur.

Parametreler

actionName: string

Eylemin orijinal adı BindAction'a geçti.

Varsayılan değer: ""
description: string

Eylemin bir metin açıklaması, örneğin "Arabanın boynuzunu çal" veya "envanteraç".

Varsayılan değer: ""

Dönüşler

()

Kod Örnekleri

This code sample demonstrates binding an "Inspect" action to a touch button created automatically by ContextActionService. The button is customized using SetImage(), SetTitle(), SetDescription() and SetPosition(). The button is further customized by using GetButton() to get a reference to the ImageButton itself and tinting it green by setting ImageButton.ImageColor3.

Paste this code into a LocalScript placed within StarterPlayerScripts to test it. In Studio, be sure to toggle the touch device emulator in order for the button to actually be created.

ContextActionService Touch Button

local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Image of a speech bubble with ? in it
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspecting")
end
end
-- For touch devices, a button is created on-screen automatically via the 3rd parameter
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- We can use these functions to customize the button:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Look")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspect something.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- We can manipulate the button directly using ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Remember: non-touch devices won't return anything!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tint the ImageButton green
end

SetImage

()

Bu yöntem, BindAction() tarafından oluşturulan bir dokunma düğmesinde gösterilen görüntüyü ayarlar.Özellikle, özelliğini içinde ayarlar , tarafından döndürülecek olan özellik.Eğer böyle bir bağlı eylem yoksa (örneğinGetButton tarafından hiçbir şey dönmez), bu işlev hiçbir şey yapmaz ve hata vermez.

Bu işlev, bir aksiyondokunma düğmesini özelleştiren bir ailenin parçasıdır. Ailenin diğer üyeleri SetPosition ve SetTitle dahil eder.

Parametreler

actionName: string

Eylemin orijinal adı BindAction'a geçti.

Varsayılan değer: ""
image: string

Resim özelliğinin ayarlanması gereken değer.

Varsayılan değer: ""

Dönüşler

()

Kod Örnekleri

This code sample demonstrates binding an "Inspect" action to a touch button created automatically by ContextActionService. The button is customized using SetImage(), SetTitle(), SetDescription() and SetPosition(). The button is further customized by using GetButton() to get a reference to the ImageButton itself and tinting it green by setting ImageButton.ImageColor3.

Paste this code into a LocalScript placed within StarterPlayerScripts to test it. In Studio, be sure to toggle the touch device emulator in order for the button to actually be created.

ContextActionService Touch Button

local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Image of a speech bubble with ? in it
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspecting")
end
end
-- For touch devices, a button is created on-screen automatically via the 3rd parameter
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- We can use these functions to customize the button:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Look")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspect something.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- We can manipulate the button directly using ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Remember: non-touch devices won't return anything!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tint the ImageButton green
end

SetPosition

()

Bu yöntem, BindAction() tarafından oluşturulan bir dokunma düğmesinin konumunu ayarlar.Özellikle, GuiObject.Position özelliğini ImageButton tarafından döndürülecek olan GetButton özelliğinin özelliklerini ayarlar.Eğer böyle bir bağlı eylem yoksa (örneğinGetButton tarafından hiçbir şey dönmez), bu işlev hiçbir şey yapmaz ve hata vermez.

Bu işlev, bir aksiyondokunma düğmesini özelleştiren bir ailenin parçasıdır. Ailenin diğer üyeleri SetImage ve SetTitle dahil eder.

Parametreler

actionName: string

Eylemin orijinal adı BindAction'a geçti.

Varsayılan değer: ""
position: UDim2

KontextButtonFrame içindeki konum.

Varsayılan değer: ""

Dönüşler

()

Kod Örnekleri

This code sample demonstrates binding an "Inspect" action to a touch button created automatically by ContextActionService. The button is customized using SetImage(), SetTitle(), SetDescription() and SetPosition(). The button is further customized by using GetButton() to get a reference to the ImageButton itself and tinting it green by setting ImageButton.ImageColor3.

Paste this code into a LocalScript placed within StarterPlayerScripts to test it. In Studio, be sure to toggle the touch device emulator in order for the button to actually be created.

ContextActionService Touch Button

local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Image of a speech bubble with ? in it
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspecting")
end
end
-- For touch devices, a button is created on-screen automatically via the 3rd parameter
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- We can use these functions to customize the button:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Look")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspect something.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- We can manipulate the button directly using ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Remember: non-touch devices won't return anything!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tint the ImageButton green
end

SetTitle

()

SetTitle, BindAction tarafından oluşturulan bir dokunma düğmesinde gösterilen metni ayarlayacaktır.Özellikle, bu, TextLabel.Text özelliğini bir TextLabel içinde ayarlar ki bu ImageButton tarafından döndürülecek GetButton .Eğer böyle bir bağlı eylem yoksa (örneğinGetButton tarafından hiçbir şey dönmez), bu işlev hiçbir şey yapmaz ve hata vermez.

Bu işlev, bir aksiyondokunma düğmesini özelleştiren bir ailenin parçasıdır. Ailenin diğer üyeleri SetImage ve SetPosition dahil eder.

Parametreler

actionName: string

Eylemin orijinal adı BindAction'a geçti.

Varsayılan değer: ""
title: string

Butona görüntülenecek metin.

Varsayılan değer: ""

Dönüşler

()

Kod Örnekleri

This code sample demonstrates binding an "Inspect" action to a touch button created automatically by ContextActionService. The button is customized using SetImage(), SetTitle(), SetDescription() and SetPosition(). The button is further customized by using GetButton() to get a reference to the ImageButton itself and tinting it green by setting ImageButton.ImageColor3.

Paste this code into a LocalScript placed within StarterPlayerScripts to test it. In Studio, be sure to toggle the touch device emulator in order for the button to actually be created.

ContextActionService Touch Button

local ContextActionService = game:GetService("ContextActionService")
local ACTION_INSPECT = "Inspect"
local INPUT_INSPECT = Enum.KeyCode.E
local IMAGE_INSPECT = "rbxassetid://1826746856" -- Image of a speech bubble with ? in it
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_INSPECT and inputState == Enum.UserInputState.End then
print("Inspecting")
end
end
-- For touch devices, a button is created on-screen automatically via the 3rd parameter
ContextActionService:BindAction(ACTION_INSPECT, handleAction, true, INPUT_INSPECT)
-- We can use these functions to customize the button:
ContextActionService:SetImage(ACTION_INSPECT, IMAGE_INSPECT)
ContextActionService:SetTitle(ACTION_INSPECT, "Look")
ContextActionService:SetDescription(ACTION_INSPECT, "Inspect something.")
ContextActionService:SetPosition(ACTION_INSPECT, UDim2.new(0, 0, 0, 0))
-- We can manipulate the button directly using ContextActionService:GetButton
local imgButton = ContextActionService:GetButton(ACTION_INSPECT)
if imgButton then -- Remember: non-touch devices won't return anything!
imgButton.ImageColor3 = Color3.new(0.5, 1, 0.5) -- Tint the ImageButton green
end

UnbindAction

()

UnbindAction, eylem işleyici işlevinin artık çağrılmayacağından eylem adıyla kullanıcı girişlerinden bir eylemi bağışlayacaktır.Bir eylem için konteyner artık uygulanabilirolmadığında bu işlevi çağırın, örneğin bir kullanıcı arayüzünü kapatmak, bir arabayı kapatmak veya unequipping bir Tool çıkmak.Bağlı eylemlerin nasıl çalıştığına dair daha fazla bilgi için BindAction bakın.

Bu işlev verilen diziile bağlı bir eylem yoksa bir hata atmayacaktır .GetAllBoundActionInfo veya Geliştirici Konsolun "Eylem Bağları" sekmesini kullanarak, mevcut olarak bağlı eylemleri bulabilirsiniz.

Parametreler

actionName: string
Varsayılan değer: ""

Dönüşler

()

Kod Örnekleri

This example properly shows how to use ContextActionService in binding user input to a contextual action. The context is the tool being equipped; the action is reloading some weapon. Test this code sample by placing it in a LocalScript parented to a Tool. When the Tool is equipped, a "Reload" action is bound, and when the Tool is unequipped the "Reload" action is unbound. When the player presses R with the Tool equipped, the message "Reloading!" will appear.

ContextActionService Tool Reload

local ContextActionService = game:GetService("ContextActionService")
local ACTION_RELOAD = "Reload"
local tool = script.Parent
local function handleAction(actionName, inputState, _inputObject)
if actionName == ACTION_RELOAD and inputState == Enum.UserInputState.Begin then
print("Reloading!")
end
end
tool.Equipped:Connect(function()
ContextActionService:BindAction(ACTION_RELOAD, handleAction, true, Enum.KeyCode.R)
end)
tool.Unequipped:Connect(function()
ContextActionService:UnbindAction(ACTION_RELOAD)
end)

UnbindActivate

()

UnbindActivate bir Enum.KeyCode ile kullanılan bir Enum.UserInputType kullanarak bir Tool (veya bir HopperBin ) aktifleştirmek için BindActivate kullanır.Bu işlev temelde bu işlev tarafından gerçekleştirilen eylemi geri alır.

Parametreler

userInputTypeForActivation: Enum.UserInputType

Aynı KullanıcıGirişTürü aslında BindActivate'a gönderildi.

Varsayılan değer: ""
keyCodeForActivation: Enum.KeyCode

BindActivate'e ilk gönderilen aynı Anahtar Kodu.

Varsayılan değer: "Unknown"

Dönüşler

()

UnbindAllActions

()

Tüm bağlı işlevleri kaldırır.Eylem adları kalmayacak.Tüm dokunma düğmeleri kaldırılacaktır.Bir düğme manuel olarak manipüle edildiyse, temizleneceğine dair bir garanti yoktur.


Dönüşler

()

GetButton

Bekletir

GetButton, üçüncü parametre doğruysa ve cihaz ise oluşturulan düğmeyi döndürür.Bu işlevin tek parametresi, aslında BindAction'a gönderilen eylemin adına karşılık gelmelidir.

Eğer böyle bir eylem bağlanmadıysa veya bir düğme oluşturulmadıysa, bu işlev nil döndürür.

Parametreler

actionName: string

Eylemin orijinal adı BindAction'a geçti.

Varsayılan değer: ""

Dönüşler

BindAction tarafından oluşturulan bir Görüntü Düğmesi.

Etkinlikler

LocalToolEquipped

Mevcut oyuncu bir Tool donatırken ateş eder.

Parametreler

toolEquipped: Instance

LocalToolUnequipped

Mevcut oyuncu bir Tool 'yi çıkardığında ateş eder.

Parametreler

toolUnequipped: Instance