Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


How to use variables in the middle of a string?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
LastExceed
Expert Cheater
Reputation: 1

Joined: 05 Nov 2014
Posts: 130

PostPosted: Thu Jan 28, 2016 7:23 am    Post subject: How to use variables in the middle of a string? Reply with quote

What I want to do is first giving an integer that is then used inside a string:

Code:
x=1
showMessage('number x is now selected')


This ^ ofcourse does NOT work, it's just showing what I want the result to be like.
I know I can put the number at the end by doing
Code:
x=1
showMessage('you selected number'..x

But I need to know how to use x in the middle of a string (the result needs to be a single string)

How to do that?
Back to top
View user's profile Send private message
hhhuut
Grandmaster Cheater
Reputation: 6

Joined: 08 Feb 2015
Posts: 607

PostPosted: Thu Jan 28, 2016 7:30 am    Post subject: Reply with quote

Have you tried
Code:
x=1
showMessage('number ' .. x .. ' is now selected')
Back to top
View user's profile Send private message
LastExceed
Expert Cheater
Reputation: 1

Joined: 05 Nov 2014
Posts: 130

PostPosted: Thu Jan 28, 2016 7:50 am    Post subject: Reply with quote

hhhuut wrote:
Have you tried
Code:
x=1
showMessage('number ' .. x .. ' is now selected')


Thanks!
Back to top
View user's profile Send private message
Zanzer
I post too much
Reputation: 126

Joined: 09 Jun 2013
Posts: 3278

PostPosted: Thu Jan 28, 2016 4:02 pm    Post subject: Reply with quote

Code:
x = 1
y = 1.523
z = "dunno"
showMessage(string.format("number %d is now selected, oh and %0.1f has... %s",x,y,z))
Back to top
View user's profile Send private message
atom0s
Moderator
Reputation: 198

Joined: 25 Jan 2006
Posts: 8517
Location: 127.0.0.1

PostPosted: Thu Jan 28, 2016 5:00 pm    Post subject: Reply with quote

string.format, as Zanzer pointed out, is the best option here for a cleaner and more dynamic building of the result string. For more info on the tokens that can be used with it, check out:
http://www.lua.org/manual/5.3/manual.html#pdf-string.format
http://pubs.opengroup.org/onlinepubs/009695399/functions/sprintf.html

(Be mindful of what the Lua docs say on what is and isn't supported.)

_________________
- Retired.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites