How do I "set text" using variable value ?

0 favourites
  • 11 posts
From the Asset Store
OpenAI TTS
$10 USD
Text-to-Speech plugin.Allows your Construct 3 project to convert written text into spoken words
  • Hi,

    I'm new to Construct and I think is awesome but it will take time to learn it.

    I'd like to create a book. For this book I'm trying to use just 2 layouts (1 is the Menu and the second one is the book itself). For the chapters I'm using a list in the Menu layout, I did a search function and when the chapter is selected it will be the current value of a variable. When I go to the Book layout, I'm using 2 sprite fonts, one for the chapter title and one for the story. For the title is easy, I'm using "set text to variable_name" and the title is the value from the variable (Chapter1 for example). Now for the text story I can't find a solution. I created texts for every chapter and the name of the texts are the exact names from the list in the menu (e.g Chapter1). How can I use the variable value (that is the same as the title and the name of the Chapter) and set text from Chapter1.Text for example (set text to variable_name.Text is not allowed, is not an object). Hope someone will help me.

    Thank you in advanced.

  • Construct doesn't allow to access variables by their string name. It's only possible via scripting.

    You should use a bunch of events like these:

    Compare variable ChapterNameVariable="Chapter1" Text Set text to Chapter1
    Compare variable ChapterNameVariable="Chapter2" Text Set text to Chapter2
    .....
    

    But a much better solution is to use a dictionary. Create a new Dictionary file with texts for each chapter. Then you'll be able to access dictionary keys using these expressions:

    Dictionary.Get("Chapter1")

    or

    Dictionary.Get(ChapterNameVariable)

  • Thank you dop, I'll try as you said.

  • You are awesome !!! A big THX from me.

    Another issue I'm having is that the chapters are not the same, as lenght, of course.

    I tried like this to make the sprite font automatically as the text but it's not showing the entire text.

    sprite_font_story set size

    width:sprite_font_story.TextWidth

    height:len(sprite_font_story.Text)

    After that I did

    sprite_font_story set size

    width:sprite_font_story.TextWidth

    height:len(sprite_font_story.Text)*2

    now it shows the entire text but I don't know if it's correct or not, also for big texts.

  • width:sprite_font_story.TextWidth

    height:len(sprite_font_story.Text)

    TextWidth returns the width of text in Text/Spritefont object. Setting width to TextWidth will have no effect.

    And len() expression returns the number of characters. Setting object height (in pixels) to the number of characters makes absolutely no sense.

    .

    Simply make the SpriteFont object large enough in layout editor to fit the largest chapter.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thank you dop for your time!

    Is there a way to search in a list by any of the characters ? Not just the first letter. Can the list be edited ? as modified, like css, to do it more artistic to say so.

  • Check out this demo:

    howtoconstructdemos.com/search-for-a-text-in-a-large-list-by-typing-first-letters-capx

    Lists and other Form Control objects (TextInput, Button, ProgressBar etc.) are difficult to style. You may be able to change some properties with CSS, but the possibilities are limited. There are other issues with them form controls (for example, they are drawn on top of everything else on the screen, they may "steal" keyboard and mouse events etc.), so they are not suitable for most games.

  • Thank you dop !!!

  • dop,

    I copied the json file to an website and tried an ajax request from url but it's not working, it's like it doesn't have access to that path and is not retrieving the data from the json. I've used "On completed" event but no luck.

    Any idea ?

    Thank you in advanced.

  • You need to show your code, otherwise it's guesswork.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)