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:
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.
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.
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?
The name of the variable to act on in the function
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.
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:
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.
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);