• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

DrawEvent - Help me understand! *SOLVED*

M

Mandarieno

Guest
Hello, I'm working on a NPC dialogue system for my 2d adventure game. And I will go straight to my problem:

I made a parent object (obj_npc_parent) that has a string variable (speach) in it's create event.
It also has a draw event in which it draws the string (speach) in the room. This works.

Then I have two different NPC objects (obj_npc_1 and obj_npc_2) that use the same parent (obj_npc_parent). In their create event they change the string variable (speach) into their own individual sentences.
Now when both NPC's are in the same room, they should both have their own individual string variable (speach), which means both have their individual sentence.

The problem is that they both draw the same sentence into the room.
If I give them an int value instead of a string and draw the int value with the drag and drop system it writes both
numbers at the same place. This confuses me.

I my problem is understandable and thanks for reading.

*EDIT: Also when I draw the strings with DnD it it writes both sentences at the same place.

my code:------------------------------------------------------------------------------------------------------------------------------

*obj_npc_parent*

*create event*
speach = "default";

*draw event*
draw_self()
if (global.tbox == true)
{
draw_rectangle_color(view_xview + 0, view_yview + 96, view_xview + 160, view_yview + 144, 0, 0, 0, 0, false);
draw_set_color(c_white);
draw_text_ext(view_xview + 0, view_yview + 96, speach, 16, 160);
}
------------------------------------------------------------------------------------------------------------------------------

*obj_npc_1*

*creat event*
event_inherited();
speach = "something.";

------------------------------------------------------------------------------------------------------------------------------

*obj_npc_2*

*creat event*
event_inherited();
speach = "blabla.";
 
Last edited by a moderator:

Fabseven

Member
I donot understand where you made a mistake but here is a test project illustrating what you want to test : https://we.tl/Yl9Qlid0WZ
By the way i hope you are not planinng to make one objet per NPC
You could edit the creation code of the objet in the room editor
or in your code create all your npc and modify the speach value.
 
M

Mandarieno

Guest
Thank you! Your test project didnt directly help me but it defiently inspired me and i apreciate the effort. :)
I took away the black rectangle and noticed it actually draws both strings all the time at the same place. So I found out that "if (global.tbox == true)" is the problem
because it is global and activates both variables. So I will fix that now.

"By the way i hope you are not planinng to make one objet per NPC" This is pretty much the first time I'm making a game by myself so I am kinda learning while developing. So also thanks for that tipp :)
 

Fabseven

Member
Yeah i am learning while developing too, sometimes it's quite hard.
Another tipp :
use the function distance_to_object https://docs.yoyogames.com/source/d...d collisions/movement/distance_to_object.html
to draw or not your speach value.

Code:
in creation event : maxrange = 100
in step event : 
range = distance_to_object(obj_player)
if( range <= maxrange)
{
   //draw your text
}
It's easy to code and quite powerful.
You could initialize maxrange to sprite_width or something like that. (meaning the speach will be draw when the player is sprite_width distance from the center of the npc)

Thank you! Your test project didnt directly help me but it defiently inspired me and i apreciate the effort. :)
I took away the black rectangle and noticed it actually draws both strings all the time at the same place. So I found out that "if (global.tbox == true)" is the problem
because it is global and activates both variables. So I will fix that now.

"By the way i hope you are not planinng to make one objet per NPC" This is pretty much the first time I'm making a game by myself so I am kinda learning while developing. So also thanks for that tipp :)
 
M

Mandarieno

Guest
I use place_meeting(x,y,object) because I want my player to face the npc to talk to it.
I wonder, do you know any source that can help me how to understand to make all my npc from code? Currently I'm just creating endless loops the way I try to do it ;d
 

Fabseven

Member
I donot if i understood you but you could do this :
Create a script filling a array

Code:
Script system_tabNPC()
{
    global.tabNPC [1000] = 0
   
    num = 0
    global.tabNPC[num,info_npc.id] = num
    global.tabNPC[num,info_npc.name] = "Robin"  
    global.tabNPC[num,info_npc.posx] = 10
    global.tabNPC[num,info_npc.posy] = 10
    global.tabNPC[num,info_npc.speach] = "Hello Friend !"
    global.tabNPC[num,info_npc.life] = 10

    num = 1
    global.tabNPC[num,info_npc.id] = num
    global.tabNPC[num,info_npc.name] = "John"  
    global.tabNPC[num,info_npc.posx] = 200
    global.tabNPC[num,info_npc.posy] = 10
    global.tabNPC[num,info_npc.speach] = "Hello !"
    global.tabNPC[num,info_npc.life] = 10
   
   <and so on...>
}

and create a obj_systemNPC
create event :
enum info_npc {
    id=100,
    name = 0,
    posx = 1,
   posy = 2,
  speach = 3,
   life = 4
 <others values if you want >
}

system_tabNPC()  //load global.tabNPC
global.nbNpc = array_length_2d (global.tabNPC)
for(i=0 ; i < global.nbNpc ; i++)
{
     npc = instance_create(0,0,obj_npc)
    npc.x = global.NPC[i,info_npc.posx]
   npc.y = global.NPC[i,info_npc.posy]
   npc.name = global.NPC[i,info_npc.name]
  npc.speach = global.NPC[i,info_npc.speach]
   npc.life = global.NPC[i,info_npc.life]
  <others values if you want>
}
of course you have to create a obj_npc object with x,y,name,speach,life,etc in the create event.
If your game contains multiples rooms add room in the enum and in the tabNPC and use the current room as a argument in the script ( you have to add a line like cur_room = argument0)
and so load and create only the NPCs in the room. (call : system_tabNPC( room ))
 
Last edited:
M

Mandarieno

Guest
" npc = instance_create(0,0,obj_npc)" This is very helpful! I have not thought about that you can tell a variable to be a built in function, thanks :)
 
Top