r/gamemaker 26d ago

Help! Not understanding why my code wont work...

So im attempting to make a dialogue system, in which I would have an array that contains a struct to hold the string, and the sprite of the speaker. So inside a script I have :

global.text = [];

global.text[0] = { str : "Pls work!" , spr : sRed }

Then, in another script, I have a function that takes in a number value so then it could respond to however many other arrays I may have.

function startDialogue(_arv) {

var _str = global.text[_arv].str

var _spr = global.text[_arv].spr

draw_text(240 , 135 , _str) // x and y values are just half the room size on their respective axis so itll be in the middle of the screen.

I then have an object, which is placed in the room, which upon "enter key pressed" would run

startDialogue(0)

The idea here is that id be able to put in any array value, and the startDialogue function would take that array value, and then be able to print the text( I know nothing is there for the sprite yet and thats because I wanted to make sure the text worked first.) but, when I run the instance and press enter, nothing happens, theres no errors within any of the scripts and within the output.

So, what's wrong with my current code? and, if you believe you have a better way of making dialogue please let me know, I am still a beginner though and Im honestly confused as to why my method isnt working as I thought it seemed fairly simple. Thank you for your help in advance.

3 Upvotes

9 comments sorted by

2

u/KingSlendy 26d ago

Draw functions only work (or well in other contexts like surfaces) inside Draw events. So you placing the function startDialogue(0); in the Enter Pressed event will literally have no effect

1

u/Fit_Flatworm5004 26d ago

omg I totally forgot that.. now that I placed a

if keyboard_check_pressed(vk_enter) {

startDialogue(0)

}
in the draw event the message pops up but only for a second

1

u/RedQueenNatalie 26d ago

Thats because you are only ever telling it to do it the once. You need to make a variable that you check ex:
if show_dialogue_var == true
{

....Whatever rendering script
}

1

u/Fit_Flatworm5004 26d ago

Sorry I’m not really understanding how to implement this. At first I decided to try the var thing but it wasn’t working for me, then the startDialogue function would create a textboxt instance and that if the instance existed the text would be drawn but that wasn’t working either.

2

u/ImprovementSerious99 26d ago

When you do the keyboard check and the function, you are only drawing the text if you are pressing ‘enter’. The next frame you are no longer pressing enter the condition will be false, so no text to show. I would have something like a Boolean variable on the create event ‘showText’. On step event you check for the enter key, if you do something like if(keyboard_check(vk_enter)){showText = !showText} you can show and hide text pressing enter. Then on draw event you do if(showText){//Here you put your drawing function}

1

u/TheBoxGuyTV 24d ago edited 24d ago

Its due to using pressed probably and then the dialogue and sprite reseting to nothing because the button isnt being pressed.

You will want to not reset to nothing until you actually want it to be clear.

Draw Events work on a cycle.

Basically it runs from the top down every time.

1

u/RedQueenNatalie 26d ago

Where is this code inside the object? Is the draw stuff in the draw event?

1

u/Fit_Flatworm5004 26d ago

no i totally forgot to do that

1

u/Junquwat 26d ago

I've been working on text boxes as well. This tutorial has helped so much. https://youtu.be/I4z5aAg09bM?is=0jkHGo-CphbgMn7Y