transform an instance variableto a global variable

Get help using Construct 2

Post » Mon Apr 22, 2013 3:31 pm

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?
HARMLESS SIGNATURE THAT IS VIEWED BY OTHERS AS OFFENSIVE AND CONTROVERSIAL
B
14
S
3
G
3
Posts: 356
Reputation: 4,362

Post » Mon Apr 22, 2013 3:45 pm

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.
B
24
S
8
G
7
Posts: 756
Reputation: 7,202

Post » Mon Apr 22, 2013 3:53 pm

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.
HARMLESS SIGNATURE THAT IS VIEWED BY OTHERS AS OFFENSIVE AND CONTROVERSIAL
B
14
S
3
G
3
Posts: 356
Reputation: 4,362

Post » Mon Apr 22, 2013 4:00 pm

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! :)
B
27
S
8
G
5
Posts: 429
Reputation: 6,902

Post » Mon Apr 22, 2013 4:02 pm

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 :)
B
24
S
8
G
7
Posts: 756
Reputation: 7,202

Post » Mon Apr 22, 2013 4:22 pm

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 :)
B
87
S
18
G
9
Posts: 2,455
Reputation: 14,834

Post » Wed Apr 24, 2013 7:00 am

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.
HARMLESS SIGNATURE THAT IS VIEWED BY OTHERS AS OFFENSIVE AND CONTROVERSIAL
B
14
S
3
G
3
Posts: 356
Reputation: 4,362


Return to How do I....?

Who is online

Users browsing this forum: alextro, h1k3, Unconnected and 0 guests