Get Names from Lists

0 favourites
  • 6 posts
  • Hello!

    in my project at the start of each level i spawn sprites called "Citizen" with the text variables "FirstName" and "LastName".

    i was wondering how to get and randomly assign a "FirstName" and "LastName" from a list of names i have compiled.

    Searching the forums and tutorials it seems this would be best accomplished with XML files but i dont understand exactly how and maybe there is a better way?

    help please!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Easiest way : use an array. Put all your names in this array, and then take a random element from it.

    Take a look here :events capture

    In the first event, I just put names into the array.

    In the second one, I select a random number, between 0 and (numberOfNames-1). This will be the index of the random name I want to use. I then set some text, but this was to show you how to access the array, and how to have the firstName or the lastName.

    Hope this helps !

  • thanks i will look at the example and read up on arrays!

  • <img src="" border="0">

    original image

    would something like that work?

  • You certainly have the right idea. Although, you could simplify it...

    if you use "Set at X" instead of "Set at XY" you could leave out the extra ",0" at the end of every line that sets or accesses the array.

    You could also easily eliminate the global variables by just putting the random() statement down at the bottom where you set the citizen instance variables. Make sure you put the Floor() outside of the Random() call: you want "Set FirstName to FirstName.At(Floor(Random(FirstName.Width)))"

    Also the last section where you assign the names to citizens will run every tick - constantly reassigning names unless you put that in an "On start of layout" block or in a function...

    One other thing to be aware of, FirstName.Width will be the number of spaces defined in your array, not how many you have used. Your code shows you have set up 3 names, but if your array has room for 10 names, then your code would try to assign names that don't exist sometimes.

  • hm ok thanks will try cleaning it up. also, i want to store hundereds of last and first names, is an Array really optimal for that?

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