Scripts / Display Text or Image with Key Press

Author
Message
Teabone
Forum Support
18
Years of Service
User Offline
Joined: 8th Jun 2006
Location: Earth
Posted: 10th Jul 2017 16:00
Hi everyone, I've been struggling with what may be one of the most simplest tasks in LUA.

I'm trying to display text or sprites after a key has been pressed and hiding the text or sprite after the same key has been pressed. I've completely mucked up my original test code for this. I have no problem with getting text or images to show up after a key has been pressed but how do you make it hide after pressing the same key? For example like a status window or inventory?



Twitter - Teabone3 | Youtube - Teabone3 | Twitch - Teabone3 | TGC Assets - Store Link

i7 -2600 CPU @ 3.40GHz - Windows 7 - 8GB RAM - Nivida GeForce GT 740
3com
10
Years of Service
User Offline
Joined: 18th May 2014
Location: Catalonia
Posted: 10th Jul 2017 16:34
Use some var as condition, ie:



hth

3com
Laptop: Lenovo - Intel(R) Celeron(R) CPU 1005M @ 1.90GHz

OS: Windows 10 (64) - Ram: 4 gb - Hd: 283 gb - Video card: Intel(R) HD Graphics
cpu mark: 10396.6
2d graphics mark: 947.9
3d graphics mark: 8310.9
memory mark 2584.8
Disk mark: 1146.3
Passmark rating: 3662.4

PM
MrEaKing(YT)
7
Years of Service
User Offline
Joined: 9th Mar 2017
Location: Ontario
Posted: 10th Jul 2017 21:22
using 3coms code to set the variable, why not just relocate it outside the screen SetSpritePosition (sprite, x, y) set the x and y to -1000 should work for hiding it.
Belidos
3D Media Maker
9
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 10th Jul 2017 22:07 Edited at: 10th Jul 2017 22:21
To display text on a key press is simple, just add a variable so if the variable is 0 you don't print text, and if it's 1 you print text, the tricky part is setting the keypress, you need to toggle a variable for on and off states.

Try the attached script, place it in an always active, dynamic entity, when in test if you press J it will toggle the sprite and the text on and off (YourSprite.png goes in scriptbank\images)

I've included sprite control in the script because I've always found that sprite backgrounds for text are always "the next step", so I'm anticipating the next question lol.

What we do when we show/hide sprites, we set the position outside of the screen and then use paste to put it in place using a variable, we use paste so we can set it on a lower level than the text so text appears on top of the sprite.

Basically how it works is it creates the sprite in the init and puts it off screen, then if the condition is met (pressing J) it pastes a copy of the sprite in the screen, the key press for J uses a variable so basically if the variable is 1 it pastes the sprite and text, if it's zero it doesn't paste the sprite and text. If you get what I mean.

Primary Desktop:
i7 7700,k NV1070 8GB, 16GB 3200mhz memory, 1x 2TB Hybrid, Win10.

Secondary Desktop:
i5 4760k, NV960 2GB, 16GB 2333mhz memory, 1x 2TB Hybrid, Win10.

Laptop:
i3, Intel 4000 series graphics, 6GB memory, 1x 500gb HDD, Win8.1.

Attachments

Login to view attachments
Teabone
Forum Support
18
Years of Service
User Offline
Joined: 8th Jun 2006
Location: Earth
Posted: 11th Jul 2017 01:26 Edited at: 11th Jul 2017 02:42
Thanks guys! 3com the only thing I was missing that was different from my code really was the "else" statement. I'm quite new to LUA didn't even consider it.

EDIT: cant seem to get it to work. Testing with prompts (just for testing purposes before checking out text or sprites). It returns a syntax error for the "g_KeyPressE"



I should note I have a working sprite system with text floating on top and various parts of the sprite. I do have the sprites off screen and pasted in, when required in view. Here is a bit of a test of that:



EDIT: Belidos your example works pretty good. Now I just have to understand how you did it lol

EDIT2: Everything is working great with your example codes blended with my own. I have another question that is kind of related. How do you actually disable more than one variable and more than one text string inside a text element?

