1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

Is it possible to place a "creating code" on an object that doesn't exist yet?

Discussion in 'Programming' started by Iniciante, Dec 4, 2019.

  1. Iniciante

    Iniciante Member

    Joined:
    Dec 31, 2018
    Posts:
    16
    I was creating a text box system for NPCs where when the player presses "S" near an NPC he creates obj_text and inside the obj_text Create Event has the text script that appears in the text box.

    This works perfectly, but is it possible to simplify it? Or do I have to create multiple text objects for each text?

    Code:
    //code inside the NPC
    if distance_to_object(obj_player) < 75 && distance_to_object(obj_player) > 0 {
    if (keyboard_check_pressed(ord("S")) && global.text1 = false){
        global.text1 = true
        instance_create(x,y,obj_text)
        
    }
    }
    Code:
    //obj_text create code
    
    y=view_yview[0]+1;
    x=view_xview[0]+1;
    
    instance_create(view_yview[0]+328,view_yview[0]+66,obj_next_talk)
    
    tm=2;
    con=0;
    post=1;
    
    alarm[0]=tm;
    fin="";
    
    scr_test_text() // This is where the NPC texts are
    
    esc=false;
    
    
    Code:
    //obj_text alarm
    
    var cop;
    cop=string_char_at(text[con],post);
    fin+=cop;
    post+=1;
    alarm[0]=tm;
    
    
    
    if (cop!=""){
    audio_play_sound(snd_text,10,false);
    obj_next_talk.visible=false
    }else {obj_next_talk.visible=true}
    
    
    if (cop=""){esc=true;}
    Code:
    //obj_text draw event
    
    draw_self();
    draw_set_color(c_white);
    draw_set_font(fnt_text)
    draw_text(view_xview[0]+15,view_yview[0]+15,fin)
    
    
    Code:
    //key press Z event
    
    if (esc) {
    
    if (con<=maxi) {
    con+=1;
    post=1;
    cop="";
    fin="";
    alarm[0]=tm;
    esc=false;
    }
    
    if (con>maxi)
    {
    instance_destroy()
    global.text1 = false
    with (obj_next_talk) {instance_destroy()}
    }
    }else
    {
    fin=text[con]
    post=string_length(text[con])+1
    }

    Code:
    //scr_test_text
    
    text[0]="Hey look it's a test menssage! I hope that this#will work without trouble."
    text[1]="I'm working on it for a few days... if it doesn't#work i will be frustrated."
    
    maxi=1;
     
  2. Meowanator

    Meowanator Member

    Joined:
    Mar 21, 2017
    Posts:
    72
    Are you trying to use the same script for multiple NPCs and texts? You can pass through the strings into the obj_text individually from each NPC when the obj_text is created.
    Code:
    var textbox = instance_create(x,y,obj_text);
    textbox.text[0]="some text here";
    textbox.text[1]="some other text";
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice