Jump to content

Text in-game by pressing the interact button?


lxFirebal69xl
 Share

Recommended Posts

Exactly what the title says, how can I have assets in game that the player is able to use and text shows up? Kind of like how in some horror games the player clicks an object and the main character talks about it and text shows up. I'm a LUA noob, so I apolozige in advance if this is a dumb question.

Thanks for reading

 

-Fire

Link to comment
Share on other sites

I might have a script i can post for you to try out. I think you could look it over and learn from it to make or modify to suit your needs.

 

As far as that particular script nick referenced, if you open it up and look at it, you will see this,

function App:Start()

 self.window = Window:Create()
 self.context = Context:Create(self.window)
 local font = Font:Load("Fonts/Arial.ttf",36)
 self.context:SetFont(font)
 font:Release()
 return true
end
function App:Loop()
 if (self.window:Closed() or self.window:KeyDown(Key.Escape)) then return false end

 self.context:SetColor(0,0,0)
 self.context:Clear()

 --Draw some centered text on the screen
 local text = "Leadwerks"

 local font = self.context:GetFont()
 local x = self.window:GetWidth()/2
 local y = self.window:GetHeight()/2
 x = x - font:GetTextWidth(text)/2
 y = y - font:GetHeight()/2
 self.context:SetBlendMode(Blend.Alpha)
 self.context:SetColor(1,1,1)
 self.context:DrawText(text,x,y)
 self.context:SetBlendMode(Blend.Solid)
 self.context:Sync()
 return true
end

 

the part that says

 --Draw some centered text on the screen
 local text = "Leadwerks"

the word Leadwerks between the quotes is the text you are seeing on the screen. You can change this to say what ever you want. Im not too familiar with the script but Id imagine that you would be limited in how much text can be there. Its somethign you would have to play with.

self.context:SetBlendMode(Blend.Alpha)
 self.context:SetColor(1,1,1)
 self.context:DrawText(text,x,y)
 self.context:SetBlendMode(Blend.Solid)

The setcolor command is where the color of the text is set. Bottom of the script. I use the color editor from Paint to find a color I like and then take the numbers there and divide them by 255 to get teh hexdecimal value to actually set the color. This is a RGBA scale so the first number in that sequence is the RED value, the second number is the GREEN and then the BLUE as well as the ALPHA value.

 

The setcolor command at the top of the script is for defining the color of that box that is drawn. So, if you wanted that box to be red, that would look like self.context:SetColor(1,0,0). If you wanted to add the alpha to it, it would look like self.context:SetColor(1,0,0,.5)

 

This creates a rectangle of the red color and half of its transparency.

 

I really hope Ive explained this correcxtly but thats what I know about that. rolleyes.gif If i can be of any further help here, feel free to ask. if I know, Ill happily tell you.

Asus sabertooth 990FX, AMD FX 9590 , Windows 7 Home Premium 64 Bit, 4 HDD's Western Digital Caviar Black set in Raid 0, 16 GB Crucial Ballistix Elite, Asus Radeon R9 270 X 4GB, Corsair CM750M,

Link to comment
Share on other sites

Thanks for the answer but, now everytime I run the game there's a black screen that says "Leadwerks". Again, I'm pretty new to Lua, so I really don't know how to put the text to show up only when the player interacts with the object.

 

I'm new to Lua too and wanted to to something similar myself. It probably isn't very good, but my solution was as follows:

 

Script.ObjectDescription = "" --String "Object Description"
Script.DisplayTimeMax = 5000 --Int "Display time"
Script.Used = "0"
Script.DisplayTime = 0
Script.DisplayEnabled = false
local font1 = Font:Load("Fonts/arial.ttf", 15)

function Script:Use(context)
  self.Used = "1"
  self.CurrentTime = Time:GetCurrent()
  self.DisplayTime = Time:GetCurrent() + self.DisplayTimeMax
end


function Script:PostRender(context)
  if self.Used == "1" then
  App.context:SetBlendMode(Blend.Alpha)
  App.context:SetFont(font1)
  self.DisplayEnabled = true
	   if self.DisplayEnabled == true then
		    if self.DisplayTime > Time:GetCurrent() then

				 self.texture = Texture:Load("Materials/Developer/GreenGrid.tex")
				 App.context:SetColor(1,1,1)
				 App.context:DrawRect(598,598,304,104,0)
				 App.context:DrawImage(self.texture,600,600,300,100)
				 App.context:SetColor(1,1,1)

				 App.context:SetColor(0.1,0,1)
				 context:DrawText(self.ObjectDescription, 610, 625)
			 else
				 self.DisplayEnabled = false
				 self.Used = "0"
			 end
		   end
		   App.context:SetBlendMode(Blend.Solid)
	   end
end

 

Basically, this allowed me to display a short piece of text with a background when I pressed the Use key over an object the script was attached to.

  • Upvote 2
Link to comment
Share on other sites

That seems t be what he is after. Looks like a great script and thank you for helping some one else here with that script. More of what the community could use more than the typical response of

One way is to keep an empty string variable (i.e. "") and then use this command to draw text after you render:

http://www.leadwerks.com/werkspace/page/documentation/_/command-reference/context/contextdrawtext-r731

 

Not every one can make sense of this and can actually look at the script posted to learn how it works.

Asus sabertooth 990FX, AMD FX 9590 , Windows 7 Home Premium 64 Bit, 4 HDD's Western Digital Caviar Black set in Raid 0, 16 GB Crucial Ballistix Elite, Asus Radeon R9 270 X 4GB, Corsair CM750M,

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...