How can I dynamically create a character

Previously due to lots of help from forum members especially @Keith_1357 I was able to create a dynamic player choose screen. Now i want the varying player character to appear on the following scene. But it doesnt work. Here are the, global, scene and object variable screens along with relevant event sheet parts.

Global Variables

Here are my second scene variables:

The object empty button variables go like this

The relevant parts of the event sheet look like this:

All help will be appreciated!

If you want to save the name and tower between scenes, you could set them to global variables.

Setting the them in the variable screen as child.tower and child.name literally sets them to “child.tower” and “child.name” not to the values in the corresponding variables. Plus, the child variables are scene variable not global.

To use name and tower in other scenes, you could create 2 global variables with those names and set them from the 1st scene. Either when the selection buttons are clicked or the button to start the next scene.

If you want to access the array though, you would need to put the array’s index number into a global variable that’s a number.

If the global variables was player you would get the array values with

GlobalVariableString(Global[GlobalVariable(player)] .Name)
GlobalVariableString(Global[GlobalVariable(player)] .Tower)

Or using the new variable method, if the variables are declared with any initial value in the global variable screen you could simplify it because you don’t need to use the GlobalVariable part for them.

Global[player].Name
Global[player].Tower

well i sort of tried that out with the following event but it didnt work

basically i need the condition, global.name[id] and the action, create object global.tower[id]. i cant figure out the expressions for the same. can u help?

If you assign the array’s element or index to a global variable. You can use that variable to get the tower and name and any other child of that array element.

Here’s are global variables.

and my scene
image

This would act like the previous examples. create buttons , assign the name to the button and the element or index number to the button variable named Index.

When he button is clicked, it sets the global variable PlayerID to the button variable. That variable can be used to get the tower or name. In my test project, I just added the name and tower to a text object.

Try me:

is this modification ok?

also can you give me any testing and debugging tips or a quickstart?

It looks good. Does it work?

As far as tips. I can’t really think of anything. You have the debugger where you can check for errors and variable and other values.

https://wiki.gdevelop.io/gdevelop5/interface/debugger/#game-debugger-and-profiler

it aint working across scenes, heres what im doing in the first scene

in the following scene i try to create the tower by using this code

heres the towers group

and to refresh this is the global variable list:

what am i doing wrong?

I’m not sure where tower index fits in. If you set the playerIndex to the array index then you can retrieve the values from that element using that variable.

(simplified code, unless you use he new system, you need the GlobalVariableString(). and other variable evwnts)

Global[PlayerIndex].Tower
Global[PlayerIndex].Name
Global[PlayerIndex].Ammo

i think im making a mistake in this line

i tried putting it out of order, changing layer, but nothing works. And a heartfelt gratitude and thanks for your patience and for helping me out!

1 Like

I think it should be GlobalVariableString(Global…

The children Tower and Name are strings, so you need to use the string version. As witten it’s trying to get a string as a number and then convert it to a string.

The index is a number but the children of the array of structures is a string. When it can’t convert the string to a number, I belive it defaults to 0 which is the convert to a string equivalent to using “0”.

I’m also unsure about PlayerIndex inside the braces or brackets. The new variable syntax should work with just [PlayerIndex] as long as PlayerIndex is in the Global Variable screen. Otherwise, it’s [GlobalVariable(PlayerIndex)]

So, using the older way, I believe it should be

GlobalVariableString(Global[GlobalVariable(PlayerIndex)].Tower) 

the expression shows a syntax error

Sorry, I missed the closing parenthasis. Move that last ) to the end. I’ll edit my last post. It’s tough to tell when you’re typing on a phone. That’s why I like to test my answers.

I believe this is good.

GlobalVariableString(Global[GlobalVariable(PlayerIndex)].Tower)

:slight_smile: yes, its working now

1 Like