0
\$\begingroup\$

When i make a script that excecutes at the beggining and has a var ammo and that finishes excecution, will i be able to call the var ammo in other scripts or it will be deleted? If it will not be deleted, will I be able to change its value on other scripts?

Thanks in advance.

\$\endgroup\$
1
  • \$\begingroup\$ Who calls the script? Or rather, where did you put the call to the script? Is it in the room initialization code or is it in an object's code? That makes a large difference. \$\endgroup\$
    – user64742
    Commented Jun 3, 2017 at 21:43

1 Answer 1

1
\$\begingroup\$

This question can be expanded to almost all modern languages. The short answer is: no, it won't, the long answer is:

In programming, there's a word called scope, a scope is a chunk of code defined by the enclosing statements in the programming language (in most of the languages, they're brackets {}). There can be multiple scopes inside another scopes, creating a hierarchy system.

A scope has 2 types of variables. local and global. Global variables are defined in a parent scope, and can be overriden by local variables. Local variables are only available in that scope or the child-scopes. They get removed once the scope is removed. If you name a loca, variable the same as a global, the global gets overridden but it dodsn't get deleted, nor changed.

In your case, ammo is a local variable to script "A". Once script "A" is finished, ammo gets deleted, and can't be reached by other scripts.

\$\endgroup\$
7
  • \$\begingroup\$ Thanks for your clarification. If so, how can i reach ammo from other scripts? \$\endgroup\$ Commented Apr 3, 2016 at 10:48
  • \$\begingroup\$ Dunno, I'm not a game make developer, there's surely some way to make it global. \$\endgroup\$
    – Bálint
    Commented Apr 13, 2016 at 19:39
  • \$\begingroup\$ I finally found how to. In case someome is reading this you must add a global. Before your name var ex global.ammo = 0; \$\endgroup\$ Commented Apr 14, 2016 at 11:44
  • \$\begingroup\$ @Bálint If you are not a game maker programmer, then please don't assume such things about scope. In Game Maker there is only global scope, object scope, and script argument scope. The last is explicitly defined via the "argument[]" array. So no, ammo will not be deleted. It will either be a global variable or an instances local variable. \$\endgroup\$
    – user64742
    Commented Jun 3, 2017 at 21:38
  • \$\begingroup\$ @TheGreatDuck First of all, I never assumed anything. If I post an answer about an engine, language or framework I don't use, then I research it before. As stated on the main game maker page and I quote "local: these variables must be declared using the "var" function. A local variable is only valid for the event or script resource in which it is created. So GameMaker: Studio will create the variable, use it for the duration of the event and then "forget" it again, meaning that if you try to use it later you will get an "unknown variable" error." \$\endgroup\$
    – Bálint
    Commented Jun 4, 2017 at 20:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .