Why is this parameter in a function (to reference a variable) not working?

Why is this parameter in a function (to reference a variable) not working?

I’ve got quite a few variables that start at 0 and may need to be added to - Bet1, Bet2, Bet3, etc.

I’m trying to make a fairly simple function to call to add to that variable by passing in the name of it as a parameter (as well as one other parameter to use as a maximum value.)

It’s not working when I use the parameter name and I’m not sure why. I’m sure I’m just doing something stupid, but I’m not sure what.

Here’s how I’m calling it:

aaacallingfunc

Here’s the function: (I realize I’m trying to do the same thing twice as pictured, but I’m just trying to show a comparison of what works and what doesn’t in the screenshot.)

My apologies if I’m missing something obvious, I’m pretty new to Gdevelop here but I’m especially confused since this seemed to work just fine with a different project and pretty much everything else the same (other than the variables.) Comparing the two I can’t see what’s going wrong.

Any help/suggestions much appreciated!

What are you trying to achieve here?

Not exactly sure what you mean, in a nutshell I’m trying to pass along the name of a variable to a function as a parameter, and have that variables valued changed within that function.

I call a function and pass in the parameter ‘Bet1’ - and the scene variable ‘Bet1’ increments.

If you mean the grand scheme of things, it’s for a roulette thing I’m working on - and there’s 60+ betting spots someone can click on. Using a function seemed more appropriate than adding basically the same code to 60 different buttons, especially if I have to make changes/additions.

Check if “BettingSpot” a string or something
Also, can you show the whole code in the function
and also the use of BettingSpot

To simplify a made a new project with just the issue I’m having. Here’s the whole thing:



Running it with the debugger, it’s adding 5 to a new variable called ‘BettingSpot’ rather than ‘Bet1’ which I was trying to reference using the parameter BettingSpot.

So clearly I’m just referencing it incorrectly, but what’s the proper way?

Are yoi trying to pass the value of the variable to a parameter, or the name of a variable itself to a parameter?

The latter isn’t possible in general, as fae as I know.

The name of the variable to act on in the function :frowning:

If it’s not possible it isn’t, it just seems like if I can put “Bet1” in the variable spot for an action there would be a way to pass that in via a parameter with the value “Bet1” as well… But admittedly I still hardly know what I’m doing here.

Well, you can’t pass the name that way, but you’re also specifically calling “Change the scene variable “BettingSpot””

Right now, your function actions aren’t interacting with the parameter, nor is your main event doing anything with the parameter BettingSpot.

Instead of "Change the scene variable “BettingSpot” it should probably be “Change the scene variable “GetArgumentAsString(“BettingSpot”)””

See if that helps. Also you probably want Trigger once on a bunch of this ,otherwise it’ll infinitely add 5.

1 Like

Thanks SS, I’ve tried GetArgumentAsString and unfortunately it doesn’t do anything. (The fact that the ‘Variable’ field doesn’t seem to have any validation like the ‘Value’ field does leads me to believe that anything you type in ‘Variable’ is taken as a literal, no expressions allowed.)

I guess I’ll have to find a different method here!

If you want edit the value of an variable you need pass the name of this variable directly. Because the Variable field cannot use expressions at least in the function editor as far i know.
In other word here you try to pass the scene variable Bet1 Bet2 Bet3 in the function by the parameter BettingSpot. but it’s not possible to use BettingSpot in an variable feild, and anyways this canno’t work because an parameter need to be called like this:

String parameter:

GetArgumentAsString("myParameter")

Number parameter:

GetArgumentAsNumber("myParameter")

In you case you can do this:

1 Like

Thanks Bouh,

Looks like that’ll be the workaround, though having to repeat the same thing 60 times was a large part of the reason for a function in the first place.

You think it’s reasonable to ask as a feature request that expressions could be used in the Variable field, or is there some reasoning behind it I’m unaware of.

Thanks again everyone for the input.

I don’t know why the variable field is like this, the function editor is pretty new but solid, your goal is legit and you can push on theroadmap for ask to improve this field for sure!

There is an advanced way to do what you looking for.
What you try to do can be possible with JSevent but it’s not the aim here if you didn’t know how code in Javascript and if you didn’t know use the GDJS API.

Push on the roadmap before use this hack.
/**
 * This is the Javascript code is for be added in an action function.
 * This code edit an scene variable by this name, it's for an scene variable NUMBER type only, and add the new value to the previous one (equal to the operator ADD)
 * Like the engine do if the variable doesn't exist this one is created, be sure to edit an variable already exist.
 * This require two parameters to setup yourself in the function.
 * First is the parameter "sceneVariableName", type String.
 * Second parameter "value", type Number. 
 * 
 * Author: @Bouh
 */

const sceneVariables = runtimeScene.getVariables();

const arg_sceneVariableName = eventsFunctionContext.getArgument("sceneVariableName");
const arg_value = eventsFunctionContext.getArgument("value");
const previousValue = sceneVariables.get(arg_sceneVariableName).getAsNumber();

sceneVariables.get(arg_sceneVariableName).setNumber(previousValue + arg_value);

1 Like

I didn’t quite get the hack code, but got the idea of it at least - this worked out.

Same idea I’m sure, thanks again for all the help!

1 Like