0
Answered

Setting UI text to a local variable

kytestar 2 weeks ago in Game Creator updated by Marti (Lead Developer) 1 week ago 5

Heya.

I am trying to change the value of a UI Text Field so that it takes it's entry from a local variable attached to my enemy prefab.  What I want is the enemy name displayed on the "target information" on the UI.  I have looked through most of the actions I thought might do this, but I cannot find anything suitable.


I created a local variable component on my enemy character called "EnemyName".  I then set that as a string and entered in "Robot".  All my enemy prefabs (orcs, trolls, etc)  will have the same variable name (i.e. EnemyName) so the target system takes the name from that field.

As I could not directly find a way to make the local variable dynamically populate the UI text field I thought that I could populate a Global Variable with the current target and show that in the UI Text (I found a way to do it static, using the Variable Text script and selecting Local Variable, adding the Game Object as my robot character and then selecting EnemyName as the field, but this would always show "Robot" as the target name regardless of what was selected).

Now I need to find out how to populate the Global Variable with the selected Game Object's local EnemyName variable, and again, I cannot seem to find an action that takes a local variable value and populates a global variable.

Struggling with this atm.  Could you assist at all with how you would handle this, please.

Many thanks, Steve

Unity version:
2018.4.11f1
Game Creator version:
1.0.4

Answer

Answer

Working!  I had a few problems and had somehow managed to add the Variable Text directly onto my canvas which stopped the left mouse click working (it was also added in the correct place too, I still have no idea how I did that :) ).  I have it working now as I realised that the action needed to be on the enemy game object, and not a standalone action. 

Thanks so much for your help, it was most appreciated :)

Answered

Hi Kytestar;

Good question. So basically it boils down to how to make a targeting system, where you want to display the name (and probably some stat values, like the health) of the current targeted game object.

First of all, you'll need a Local/Global variable that holds the object being targeted. We're going to be using a Global Variable, since it makes more sense and is easier to do. Let's create a Global Variable of type Game Object and call it "target".

We'll let you do the logic behind how the object gets selected. You could, for example, add a "On Left Click" Trigger on each enemy and use the Assign Game Object action, to assign the enemy to the "target" global variable. This is up to you.

Now, to display a text on screen with the value of a variable you can use the "Text Variable" component you'll find right clicking on the Hierarchy Panel -> Create -> Game Creator -> UI -> Text Variable. This component looks exactly like Unity's Text component, but also allows to print the value of a variable using the {0} symbol. You can select the "target" Global Variable and it will print the name of the game object selected.


Try clicking play and see how it goes. If it works, great! However, you probably want to print the name of the Local Variable that holds the name of the enemy, not the name of the game object. This is fairly easy to do too. All you need to do is create a new Global Variable of type "String" and call it "target-name". Then, create a Trigger with the type "On Variable Change". You can use this to detect when the "target" variable changes and assign the value of the Local Variable that holds the name to "target-name".


Now, all that's left to do is change the Text Variable component and tell it to print the value of "target-name" instead of the value of "target".


Hope this helps. Let me know if there's anything unclear. Cheers!

Thanks so much for the reply.  The problem I have is the final stage, which is "assign the value of the Local Variable that holds the name to "target-name"".

I am not sure what action to use for this task.  Also, if I use the Local Variable, do I have to add a "Game Object" to be able to select the correct variable.  If I add a Game Object, wont the value of the local variable always be set to that specific "Game Object".

I have tried other ways of doing this using either Local Variable, or Invoker and none of them seem to set the Global Variable to reflect the target name (unless I set a Game Object, whereby it always sets it to that target name).

I'll keep persevering (I am trying currently with the Variable Action - Assign String).

Continued:  I tried setting this up using the example-6 from the shooter examples. 

1.  I created 2 Global Variables - "Target" and "Target-Name"

2. I added a local variable to the enemy (called enemy-name (string) and set it to "Robot Gunner").

3. I created a trigger called TriggerTarget and added an input action to target via Tab.  The target action I used, stored the Target into the corresponding (Target) global variable if the Game Object within 30 from player had a local variable called "enemy-string".

4.  I created a trigger called TriggerTargetName and added a "On Variable Change" for the Global Variable "Target".  The action I used then was the "Assign String" which I set to replace the Global Variable "Target-Name" with the result from "Local Variable -> Invoker -> "enemy-name".  I then added two debug actions to look for Name and also the value of "Target-Name".

Upon running (and running around to avoid getting shot), I tabbed and got the results "Name: TriggerTargetName" and "Target-Name: null".  It looks like the invoker inherited the name of the trigger but did not populate the global variable Target-Name via the local variable "enemy-name".  I am sure I am missing something simple, but cannot figure this out.  Been working on this now for almost 3 days :(.  Thanks for any assistance :)

Answer

Working!  I had a few problems and had somehow managed to add the Variable Text directly onto my canvas which stopped the left mouse click working (it was also added in the correct place too, I still have no idea how I did that :) ).  I have it working now as I realised that the action needed to be on the enemy game object, and not a standalone action. 

Thanks so much for your help, it was most appreciated :)

Hi! Sorry for the late reply. As I said on Discord, happy this is fixed :-D You solution looks good to me. I'm closing this ticket now. Cheers!