Example:

TextColor(5,10,3,"Experience:" .. experience,200,200,200)

Displays
Experience: 0

How do I go about displaying something like:

Experience: 0/100

with using "experience" and "maxexperience" variables?

This doesn't work:
TextColor(5,10,3,"Experience:" .. experience "/" .. maxexperience,200,200,200)
Twitter - Teabone3 | Youtube - Teabone3 | Twitch - Teabone3 | TGC Assets - Store Link

i7 -2600 CPU @ 3.40GHz - Windows 7 - 8GB RAM - Nivida GeForce GT 740

Attachments

Login to view attachments
MrEaKing(YT)
7
Years of Service
User Offline
Joined: 9th Mar 2017
Location: Ontario
Posted: 11th Jul 2017 02:19


the problem i found with the syntax appears to be some spaces above the if on your script i rewrote it for these reasons. 1. when you press e it can switch between the 2 states instantly so i added in a pause to give the player time to let go of the e key 2. with prompts they will only appear when you press the button but once its not pressed its no longer being called to be displayed so thats why i added in the extra variable InvShow so a state could be set to show the prompt. attaching this to a barrel and making it always active should get what you were looking for... I hope lol.
oh and sorry if i made it seem harder then what it needs to be i'm just learning too. oh and nice inventory screen looks very professional.
MrEaKing(YT)
7
Years of Service
User Offline
Joined: 9th Mar 2017
Location: Ontario
Posted: 11th Jul 2017 02:22
my bad didn't see your edit at the bottom of your screenshot.
Belidos
3D Media Maker
9
Years of Service
User Offline
Joined: 23rd Nov 2015
Playing: The Game
Posted: 11th Jul 2017 06:03 Edited at: 11th Jul 2017 06:05
Quote: "Everything is working great with your example codes blended with my own. "


That's great, glad i could help.

Quote: "I have another question that is kind of related. How do you actually disable more than one variable and more than one text string inside a text element?"


TextColor(5,10,3,"Experience: "..experience.." / "..maxexperience,200,200,200)

Note the spacing, and the .. at the end of the first variable.

Primary Desktop:
i7 7700,k NV1070 8GB, 16GB 3200mhz memory, 1x 2TB Hybrid, Win10.

Secondary Desktop:
i5 4760k, NV960 2GB, 16GB 2333mhz memory, 1x 2TB Hybrid, Win10.

Laptop:
i3, Intel 4000 series graphics, 6GB memory, 1x 500gb HDD, Win8.1.
Teabone
Forum Support
18
Years of Service
User Offline
Joined: 8th Jun 2006
Location: Earth
Posted: 11th Jul 2017 15:52
Thanks so much you guys are the best. Everything is working perfectly.
Twitter - Teabone3 | Youtube - Teabone3 | Twitch - Teabone3 | TGC Assets - Store Link

i7 -2600 CPU @ 3.40GHz - Windows 7 - 8GB RAM - Nivida GeForce GT 740
3com
10
Years of Service
User Offline
Joined: 18th May 2014
Location: Catalonia
Posted: 11th Jul 2017 17:02
@ Teabone

Sorry my fault, notice the capital "If" instead of "if" (If g_KeyPressE == 0) should look like:
if g_KeyPressE == 1

Anyway glad you fix it; NIce looks your inventory, match fine with the theme.

3com
Laptop: Lenovo - Intel(R) Celeron(R) CPU 1005M @ 1.90GHz

OS: Windows 10 (64) - Ram: 4 gb - Hd: 283 gb - Video card: Intel(R) HD Graphics
cpu mark: 10396.6
2d graphics mark: 947.9
3d graphics mark: 8310.9
memory mark 2584.8
Disk mark: 1146.3
Passmark rating: 3662.4

PM
Teabone
Forum Support
18
Years of Service
User Offline
Joined: 8th Jun 2006
Location: Earth
Posted: 11th Jul 2017 17:18
I actually had no idea capitalizations mattered when it came to commands and things like that.
Twitter - Teabone3 | Youtube - Teabone3 | Twitch - Teabone3 | TGC Assets - Store Link

