VG:Miscellaneous (VG1)

From MyProjectFun
Jump to: navigation, search

This page is composed of threads of a previous iteration of the ProjectFUN forums. It is stored here in the hopes that many of our students will find answers to questions they may have during their course(s).

Contents

Adding Points or Money

Student1: I have been attempting to add money to my game and so far I have not had any success. I have tried to add global data that keeps track of how much money the player has, but I cannot get code that adds money to work.

I approached accessing and adding to global data in a similar way to local data but it did not work. The reason why I am using global data is because I want the amount of money the player has to persist through all the levels of the game.

Student2: You are on the right track with the Global Data and adding to it. The code is quite simple.

Lets say the name of the Global Data is MoneyGD and we have a behavior function that deals solely with adding money to that Global Data when the sprite, lets say a coin, collides with the player. Then the code would look something like the following:

if (This->CollisionWithSprite("Player"))
{
   MoneyGD += 5;
}

Of course the name of your player might be different, but the code should be as straight forward as that.

Student1: That appears to be working. However, I am now trying to make a text object that displays the money global data. There is only a spot for local data on the general tab and I do not see any other ways to directly let it use the global data. Is there a fun function that can be applied to the text object or is there a way to make a local data that has the same number as the global data?

Student3: a simple Object Function used by Texts:

This->Value = MoneyGD;

that should do the trick

Student1: Everything appears to be working fine. As a note I noticed that if global data is not attached to something the game will crash just like local with local data.

I am now trying to make text objects that display the max MP and current MP the player has. I have added these numbers in the local data that is used by the player's character. I want to end up with MP that is used when spells are cast, regenerates automatically and is displayed by these text objects. Thanks for the help but I get the feeling I will be coming back soon when another problem pops up...

Student3: Soooo...did you need help with any of the MP/HP jazz? Let me know.

Student1: I messed around with trying to get text objects to display local data for MP, but I am not quite sure what to write in a function for it. Also, do you know of a way to make MP regenerate?

Student3: MP regeneration: Make a function that increases the your MP variable, then set a timer on that function for however long you want. Example:

if (MP < MaxMP)
{
  MP += 1;
}

(set a timer for like...3000 milliseconds to make it increment once every 3 seconds)

As for displaying the local data...

SpritePTR player("player");
This->Value = GetPlayerData(player)->Health;

..replace "player" with the name of the sprite and also GetPlayerData with Get[name of your local data]

Gravity

Student: Okay I know this is probally a stupid question and there is some amazing sprite method that i just happened to miss in the fun library, but how exactly do you get the FUN default gravity to work well with vector direction?

I am trying to make a sprite jump, move left, and move right. The left and right functions go really slow or not at all when Gravity is on, and jump doesn't even work.

I have tried many ways to get this to work, including using translation, mapPosition, worldPosition, a PlayerLD w/ a gravityIndex to check if the player reached the height of the jump (gravity would be disabled while jumping, when reach the height of jump gravity would be turned on), making a simple variable for gravityIndex and just changing it each time the sprite is moved in the y direction to detect for hieght of the jump, simply changing the animation set for jump and the collision data to give the visual of jumping... nothing seems to be working.

I know that i could use the custom simulated gravity function that we created in the platformer, but it seems that can only be done without bieng tedious with State Machines, which i only want to use sparignly since i find Behaviors to work a lot better for me. Does anyone have suggestions on how to get gravity to work for this, or if that is not possible then how to get custom simulated gravity function to work with behaviors?

Instructor: The short answer to:

"how exactly do you get the FUN default gravity to work well with vector direction?"...

is... you can't.

Basically, if the built-in FUN gravity is applied, that takes complete control over the vector direction. The gravity affects both the Y and X components of the vector direction, so anything you set the vec direction to will be overridden.

The reason for this is that ProjectFUN Editor normalizes the vector direction. What that means is, whatever vector direction you set, it keeps it as a vector of length 1 (using the Pythagorean Theorem).

For example, you can set a Vector Direction of (1,0), and it will be (1,0). But if you set a direction of (10,0), ProjectFUN converts that to (1,0).

Similarly, setting a direction vector of (3,4), which has a length of 5, will be stored as (0.6,0.8), which is the same direction but has a length of 1.

ProjectFUN always keeps your direction vector as a vector of length 1 (except 0,0), because then you can set the Speed independently. Think of a character that needs 8-way movement. You can move in directions like (1,0) and (0,-1), but also directions like (1,1) and (-1,1). When you think about it, the vector (1,1) is longer than the vector (1,0). The vector (1,1) has a length of sqrt(2), ~= 1.414. If ProjectFUN didn't normalize the direction vector to a length of 1, you'd have a really hard time having a sprite that maintained a constant speed. And you'd have trouble setting the speed.

This scheme makes it easy for constant speed, but makes it more difficult to apply gravity. Because if you add something to the "Y" component of the vector direction, and then set the vector direction again, the "X" component will ALSO be changed by the normalization process.

So there is really no choice but to keep track of your OWN "directionX" and "directionY" and use them to guide your character if you want custom gravity.

I'll attach a very rough demo of how this works I made years ago -- use left, right, and up to move around. It's not perfect but can give you an idea, if you have questions about the way it works please post them!

Student: Thanks, I actually think I understand it now.

AI

Student: How would you make an AI for Pong that doesnt always win?

Instructor1: You can put the Object Function the AI uses on a delay. By making it so the AI does not update every pass through the game loop, you can make the AI seem less responsive and prone to making judgement errors.

Student: Thanks, But would you know what the code would be? Because I have tried a few and none of them have worked.

Instructor2: Object Functions can be on a timer -- click the clock icon and you can set a millisecond (or game loop/frame) delay.

Instructor3: In 2.0 we can't add a delay to object functions, to my knowledge. What you CAN do is create a state machine with one state that calls the function in the Start Tab. Then create an edge to itself with a timer. That way it calls the function once and loops back to itself.

Lives

Student: How do I get a function to add a life?

Instructor: The simplest way is to create a global variable named "lives" and set its initial value to, say, 3 or 5 (whatever). Then when the player loses, you decrease it:

lives = lives - 1;

When it is zero, the game ends:

if( lives == 0 ) {
  myGame->LevelName("YouLoseLevel");
}

Or whatever your level name is for when the player loses.

Score

Student: How can you transfer a score from the game HUD to a lose screen?

Instructor: Hi Bradley, to transfer score from one level to another, you'll need to use a global variable. All objects that "live" within a level also "die" when that level ends.

If you're currently using a Text Object to keep your score, then you can make a global variable called something like "scoreTemp" or "scoreTransfer". Make it type "int".

Then, when your game level ends, you write the text object's score into the global variable. You can do that on the OnEnd tab in the level properties (that way you're sure it always gets transferred - the OnEnd tab runs just before the level ends). Just right click the level name and go to the OnEnd tab and write the code, something like:

// use whatever your text name is
TextPTR p ("ScoreTextName");
scoreTemp = p->Value;

Then, on the OnStart tab of your lose level, you'd have code similar to:

TextPTR p ("ScoreTextName");
p->Value = scoreTemp;
Personal tools
Namespaces
Variants
Actions
Navigation
Engineering
Game Design
Multimedia Production
VG Programming
Downloads
Toolbox