Solved: How do I work with String values in Object variable? (not all variable names are allowed)

My use case is there is some sprite that can be shot from each of several players. To know who shot and scored, I introduce an object variable to hold the shooter’s name. it is called owner and has initial value “none”.

When a player shoots, a Bullet is created. When the sprite is hit, that field is compared to different values. If it contains “left” the left player’s score is increased, if it is “right” the right player’s score is increased. Not that rocket science.

However my perception is that both player’s score is increased, regardless of whose bullet hit. I used the debugger to see the value of the bullet variable, and it says “undefined”.

  1. why is it not “left” or “right”, as I intended to assign the value after the bullet is shot?
  2. even if it is undefined although I have “non” as default value?
  3. how come both comparisons for “left” and “right” evaluate to true? I’d expect neither matches and no score gets increased

My gut feeling is several things went wrong. But what exactly?

How do you get the variable value?
For instance variables or Object variables both use the method get Text variable to obtain Text values
Maybe i think you declare the variable as text var but trying to get it numeric value
For Globals and Scene Variables

For Objects Variables

1 Like

Good idea. Although the UI offers those pairs side by side I never bothered for the text version.
Now I tried to just set the values and check with the debugger. And although in the UI I read
Change the text of variable owner of Bullet: set to "right"
(and a similar item for the other players) I still always saw undefined in every Bullet.

Then I removed that assignment completely, to see what the initial value is. And that also was ‘undefined’, although in the variable definition window the name is owner and the value is "none".
Not even the default value gets assigned…?

Just out of curiosity I changed the default value "none" into none, but the debugger still shows undefined.
Then I changed the default value to 7 (numeric this time, and again the debugger shows undefined.

Is there a general problem in GDevelop with object variables? I am running GDevelop 5.0.0beta92. Ah, 93 is out so I will try that, too…

I downloaded the new GDevelop IDE. It claims to be the beta93 but the release note dialog lists beta94 changes already.

Here I see the exact same behaviour. Either I hit a bug or I am still doing something wrong, and that is just for definition and assignment of text into an object variable.

Here are examples of instance variables:
There is no problem with them.

1 Like

You are right. I just created a project from scratch, with a scene, a sprite and both a string and numeric variable and they were displayed correctly in the debugger.

Taking a closer look at my game it seems other variables in the same sprite are handled to expectations:

Should it be possible some variable names are better suitable than others?

A good practice when asking for help in the forum is to show at least the part of the events concerning the issue.

Right now I removed all the event code that might be concerning the variable.
This is just about a text variable that should have the inital value “none”, as you can see from an above screenshot but the debugger never shows that value got assigned.

1 Like

Yes, we should still have seen the events in order to rule out the possibility that some event is triggering this behavior. Otherwise, you are deciding that the events definitely had nothing to do with the problem and that the result of the variable is a system-level issue, in which case (if you are convinced of it, since you do not show the events, which is strange since you are asking for help) you should report it as a system bug in the appropriate section of the forum for it.

Ideally, when asking for help a version of the project should be shared, the minimum is a picture of the events.

Anyways, I’m glad you’ve solved the problem by starting everything from scratch in a new project file (meaning that something in the events or project settings was causing the problem, indeed).

So meanwhile I was able to solve the problem. In essence I did three things:

  1. the assignment of values was changed to use text value assignment (suggested by UlisesFreitas)
  2. the value check was changed to use text value retrieval/comparison (suggested by UlisesFreitas)
  3. the variable name was changed from owner to shotby

I think I found a bug, and I isolated it into a separate minimal game. It is visible here now:

So does the bug occur when an object variable is named “owner”?

That is my perception, yes. Not knowing the root cause I would not exclude other names are impacted as well. Did you have a look at the events in the code I disclosed?

Confirmed on my side, undefined pops up when owner is used, even with global variables.

1 Like

You could add that statement to the github issue.

1 Like