Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411490 Posts in 69371 Topics- by 58428 Members - Latest Member: shelton786

April 25, 2024, 12:51:31 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)GMstudio: How do I make a script use a variable amount of arguments? SOLVED
Pages: [1]
Print
Author Topic: GMstudio: How do I make a script use a variable amount of arguments? SOLVED  (Read 812 times)
Canned Turkey
Guest
« on: February 23, 2017, 05:44:26 PM »

In my textbox system, I am using as many arguments as I can for groups of text in a conversation, like so:

Code:
global.c1 = argument0
global.c2 = argument1
global.c3 = argument2
global.c4 = argument3
global.c5 = argument4
global.c6 = argument5
global.c7 = argument6
global.c8 = argument7
global.c9 = argument8
global.c10 = argument9
global.c11 = argument10
global.c12 = argument11
global.c13 = argument12
global.c14 = argument13
global.c15 = argument14
global.c16 = argument15

I have to use a global variable for each conversation block, so it is a bit redundant.
It's called with the function, so I can easily type out each piece of a conversation like so:

Code:
conversation_text(
'Hey',
'Hello world test'
)

But GMstudio naturally wants all 16 arguments that I check, so I get an error until I fill each argument like this:

Code:
conversation_text(
'Hey',
'Hello world test',-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
)

Is there a workaround to this? I know it looks simple but nothing works that allows me to type out any amount of arguments that I want to for each time I call the script.
« Last Edit: February 23, 2017, 06:03:20 PM by Canned Turkey » Logged
Canned Turkey
Guest
« Reply #1 on: February 23, 2017, 05:55:22 PM »

Nevermind >_>
Logged
ThemsAllTook
Administrator
Level 10
******



View Profile WWW
« Reply #2 on: February 24, 2017, 06:05:30 AM »

If you've found a solution, could you share it here in case someone in the future finds this thread while googling for variadic GML function syntax or something like that?
Logged

Glyph
Level 10
*****


Relax! It's all a dream! It HAS to be!


View Profile
« Reply #3 on: February 24, 2017, 07:32:13 AM »

As far as I know, there is no way in GM Studio to implement any variadic scripts. There are workarounds, of course. You could fill all the arguments like you did, but that's not ideal. In your case, I might recommend just sending in one string with a separator token delineating separate blocks of text, and then just parse it at the start of the script. Something like:

Code:
conversation_text(
 "Hello
  Hi"
)
(In this case the delineator would be the newline token and you could cut out the whitespace until the next token. This would be easier to look at but trickier to implement.

The even easier alternative is for the function to just keep track of some state so that repeated calls add to the conversation. Like:

Code:
conversation_text("Hello")
conversation_text("Hi")

But if you found something better, I'm all ears!
Logged


emptyfortress
Level 0
**



View Profile WWW
« Reply #4 on: February 24, 2017, 09:42:26 AM »

You could also use an array of strings as a single argument to the script, this way you don't have to split the string on the newline character, handy if you need newlines in your text.
Logged
Canned Turkey
Guest
« Reply #5 on: February 24, 2017, 11:33:31 AM »

You could also use an array of strings as a single argument to the script, this way you don't have to split the string on the newline character, handy if you need newlines in your text.

Yes that is the workaround I found. I am using a 16 space 1D array that can take the variable text boxes, and then that fills out the global variables with what it has.

I sorted it out nearly immediately after starting this thread, I was looking at how I have 16 redundant lines and thought "I could just use an array for that." And it was a face-palm since that cleaned up my code but also fixed my issue.

Thank you all for your quick replies as always.
Logged
mokesmoe
Level 10
*****



View Profile WWW
« Reply #6 on: February 25, 2017, 11:10:55 AM »

Gamemaker studio does support scripts with varying amount of arguments, just use "argument[​#]" instead of "argument#". You can use argument_count to get the amount of arguments passed.

If you want to read more it's in the help file at "Reference\GML Language Overview\scripts"
Logged
Canned Turkey
Guest
« Reply #7 on: February 27, 2017, 07:07:28 PM »

I did find that, but argument[n] seemed to not properly render strings.
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic