How do I convert variable type?

0 favourites
  • 7 posts
From the Asset Store
Change delay, create new lines, "backspace" the text
  • I was curious if it is possible to change a variable type without having to change every event associated with that variable.

    From Local to Global.

    From Global to Local.

    While we are on variables can someone tell me the difference in Static and Constant? I am aware that Constant stays the same and Static can be changed, but I am not understanding why there is a need for that. I have never used a Constant, I only use Static. If I don't make an action/event that changes a Static Variable than why would I need to make it Constant? To me it just seems like Constant and Static does the same thing.

    Also in a variable is 01 the same thing as 1? I assume it ignores any 0s before any real numbers.

  • You just drag the variable around to change if it's local or global. Global variables are all the way to the left and local are nested under something else.

    Constant, like you say can't change.

    Static makes the variable like a global in that it keeps it's value and isn't reset every time like locals are.

  • Thank you for the Drag and Drop, that is very helpful.

    I am not understanding your explanation of Constant and Static. When are they reset? When I use a Static I never notice it changing unless an event or action made it. I've never seen a Static change by itself unless it was told to change.

    What is the difference in using a Static and not changing it with Events or Actions VS using a Constant and not being able to changing it with Events or Actions? I mean if I don't need it to be changed why would I try to change it?

  • non Static are reset the next tick, it can change via events or actions, it will simply reset (this can be useful if some cases even though I don't use them much at all).

    Static can change and will keep the new value until being changed again by an event and only then, they won't reset to their default value.

    a constant is useful for organisation, instead of using the number, you use the constant in all the expression that requires it (a speed for exemple) so you just edit the value once and it becomes relevant to all actions that uses them.

  • Depends on the 'scope' of that specific variable.

    The scope of a global is 'use it everywhere in the event sheet(s)'. Roughly said: conditions & actions can see it everywhere in the event sheet.

    The scope of a local is 'use it only in its own event (and subevents)'. Roughly said: conditions & actions can only see it when they are in that particulair event.

    Say you have a global and two events. And you adress the global in the second event.

    If you would drag this global into the first event (making it a local), you create a 'scope' problem.

    Because that second event (where you adressed it as a global) can no longer see that variable anymore, as a (newly made) local it can only be adressed in that first event, no more in the second event. Conclusion: you can not do that, have to recode things, or leave it as it is.

    There are more situations. But Construct is so friendly to warn you.

  • To best show what static does make an event like this:

    +-------------------------+
    | every tick              |
    +-------------------------+
       local number a=0
       local static number b=0
       +----------------------+
       | every tick           | add 1 to a
       |                      | add 1 to b
       |                      | set text to a&" "&b
       +----------------------+[/code:3p392zri]
    
    notice that you'll get the text set to two numbers "1" and an increasing number.  Basically static makes the local save it's value.
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I know the difference between Global, Local, Static and Constant. I was only asking what the point of a Constant was. I can only assume the reason of setting a variable as Constant would be so Construct 2 will prevent the user from changing it with events/actions. I usually just set every variable as a Static and add a description to it.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)