simle question about typebox

Hello,

First of all, I do not need a whole script from you guys premade, I won’t learn from that, just send me in a direction of what I shall watch tutorials of and what to concider, things to use etc.

I want the player to be able to start typing once pressing T, and once something is typed he presses enter and whatever he typed activates a spell, for example if he types ‘‘alohamora’’ spell ‘‘alohamora’’ gets activated. And if he types in a spell that doesn’t excist in my list of spells, it will say ‘‘invalid spell’’

so, what would be the most efficient way to do it?
Thanks in advance,
Frisout

First in an update you’ll need to detect the T keypress. Once that’s detected, you’ll need to switch focus to an inputfield. Make sure you set a bool or something that prevents the T press from triggering again while the player is inputting. Also, if you need to disable movement, or other key input.

Once focus is on the inputfield, the player should be able to type as normal. Inputfields have an event on them, something like onFinishedEdit (exact name can’t recall right now). Depending on what your requirements are, you may want to make sure that what they type is converted to full upper chars so you can test if it matches a spell. If it does, handle spell, if it doesn’t, handle spell doesn’t exist. Don’t forget to set your bool to false when they are done “casting”

2 Likes

Well I don’t want to do the cast immidiatly, once it’s typed it just set it to active so you use it with left click, but I can figure that out myself.

Thanks for the reply, I’ll look into it.

What is like an easy command to switch focus to an inputfield?
I searched online and only found HUGE scripts which didnt even work

Try these and see if they work. I haven’t ever messed with selecting an inputfield through code, so not sure

https://docs.unity3d.com/ScriptReference/UI.InputField.ActivateInputField.html
https://docs.unity3d.com/ScriptReference/UI.Selectable.Select.html

1 Like

Thanks for the help so far! I made some great progress. it did take some debugging since at first the player would keep moving to the direction he was moving in if you move and then press T, but it is all good and fixed now :smile:

Now I’m gonna look into making the input actually work once I got time

That last step was actually easy as dogshit :slight_smile:
All I am wondering tho, is if I am doing it the most effiecent way.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class spellList : MonoBehaviour {

    public InputField mainInputField;
    public string spellName;

    void Update ()
    {
            spellName = mainInputField.text;

        //Spells start here

//Lumos
        if (spellName.Contains("lumos"))
        {
            if (Input.GetButtonDown ("mouse1"))
            {
                //does the spell
                Debug.Log ("LUMOS!!!");
            }
//Nox
    if (spellName.Contains("nox"))
        {
            if (Input.GetButtonDown ("mouse1"))
            {
                //does the spell
                Debug.Log ("NOX!!!");
            }
       
    }
}

(I edited it a bit after copy pasting it so some { } stuff may be wrong, whatever)

With this script I need to add a new if function being checked for every spell I will add, maybe there is a way to make a list or so in another way but either way, I think my results are achieved, this is all I wanted when I wrote this topic :smile: