GMS 2 Script that returns a value returned from an object



Hi GM community :) Here's a thing I need a bit of help with, and if I can pull this off I'll most likely never have to ask something here again (i know you're all tired of me at this point)

Anyway I need a script that returns a value from an object. For example, let's say I wanted to ask the player to type their name, I'd want to call it like this:
player_name = player_get_name();
write_in_textbox("He goes by " + string(player_name) + ", sir!");

and "player_get_name" would basically spawn an object that the player types their name into, filters it for offensive content then returns it. and then "player_name" would be the returned variable. then write_in_textbox will run using the same method but for displaying textboxes.

However, this doesn't work because everything else in the event is executed while the player would be typing their name! And if I were using a script like this i'd get the variable not set before reading error.
The exact problem is that the script ends as soon as the object is spawned, instead of waiting for the object to be destroyed.

Now, this isn't only for getting the player's name, but many other things my game relies on. The player_get_name thing doesn't exist, I'm only using that as an example of what i need. This will be used for is textboxes, player choices, quicktime events and some features i cannot explain.
I just need to know what to add to the script or the object to make this work.

Thanks again!


In other words, you have to learn to segment your code into parts that don't all run at the same time. One way to do it is to track states and run code depending on state. Your name entry operation would in pseudocode look something like:
if(state == entering name){
    // Code that polls the keyboard and adds letters to name string, and switches to filtering state on certain keypress (like enter).
else if(state == filtering name){
    // Applies filters to given name, returns to name enter if new name needs to be given, otherwise switches to textbox state.
else if(state == displaying textbox){
   // Prepares a string with given name and displays a textbox.
Since the states contain both step and draw code, you'd do this state checkup in both events.