transform an instance variableto a global variable

0 favourites
  • 7 posts
From the Asset Store
Globals 2.0
$3.99 USD
Globals 2.0 stores and group variables. You can also load and save data (variables) from/to JSON files.
  • How can I transform an instance variable into a global variable? Long ago, I noticed that if you make a platformer game,add 3 hp to the main character as an instance variable and ways to hurt yourself, hurt yourself in one layout and then go to another layout, my Hero's HP will be 3, not 2. Now that I am remaking my game, I need to make my Hero's HP a global variable without remaking my event sheet everywhere it mentions Hero's HP. I tried to do the same thing in my other game and I screwed the game up, it's glitchy and not working.(It was a bad game anyway...) So, is there a way to transform an instance variable into a global variable?

  • It seems to me that having health as instance variable makes more sense in your case. So you should look for a way to preserve the value between layouts. You could make player object global for example.

  • But the only object using that instance variable is the Hero. And when the instance variable is used just by one object, it's not just useless, but damaging to use the instance variable. I can use edit the whole event sheet, but I'm afraid that I will f*ck it up.

  • Just have an event that adds the instance variable to a global variable at end of layout, then set the player health to that variable at the start of the layout - easy fixed! :)

  • I don't really understand your point; instance variable is a variable specific to an instance of an object. Health of an instance seems to be textbook use of instance variables :)

  • There isn't without re writing your code every where. I have one temporary solution and one permanent solution for future use.

    Temporary to quick fix your problem

    Global Var HPTransition

    On Layout End

    -- HPTransition = player.health

    on Layout Start

    -- player.health = HPTransition

    OR

    Dictionary Object name Global

    On Layout End

    -- Global.set("HP", player.health)

    on Layout Start

    -- player.health = Global.get("HP")

    Long term solution.

    Whenever creating your game and you either need to check or change a value for players, enemies or objects. Create a Group Object with functions to use as Getters and Setters.

    Player Functions // this is a group

    OnFunction "fPlayer.getHP"

    • return player.hp

    OnFunction "fPlayer.addHP"

    • player.hp + Fn.Param(0) // -num will work as subtraction for add&sub

    Using said functions. Never use any other form of accessing the players.hp. Only use the getter and setter function

    Event collision with enemy

    -- FunctionCall( "fPlayer.addHP", -10)

    Event collision with hazard

    -- FunctionCall( "fPlayer.addHP", -5)

    -- FunctionCall("check death")

    Event collision with health restore

    -- FunctionCall( "fPlayer.addHP", 20)

    OnFunction "CheckDeath"

    -- if(Function.Call(fPlayer.getHP) <= 0)

    ---- do deeath stuff

    As you can see none of your game logic every looks at or modifies your player HP outside of the dedicated functions. This means that if you need to transition your variable player.hp to global hp you can with minimal effort

    Player Functions // after modification

    global var HP

    OnFunction "fPlayer.getHP"

    • return HP

    OnFunction "fPlayer.addHP"

    • HP + Fn.Param(0) // -num will work as subtraction for add&sub

    good luck :) If your project is big. it's worth going over an applying organized code structure. If it's a smaller project, then just hack what you need :)

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Ok thanks. I just realized that I only had 4 events that quoted player's hp, and that I can easily just remake it. But if I ever do the mistake of making Hero's hp as an instance variable and my project will already be too big for remaking the events I will do as you taught. Luckily, it made sense.

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