Get Names from Lists

Get help using Construct 2

Post » Thu Feb 06, 2014 8:45 am

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!

B
7
Posts: 25
Reputation: 965

Post » Thu Feb 06, 2014 9:02 am

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 !
B
17
S
8
G
4
Posts: 461
Reputation: 6,157

Post » Thu Feb 06, 2014 9:16 am

thanks i will look at the example and read up on arrays!
B
7
Posts: 25
Reputation: 965

Post » Thu Feb 06, 2014 5:19 pm



original image

would something like that work?
B
7
Posts: 25
Reputation: 965

Post » Thu Feb 06, 2014 7:40 pm

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.AllanR2014-02-06 19:48:05
B
95
S
33
G
20
Posts: 438
Reputation: 17,640

Post » Fri Feb 07, 2014 7:30 am

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?
B
7
Posts: 25
Reputation: 965


Return to How do I....?

Who is online

Users browsing this forum: fearfulelephants and 22 guests