0 votes

I would like to use simple line based .txt files as data source for games. Each line of text I would like to represent as a wall, for example. What would be best way of doing this? Any hints or similar examples, games, demos? Thanks in advance.

Example case: .txt file with 100 lines, in game I have 100 walls, each line should be randomly on some wall. When touching this wall, something could happen to the text content for example. e.g. it could be translated to another language. Imagine vocabulary learning games, or anything you could do with text.

Second question, which is not in the title. How you would do same with 100 .svg files, rather than 100 lines of text? 100 .svg files, randomly placed on 100 walls in a game. A game development could be very quick then, just design your 100 svg files, and convert it quickly to a 'wall based game'. Imagine this could be something like an art gallery where every 'art piece' is represented on one wall. I would prefer .svg over .png, here.

in Engine by (15 points)

1 Answer

0 votes

Before anything else, you can load the lines from file into an array, and call array.shuffle() to randomize the order:

func load_file(filename):
  var result = {}
  var f = File.new()
  var index = 1
  while not f.eof_reached():
    var line = f.get_line()
    result[str(index)] = line
    index += 1

Then, in the ready function of your wall object, it should read the next element of your array.

text = result[GLOBAL.index]
GLOBAL.index += 1
by (337 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.