Dialogue System

F

FawkingAwesome

Guest
Hey guys, I'm working on making a dialogue system but it's proving to be fairly difficult. The code goes through three stages before it's finally displayed, I'm not entirely sure how to phrase my code but its starts from the civilian your talking to, which has a set of strings that are listed as either being response or text based, it passes those lines through to a script which uses a with object to funnel the lines to an object that can print them with draw_gui. The lines are added to one of two ds_lists depending on whether it's player input or a response from the ai. For some reason instead of incrementing, counter starts a one when dialogue is initally started, then goes from 2 to 1 every time spacebar is pressed, which keeps it stuck at player input. I've attached a picture of the output below, thank you

This is the code from the civilian to the script named Dialogue Code

referTo = oRespond;
keyActivate = keyboard_check_pressed(vk_space);
if(keyActivate)
{
var inst = collision_rectangle(oPlayer.x+3,oPlayer.y+3,oPlayer.x-3,oPlayer.y-3, oCivilian2, false, false);
if (inst != noone)
{
DialogueCode(text1,text2,text3,text4,response1,response2);
}
}

This is the code contained within DialogueCode

// @desc execute script with argument array
/// @returns any
/// @arg args[]
/// @arg args[]
/// @arg args[]
/// @arg args[]
/// @arg args[]
/// @arg args[]
var _obj = oRespond;
with (instance_create_layer(0,0,"Player", _obj))
{
text1 = argument[0];
text2 = argument[1];
text3 = argument[2];
text4 = argument[3];
if(instance_exists(other)) originInstance = other.id else originInstance = noone;
response1 = argument[4];
response2 = argument[5];

ds_list_add(textScript,text1);
ds_list_add(textScript,text2);
ds_list_add(textScript,text3);
ds_list_add(textScript,text4);
ds_list_add(responseScript,response1);
ds_list_add(responseScript,response2);
for(var i = 0; i < ds_list_size(textScript); i++)
{
var val= "";
val = ds_list_find_value(textScript,i);
}
}


This is the step function in the object oRespond


lerpProgress += (1 - lerpProgress) / 50;
textProgress += global.textSpeed;

x1 = lerp(x1, x1Target,lerpProgress);
x2 = lerp(x2, x2Target,lerpProgress);
if (counter % 2 == 1)
{
keyUp = (keyboard_check_pressed(vk_up)) || (keyboard_check_pressed(ord("W")))
keyDown = keyboard_check_pressed(vk_down) || keyboard_check_pressed(ord("S"));
responseSelected += (keyDown - keyUp);
var _max = 1;
var _min = 0;
if (responseSelected > _max) responseSelected = _min;
if (responseSelected < _min) responseSelected = _max;
}

if(keyboard_check_pressed(vk_space))
{
counter+=1;
show_debug_message(counter)
for(var i = 0; i < ds_list_size(textScript); i++)
{
var _messageLength = string_length(textScript[|i]);
}
if (textProgress >= _messageLength)
{
instance_destroy();
if(instance_exists(oRespondQueued)) {
with (oRespondQueued)
{
left --;
}
} else {
with (oPlayer) state = lastState;
}
} else {
if (textProgress > 2)
{
textProgress = _messageLength;
}
}
if(counter % 2 == 1)
{
if(keyboard_check_pressed(vk_space))
{
_i++;
show_debug_message("Working");
responseSelectedTrue = responseSelected + 1;
}
}
}

This is the Draw_Gui in oRespond

// text
//response
NineSliceBoxStretched(sTextBox, x1,y1,x2,y2, 0);
draw_set_font(fText);
draw_set_halign(fa_center);
draw_set_valign(fa_top);
draw_set_color(c_black);
if (counter % 2 == 0)
{
if (responseSelectedTrue==1)
{
var _print = ds_list_find_value(textScript,1);
response1 = true;
} else if (responseSelectedTrue == 2){
var _print = ds_list_find_value(textScript,2);
response2 = true;
} else {
var _print = ds_list_find_value(textScript,_i);
}


draw_text((x1+x2)/2, y1+8, _print);
draw_set_color(c_white);
draw_text((x1+x2) /2, y1+7, _print);
} else if (counter % 2 == 1) {
var _print = "";
for (var i = 0; i < ds_list_size(responseScript); i++)
{
_print += "\n";
if (i == responseSelected) _print += "--> "
_print += ds_list_find_value(responseScript,i);
if (i == responseSelected) _print += " <-- "
}
if(keyboard_check_pressed(vk_space))
{
responseSelectedTrue = responseSelected;
}
draw_text((x1+x2)/2, y1+8, _print);
draw_set_color(c_white);
draw_text((x1+x2) /2, y1+7, _print);
tickets--;
}

And this is the queued that deletes the textbox, I set left to 5

/// @description Insert description here
// You can write your code in this editor
if (left >= 0)
{
instance_change(oRespond, true);
}
 

Attachments

Nidoking

Member
for(var i = 0; i < ds_list_size(textScript); i++)
{
var _messageLength = string_length(textScript[|i]);
}
You do things like this sometimes. What is this meant to accomplish? Okay, you now have the length of the last message in the list, after having thrown away all of the earlier ones you calculated. There's another one:

for(var i = 0; i < ds_list_size(textScript); i++)
{
var val= "";
val = ds_list_find_value(textScript,i);
}
This literally does nothing and takes its time doing it. I think you may find the problem more easily if you clean things up. Whatever this does, you can probably do it in about half the code, and properly indented as well.
 
F

FawkingAwesome

Guest
You do things like this sometimes. What is this meant to accomplish? Okay, you now have the length of the last message in the list, after having thrown away all of the earlier ones you calculated. There's another one:



This literally does nothing and takes its time doing it. I think you may find the problem more easily if you clean things up. Whatever this does, you can probably do it in about half the code, and properly indented as well.
Yeah those were from previous attempts to get it working, I removed the for loop in the first example and just set the element to whatever counter is equal to, and removed the second example from my code. I still am severely confused about why counter loops instead of incrementing, thanks.
 

Nidoking

Member
Where is counter even getting its initial value? I see counter += 1 in there, but no initial setting.

And for the love of programming, PLEASE put your code in CODE blocks with indentation if you want people to read it.
 
Top