Local function with optional parameters in AGS 3.3

Started by homelightgames, Fri 15/08/2014 22:34:37

Previous topic - Next topic

homelightgames

So, I've been trying to create a function in a room script that has optional parameters.  After looking through the forums and online help I found that I should be able to import the function before it is defined in the room script:

(this is only an example and not my actual code, but it's the same principle)
Code: ags

import function changeSprite(int sprNumberStart, int sprNumberEnd = 2);

function changeSprite(int sprNumberStart, int sprNumberEnd)
{
    object[1].Graphic = sprNumberStart;
    object[2].Graphic = sprNumberEnd;
}

In this example "sprNumberEnd" would be the optional parameter. Unfortunately, when I try this, it gives me a "Variable 'changeSprite' is already imported" message, which makes sense as an error, but from what I read it should work.

I don't know if I'm doing something wrong, or if AGS 3.3 is different than 2.72.  Any thoughts or ideas?

Thanks in advance.

AnasAbdin

Shouldn't this be
Code: ags
import void changeSprite(int sprNumberStart, int sprNumberEnd = 2);

Crimson Wizard

#2
@visionmind, I just copied your code to an empty project, and that compiled fine.
Might be something else.


E: Sorry, it works in GlobalScript but not room script, for some reason... :/

homelightgames

@AnasAbdin, yeah I tried that (to copy the forum post I found), but the same error is thrown.  Not only that I don't know what 'void' means or does, and couldn't find its definition in the manual.

@Crimson Wizard, Yeah, I was hoping I could do this in a room script, but I'm not sure if it is possible, or if it is supposed to be possible.

AnasAbdin

It's used when the function doesn't return a value...

Crimson Wizard

Quote from: visionmind on Fri 15/08/2014 23:17:34
@Crimson Wizard, Yeah, I was hoping I could do this in a room script, but I'm not sure if it is possible, or if it is supposed to be possible.
I do not see a reason why this should not work in the room script if it works in global script; also the error message is weird (it mentions some "variable", although we have function).
Might be a bug in compiler.

homelightgames

@AnasAbdin, ah, thanks, that's good to know.

@Crimson Wizard, okay that's good to know.  I guess, at least, I wasn't doing anything wrong.  I'll try to find a workaround for it, or something.

Thanks for your time, both of you.

visionmind

Crimson Wizard

Quote from: visionmind on Sat 16/08/2014 00:06:12
@Crimson Wizard, okay that's good to know.  I guess, at least, I wasn't doing anything wrong.  I'll try to find a workaround for it, or something.
The "workaround" is to put the function into global script and "import" line in global header. This way you will be able to use it from any room.

homelightgames


SMF spam blocked by CleanTalk