Page 2 of 2

Re: Local vs Global

Posted: Tue Nov 26, 2024 12:07 pm
by jPV
oceanarts wrote: Tue Nov 26, 2024 10:42 am I see the name$ being made available to p_CharName(), but this is the type of data that should be available across the code in multiple functions. How would that be done while avoiding using a Global variable?
I guess a global variable would be ok for player data, unless you have some kind of main function where it could be local and then passed only to functions that need it.

I would probably make a table for all player data instead of separate variables, be it global or local. And for global variables I've started to have "g" in their names to see clearly the type of the variable later when you have lots of code in the program.

Like:
Global g_player = {name="", age=33, stamina=10}

Now you could init the name field of the player data like this:
g_player.name = p_GetName()

And to print something about the player:
Print(g_player.name, g_player.age)

I don't know what's your final goal, but if you plan to replace the random name generation by entering a name on keyboard, then you probably would like to print questions with p_CharName() and make it to ask a name with p_GetName(), and return the name$ from p_CharName() too. Then it could be g_player.name = p_CharName() in the main level of the code, and so on.

Re: Local vs Global

Posted: Tue Nov 26, 2024 2:35 pm
by oceanarts
Great. Thanks. :)
The idea was to have a few 'prefab' rng based characters and then a text-box to initiate a custom character creator. Just to get an insight into the workings of each approach.