ToInt and String Lists
sgreig
07 May 2012, 21:12Anyone have any experience using ToInt with String Lists? I have a string list of numbers that I need to check against a case switch, and apparently I need to use ToInt in there somewhere, but I'm kinda lost. Here's what I'm trying to do:
I have a variable called alibi_die that's set to a random number between 1 and 10. Then, depending on the number, the case switch sets the alibi attribute of the current object to the string I have set. But, I want to have a check in place so that if the number generated has already been used, it generates a new number. This ensures that all the "suspects" have a unique alibi. All of the case switch scripts add the number of that alibi to a string list I created called chosen_alibi. Here's the code I have so far:
So, how do I integrate the ToInt function in here to make this script work? Help is greatly appreciated.
I have a variable called alibi_die that's set to a random number between 1 and 10. Then, depending on the number, the case switch sets the alibi attribute of the current object to the string I have set. But, I want to have a check in place so that if the number generated has already been used, it generates a new number. This ensures that all the "suspects" have a unique alibi. All of the case switch scripts add the number of that alibi to a string list I created called chosen_alibi. Here's the code I have so far:
chosen_alibis = NewStringList()
alibi_die = ""
while (alibi_die = "" or ListContains(chosen_alibis, alibi_die)) {
alibi_die = GetRandomInt(1,10)
switch (alibi_die) {
case (1) {
current_suspect.alibi = current_suspect.firstname+" shrugs nonchalantly and says, \""+current_suspect.alibi_person.firstname+" and I spent the entire evening together in the "+current_suspect.alibi_room.alias+". You've got your work cut out for you on this one, inspector. None of us cared much for "+game.victim.firstname+", and that's the truth. Frankly, whoever did it deserves a medal.\""
list add (chosen_alibis, "1")
}
case (2) {
current_suspect.alibi = current_suspect.firstname+" replies haughtily, \"I spent the entire evening with "+current_suspect.alibi_person.firstname+" in the "+current_suspect.alibi_room.alias+". Of course we both despised "+game.victim.firstname+"--but then, so did everyone else in the house. Frankly, I think you should just leave well enough alone.\""
list add (chosen_alibis, "2")
}
case (3) {
current_suspect.alibi = current_suspect.firstname+" sneers wickedly and replies, \"I spent the entire evening with "+current_suspect.alibi_person.firstname+" in the "+current_suspect.alibi_room.alias+". And I simply cannot believe that anyone in this house would have had the bad taste to commit murder... Though I sometimes think that "+game.murderer.firstname+" is capable of almost anything.\""
list add (chosen_alibis, "3")
}
case (4) {
current_suspect.alibi = current_suspect.firstname+" shrugs nonchalantly and says, \""+current_suspect.alibi_person.firstname+" and I spent the entire evening together in the "+current_suspect.alibi_room.alias+". Far be it from me to meddle in your affairs, inspector, but I really think you should question "+game.murderer.firstname+". Why just the other day "+game.murderer.firstname+" told me that "+game.victim.firstname+" would be better off dead.\""
list add (chosen_alibis, "4")
}
case (5) {
current_suspect.alibi = current_suspect.firstname+" looks up from the floor and says, \"I spent the entire evening with "+current_suspect.alibi_person.firstname+" in the "+current_suspect.alibi_room.alias+". Why, I didn't even find out about "+game.victim.firstname+"'s death until right before you arrived. Isn't it awful. Who do you think could do such a thing!?!\""
list add (chosen_alibis, "5")
}
case (6) {
current_suspect.alibi = current_suspect.firstname+" forces a laugh and replies, \""+current_suspect.alibi_person.firstname+" and I spent the entire evening together in the "+current_suspect.alibi_room.alias+". Now just between you and me and the walls, inspector, I admired "+game.victim.firstname+" greatly. Wouldn't want the others to know about my little soft spot though.\""
list add (chosen_alibis, "6")
}
case (7) {
current_suspect.alibi = current_suspect.firstname+" turns away from you and murmurs, \"I spent the entire evening with "+current_suspect.alibi_person.firstname+" in the "+current_suspect.alibi_room.alias+".... I'm just so shocked by this whole affair. Murder is such an ugly business, don't you think? Of course I've heard that "+game.murderer.firstname+" doesn't share my opinion on that point...\""
list add (chosen_alibis, "7")
}
case (8) {
current_suspect.alibi = current_suspect.firstname+" laughs lightly and replies, \"I spent the entire evening with "+current_suspect.alibi_person.firstname+" in the "+current_suspect.alibi_room.alias+". Now... is that all you wanted to know? I really would like to go lie down if you don't mind. All of this murder business has given me a splitting headache.\""
list add (chosen_alibis, "8")
}
case (9) {
current_suspect.alibi = current_suspect.firstname+" replies in an angry voice, \"I spent the entire evening with "+current_suspect.alibi_person.firstname+" in the "+current_suspect.alibi_room.alias+". And I simply cannot believe that anyone in this house would have had the bad taste to commit murder.... Though I sometimes think that "+game.murderer.firstname+" is capable of almost anything.\""
list add (chosen_alibis, "9")
}
case (10) {
current_suspect.alibi = current_suspect.firstname+" raises an eyebrow and says, \""+current_suspect.alibi_person.firstname+" and I spent the entire evening together in the "+current_suspect.alibi_room.alias+". Actually, in my opinion, there's no one in this house with the intellectual capacity to commit murder and get away with it. Except for myself, of course. Too bad I have an alibi!\""
list add (chosen_alibis, "10")
}
}
So, how do I integrate the ToInt function in here to make this script work? Help is greatly appreciated.

Pertex
08 May 2012, 06:57You could use ToString instead, something like
But there are other problems with this function
is always true so you have an infinite loop. And I think you don't need this GetRandomInt, because it doesn't matter. If alibi_die=1 then you always get the text of the first case in position 1, and so on. Then you can use a "for (alibi_die,1,10)" which does the same
while (alibi_die = "" or ListContains(chosen_alibis, alibi_die)) {
alibi_die = ToString(GetRandomInt(1,10))
switch (alibi_die) {
case ("1") {
....
But there are other problems with this function
while (alibi_die = "" or ListContains(chosen_alibis, alibi_die)) {
is always true so you have an infinite loop. And I think you don't need this GetRandomInt, because it doesn't matter. If alibi_die=1 then you always get the text of the first case in position 1, and so on. Then you can use a "for (alibi_die,1,10)" which does the same
sgreig
08 May 2012, 08:02i'm not surprised. I took that code from a function in another game I'm working on. I didn't think it would be so difficult to implement something like this. All I want to do is have it generate a random number for each suspect in the game, and apply the string for that case to their alibi attribute, and have the number be re-rolled if the current number rolled has already been used.
I must not be seeing what you're getting at with the for loop, so could you explain it in a little more detail?
I must not be seeing what you're getting at with the for loop, so could you explain it in a little more detail?

Pertex
08 May 2012, 12:07I think I don't understand your intension and your code. Do you have another loop around your posted code where you cycle through all persons? Where do you call this code? Or could you send me a mail with your game?
sgreig
08 May 2012, 16:43Wow, yeah that probably would help wouldn't it? Here's the code for the whole function. It does loop through every suspect in the game already. First it checks if the suspect was alone, and if so picks a random alibi from a separate list, then if not, is supposed to pick a random alibi from the regular list. What I want is for the game to keep track of which alibis have been picked from the regular list so that if the same number comes up again, it gets re-rolled. This way, the suspects in the game won't ever have the same alibi text.
Also, I should mention that I will be adding more alibis to that list eventually as well so it will become less of an issue, but I still want to have this implemented because even if there's 100 different alibis, there's still an alibi could be picked more than once.
<function name="generate_alibi_strings">
chosen_alibi = NewStringList()
foreach (current_suspect, game.suspects_list) {
if (current_suspect.alibi_person = "alone") {
alibi_die = GetRandomInt(1,5)
switch (alibi_die) {
case (1) {
current_suspect.alibi = current_suspect.firstname+" turns to face you and whispers, \"I spent the entire evening alone in the "+current_suspect.alibi_room.alias+". I know that's not much of an alibi, but you must believe me when I say I had no reason to kill "+game.victim.firstname+". No reason at all.\""
}
case (2) {
current_suspect.alibi = current_suspect.firstname+" turns away from you and murmurs, \"I spent the entire evening alone in the "+current_suspect.alibi_room.alias+". And I'm afraid that nobody saw me. But I hope you're not so silly as to suspect me of the crime. Why, I hardly knew "+game.victim.firstname+", and I certainly had no reason to commit murder.\""
}
case (3) {
current_suspect.alibi = current_suspect.firstname+" replies haughtily, \"I spent the entire evening alone in the "+current_suspect.alibi_room.alias+". I suppose you assume that I killed "+game.victim.firstname+" just because I haven't an alibi, but I never cared one way or the other about that little manipulator. You'll just have to take my word.\""
}
case (4) {
current_suspect.alibi = current_suspect.firstname+" draws you closer and whispers, \"I spent the entire evening alone in the "+current_suspect.alibi_room.alias+". So, I guess I'm your prime suspect, inspector. But I think you're going to find that I didn't have any reason to murder "+game.victim.firstname+".\""
}
case (5) {
current_suspect.alibi = current_suspect.firstname+" replies distractedly, \"I spent the entire evening alone in the "+current_suspect.alibi_room.alias+". Now I suppose that looks bad for me, doesn't it? But you must believe that I could never have hurt anyone. And I certainly couldn't have killed "+game.victim.firstname+"... I just couldn't.\""
}
}
}
else {
alibi_die = ""
while (alibi_die = "" or ListContains(chosen_alibi, alibi_die)) {
alibi_die = GetRandomInt(1,10)
switch (alibi_die) {
case (1) {
current_suspect.alibi = current_suspect.firstname+" shrugs nonchalantly and says, \""+current_suspect.alibi_person.firstname+" and I spent the entire evening together in the "+current_suspect.alibi_room.alias+". You've got your work cut out for you on this one, inspector. None of us cared much for "+game.victim.firstname+", and that's the truth. Frankly, whoever did it deserves a medal.\""
list add (chosen_alibis, "1")
}
case (2) {
current_suspect.alibi = current_suspect.firstname+" replies haughtily, \"I spent the entire evening with "+current_suspect.alibi_person.firstname+" in the "+current_suspect.alibi_room.alias+". Of course we both despised "+game.victim.firstname+"--but then, so did everyone else in the house. Frankly, I think you should just leave well enough alone.\""
list add (chosen_alibis, "2")
}
case (3) {
current_suspect.alibi = current_suspect.firstname+" sneers wickedly and replies, \"I spent the entire evening with "+current_suspect.alibi_person.firstname+" in the "+current_suspect.alibi_room.alias+". And I simply cannot believe that anyone in this house would have had the bad taste to commit murder... Though I sometimes think that "+game.murderer.firstname+" is capable of almost anything.\""
list add (chosen_alibis, "3")
}
case (4) {
current_suspect.alibi = current_suspect.firstname+" shrugs nonchalantly and says, \""+current_suspect.alibi_person.firstname+" and I spent the entire evening together in the "+current_suspect.alibi_room.alias+". Far be it from me to meddle in your affairs, inspector, but I really think you should question "+game.murderer.firstname+". Why just the other day "+game.murderer.firstname+" told me that "+game.victim.firstname+" would be better off dead.\""
list add (chosen_alibis, "4")
}
case (5) {
current_suspect.alibi = current_suspect.firstname+" looks up from the floor and says, \"I spent the entire evening with "+current_suspect.alibi_person.firstname+" in the "+current_suspect.alibi_room.alias+". Why, I didn't even find out about "+game.victim.firstname+"'s death until right before you arrived. Isn't it awful. Who do you think could do such a thing!?!\""
list add (chosen_alibis, "5")
}
case (6) {
current_suspect.alibi = current_suspect.firstname+" forces a laugh and replies, \""+current_suspect.alibi_person.firstname+" and I spent the entire evening together in the "+current_suspect.alibi_room.alias+". Now just between you and me and the walls, inspector, I admired "+game.victim.firstname+" greatly. Wouldn't want the others to know about my little soft spot though.\""
list add (chosen_alibis, "6")
}
case (7) {
current_suspect.alibi = current_suspect.firstname+" turns away from you and murmurs, \"I spent the entire evening with "+current_suspect.alibi_person.firstname+" in the "+current_suspect.alibi_room.alias+".... I'm just so shocked by this whole affair. Murder is such an ugly business, don't you think? Of course I've heard that "+game.murderer.firstname+" doesn't share my opinion on that point...\""
list add (chosen_alibis, "7")
}
case (8) {
current_suspect.alibi = current_suspect.firstname+" laughs lightly and replies, \"I spent the entire evening with "+current_suspect.alibi_person.firstname+" in the "+current_suspect.alibi_room.alias+". Now... is that all you wanted to know? I really would like to go lie down if you don't mind. All of this murder business has given me a splitting headache.\""
list add (chosen_alibis, "8")
}
case (9) {
current_suspect.alibi = current_suspect.firstname+" replies in an angry voice, \"I spent the entire evening with "+current_suspect.alibi_person.firstname+" in the "+current_suspect.alibi_room.alias+". And I simply cannot believe that anyone in this house would have had the bad taste to commit murder.... Though I sometimes think that "+game.murderer.firstname+" is capable of almost anything.\""
list add (chosen_alibis, "9")
}
case (10) {
current_suspect.alibi = current_suspect.firstname+" raises an eyebrow and says, \""+current_suspect.alibi_person.firstname+" and I spent the entire evening together in the "+current_suspect.alibi_room.alias+". Actually, in my opinion, there's no one in this house with the intellectual capacity to commit murder and get away with it. Except for myself, of course. Too bad I have an alibi!\""
list add (chosen_alibis, "10")
}
}
}
}
}
</function>
Also, I should mention that I will be adding more alibis to that list eventually as well so it will become less of an issue, but I still want to have this implemented because even if there's 100 different alibis, there's still an alibi could be picked more than once.

Pertex
08 May 2012, 19:41OK, I got it. try this:
else {
alibi_die = ""
alibi_die = ToString(GetRandomInt(1,10))
while ( ListContains(chosen_alibi, alibi_die)) {
alibi_die = ToString(GetRandomInt(1,10))
}
ist add (chosen_alibi, alibi_die)
switch (alibi_die) {
case ("1") {
current_suspect.alibi = current_suspect.firstname+" shrugs nonchalantly and says, \""+current_suspect.alibi_person.firstname+" and I spent the entire evening together in the "+current_suspect.alibi_room.alias+". You've got your work cut out for you on this one, inspector. None of us cared much for "+game.victim.firstname+", and that's the truth. Frankly, whoever did it deserves a medal.\""
}
case ("2") {
current_suspect.alibi = current_suspect.firstname+" replies haughtily, \"I spent the entire evening with "+current_suspect.alibi_person.firstname+" in the "+current_suspect.alibi_room.alias+". Of course we both despised "+game.victim.firstname+"--but then, so did everyone else in the house. Frankly, I think you should just leave well enough alone.\""
}
sgreig
08 May 2012, 19:55YES! THANK YOU!!!
I can see by your code there that I was thinking along the right lines, but I was implementing it wrong. It makes total sense when I look at it now too. I can't believe I didn't figure that out. Oh well, thank you so much!!
I can see by your code there that I was thinking along the right lines, but I was implementing it wrong. It makes total sense when I look at it now too. I can't believe I didn't figure that out. Oh well, thank you so much!!