i7 -2600 CPU @ 3.40GHz - Windows 7 - 8GB RAM - Nivida GeForce GT 740
3com
10
Years of Service
User Offline
Joined: 18th May 2014
Location: Catalonia
Posted: 11th Jul 2017 20:49 Edited at: 11th Jul 2017 21:21
Yah it does, LUA is case sensitive, just the var names can be capital one, ie: PlayerDist, VarName, Ent_Name, etc.
Notice too "g_KeyPressE == 1" instead of "g_KeyPressE == 0"; 1 mean key being pressed.
Edit: Just adding something, g_KeyPressE being replace by g_PlrKeyE, see global.lua file.

3com
Laptop: Lenovo - Intel(R) Celeron(R) CPU 1005M @ 1.90GHz

OS: Windows 10 (64) - Ram: 4 gb - Hd: 283 gb - Video card: Intel(R) HD Graphics
cpu mark: 10396.6
2d graphics mark: 947.9
3d graphics mark: 8310.9
memory mark 2584.8
Disk mark: 1146.3
Passmark rating: 3662.4

PM
Teabone
Forum Support
18
Years of Service
User Offline
Joined: 8th Jun 2006
Location: Earth
Posted: 13th Jul 2017 20:40 Edited at: 13th Jul 2017 20:44
Thanks so much 3com. I'm now working on a banking system. Place for the player to deposit and take out money. Possibility of loans and interest rates. Having a lot of fun so far. Wish GG had a chat function so I could more regularly ask quick questions without flooding the boards

Quote: "oh and nice inventory screen looks very professional."


The current UI is based off an existing game but am slowly forming it into something completely original. Its cool having multiple windows actually work with different keystrokes. I am glad we can finally control images and text now.

Going to be at some point soon testing the mouse with the UI.
Twitter - Teabone3 | Youtube - Teabone3 | Twitch - Teabone3 | TGC Assets - Store Link

i7 -2600 CPU @ 3.40GHz - Windows 7 - 8GB RAM - Nivida GeForce GT 740
smallg
Community Leader
19
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 14th Jul 2017 00:06
ask away, while it might be slower to get answers, by posting you'll likely help more people in future than a chat that would vanish
the mouse in GG is % based like the sprites so it's quite easy to work out which buttons are pressed by checking the area (for simple shapes like squares and rectangles at least - fine for a basic UI).
i.e.
if g_MousePressed == 1 then -- i forget if this 1 is correct variable name
if g_MouseX >= 10 and g_MouseX <= 20 then
if g_MouseY >= 20 and g_MouseY <=25 then
--button pressed at x = 20 y = 20 with size of 10x5
end
end
end
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
Teabone
Forum Support
18
Years of Service
User Offline
Joined: 8th Jun 2006
Location: Earth
Posted: 19th Jul 2017 01:27
The whole mouse functionality thing seems quite powerful and I'm only just touching the surface.

Regarding text again though I have another question. I'm trying to create something to the same effect as the prompt command. I have enemies currently that when they are in range, display their name and health. The issue I have is when another enemy walks within range, it also displays its name and health on the screen overlapped on top of the others.

This is totally do-able with the prompt command, as it would only print one text field at a time and never overlap... but I'd like to use the text commands instead as they have a lot more control. What would be the simplest way to do this without adding to much scripting to my enemies? Is there a way to assign a field of text a variable and only display to that text field from multiple sources similar to how the prompt operates?
Twitter - Teabone3 | Youtube - Teabone3 | Twitch - Teabone3 | TGC Assets - Store Link

i7 -2600 CPU @ 3.40GHz - Windows 7 - 8GB RAM - Nivida GeForce GT 740
MrEaKing(YT)
7
Years of Service
User Offline
Joined: 9th Mar 2017
Location: Ontario
Posted: 19th Jul 2017 06:21
if i'm not mistaken this should work


