Randomized unhandled events for several characters??

Started by Chomba, Wed 29/09/2021 08:30:47

Previous topic - Next topic

Chomba

Hello! here I am again with more problems!  :grin:

Well, I plan to have 3 playable characters throughout the game and I wanted to give unique sentences to each for unhandled events.

So far I accomplished this with the following code model:

Code: ags
function unhandled_event (int what, int type)
{
if (what == 1) // Unhandled events for HOTSPOTS
{
if (type == 1) // look
{
    
switch (player)
{
    
case cRoger:
cRoger.Say("Unhandled events for HOTSPOTS 1A");
break;
    
case cBruno:
cBruno.Say("Unhandled events for HOTSPOTS 2A");
break;
    
case cCarlos:
cCarlos.Say("Unhandled events for HOTSPOTS 3A");
break;
        
}}


So far it works fine, I managed to apply it to each of the unhandled events. The problem arises when I want to add more lines and randomize them (I want each character to have at least 3 varied lines).

I tried combining it with a system I had set up for a single character and it worked well:

Code: ags
 if (what == 1) // Unhandled events for HOTSPOTS
  {
    if (type == 1) // look
    {
    int i;
    i = Random (8);
    if (i == 0)player.Say ("Nada interesante por allí");
    if (i == 1)player.Say ("Nada nuevo bajo el sol");
    if (i == 2)player.Say ("Nada que llame la atención");
    if (i == 3)player.Say ("No le veo nada especial");
    if (i == 4)player.Say ("No tengo nada que decir al respecto");
    if (i == 5)player.Say ("Aha...");
    if (i == 6)player.Say ("¿Debería decir algo sobre eso?");
    if (i == 7)player.Say ("Lo veo");
    if (i == 8)player.Say ("Nada especial sobre eso");


and it looked something like this:

Code: ags
function unhandled_event (int what, int type)
{
if (what == 1) // Unhandled events for HOTSPOTS
{
if (type == 1) // look
{
    
switch (player)
{
    
case cRoger:
int i;
    i = Random (2);
    if (i == 0)player.Say ("Nada interesante por allí");
    if (i == 1)player.Say ("Nada nuevo bajo el sol");
    if (i == 2)player.Say ("Nada que llame la atención");
break;
    
case cBruno:
int i;
 i = Random (2);
    if (i == 0)player.Say ("No sabría que hacer con eso");
    if (i == 1)player.Say ("No se que hacer con eso");
    if (i == 2)player.Say ("No me interesa hacer nada con eso");
break;
    
case cCarlos:
int i;
 i = Random (2);
  
    if (i == 0)player.Say ("No creo que eso funcione");
    if (i == 1)player.Say ("¿Por que haría eso?");
    if (i == 2)player.Say ("No le veo el sentido");
break;
        
}}


but this logic does not apply, so it does not work.
How can I achieve the desired effect?

Khris

What do you mean by "this logic does not apply"? You cannot declare a variable multiple times. This might cause an error. Are you getting an error when trying to compile this or does it work other than expected inside the game?

Also, if all of them are going to have the same number of possible responses, just do
Code: ags
  int i = Random(2);

once, before the switch block.

Chomba

I was getting an error.
I put it together as you said and it worked fine:

Code: ags
function unhandled_event (int what, int type)
{

if (what == 1) // Unhandled events for HOTSPOTS
{
if (type == 1) // look
{
int i = Random(2);    
switch (player)
{
    
case cRoger:
    if (i == 0)player.Say ("X1");
    if (i == 1)player.Say ("X2");
    if (i == 2)player.Say ("X3");
break;
    
case cBruno:
    if (i == 0)player.Say ("Z1");
    if (i == 1)player.Say ("Z2");
    if (i == 2)player.Say ("Z3");
break;
    
case cCarlos:
    if (i == 0)player.Say ("Z1");
    if (i == 1)player.Say ("Z2");
    if (i == 2)player.Say ("Z3");
break;
        
}}


Thanks again Khris!

Crimson Wizard

This is not necessary, but only as a hint: you have several consecutive "ifs" only one of which will ever trigger. Because of how script works, all of these ifs will be tested even if the first one already triggered. This makes your script slightly slower. The traditional way of doing this is to use if/else instead - this way the list of conditions will be skipped as soon as the first match is found:

so instead of
Code: ags

if (i == 0)player.Say ("X1");
if (i == 1)player.Say ("X2");
if (i == 2)player.Say ("X3");


better is
Code: ags

if (i == 0)      player.Say ("X1");
else if (i == 1) player.Say ("X2");
else if (i == 2) player.Say ("X3");


This also guarantees that only one of them will trigger each time. Maybe not critical in this case, but may help avoid mistakes in the future.

Chomba

Cool! Thanks CW, I will apply that.
I had noticed a difference (and I use the else if in other parts of the code), but it wasn't very clear to me.

SMF spam blocked by CleanTalk