you could add in another line of code with the enemy script to check if distance is less then the distance of previous enemy or have it be based on if the player is looking at them and within certain range.(check the weapon.lua for looking at object)
Youtube.com/MrEaKing

ELC - Explore, Learn and Create.

Desktop - HomeBuilt- Win 7 - AMD-FX 8300 Eight Core 3.30 Ghz
12 GB RAM
GeForce GTX 1050 Ti 4GB
Teabone
Forum Support
18
Years of Service
User Offline
Joined: 8th Jun 2006
Location: Earth
Posted: 26th Jul 2017 02:03 Edited at: 26th Jul 2017 02:17
Still having issues avoiding two enemies displaying health at the same time. Especially if they are bunched together. See what I can do tonight. Its too bad you cant like give a text box an ID and only write to it instead of creating new layers. I dont mind if a text box was constantly bouncing between different enemy names and health. But having them overlap just doesn't look visually acceptable.

How is the view range determined?

viewrange[e] = AIGetEntityViewRange(EntObjNo)

Is it referencing the property panel of the entity's cone of view and view range(distance)? The characters seem to have these set to 0 by default. When i add degree or anything value i get really odd results. Not desirable. The weapon.lua example like you have suggested makes a lot of sense as i can see its calculating the information about the player in relation to the entity. However that is a lot of code to throw into every enemy. Hmm again will have test out.
Twitter - Teabone3 | Youtube - Teabone3 | Twitch - Teabone3 | TGC Assets - Store Link

i7 -2600 CPU @ 3.40GHz - Windows 7 - 8GB RAM - Nivida GeForce GTX 960
smallg
Community Leader
19
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 26th Jul 2017 09:40 Edited at: 26th Jul 2017 09:42
Add a variable as a toggle to determine which text to show.
So
Tempdist = GetPlayerDistance(e)
if Tempdist < closestdist then
closestdist = Tempdist
closest = e
Prompt("text")
end

if closest == e then
closestdist = Tempdist
end

You would also need to reset the closest and closestdist at the start of the script and if the nearest enemy dies.
I.e. Add
closest = 0
closestdist = 9999
To both init(e) and exit (e)


And yes the view range is related to the properties panel, but only the range, the cone is only used for the final visible check.
Range is counted in the GG units so about 1500 is about max firing range.
The cone or angle is 0 - 180 (180 being full 360 vision).
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
Teabone
Forum Support
18
Years of Service
User Offline
Joined: 8th Jun 2006
Location: Earth
Posted: 26th Jul 2017 16:04
Was actuating going to give mention to a toggle/switch variable. Here is what the current script is which doesn't at all check for if the text is being displayed and ignore it if it is from any other enemy:



I was hoping there was a way in LUA to make this simpler like:

TextColor (ID) (40,6,3,"Barbarian ("..g_Entity[e]['health'].."/100)",255,255,255)

Where you can give a text block an ID and only write to it rather than creating layers of new text. Actually was curious if my code that calls the text is asking to show the text multiple times over and is wasting memory or does it just use it on one layer?

Either way it looks like I have a lot more scripting to plug in to my NPC's. Thanks a lot for your help. Going to give it a whirl.
Twitter - Teabone3 | Youtube - Teabone3 | Twitch - Teabone3 | TGC Assets - Store Link

i7 -2600 CPU @ 3.40GHz - Windows 7 - 8GB RAM - Nivida GeForce GTX 960
smallg
Community Leader
19
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 26th Jul 2017 18:56
unfortunately not, Text in AGK works that way and you get an ID with a persistent text object which stays on screen til you move or delete it but in GG Text is called for 1 frame only, so technically it is created and deleted by the system after each use otherwise you would still have it on screen if you only called the Text command in the init(e) etc and would need to manually delete it in your code.

if you have multiple Text calls in the same place then they overlap yes - which you already know from your problem.

in the end it doesn't really make much difference as you would ideally still be checking for the nearest otherwise it will just show the text for the last entity to run it's code if you have multiple characters in the same state.
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11

Login to post a reply

Server time is: 2024-12-23 01:05:36
Your offset time is: 2024-12-23 01:05:36