Fangames > Game Design

Tutorial: Adding a Sunpossible Mode to Your Game!

(1/1)

klazen108:
Hey guys,

After helping Piece & Para out by adding Sunpossible to Cultured, I thought I'd share the information in case anyone else felt like they wanted to add it to their game. This tutorial assumes you're using the Yuuutu engine, but the concept is easily extendable once you know what you're doing.

Oh, and for you plebs that don't know what Sunpossible mode is... it's a mode inspired by IWBTChuratch; you get a set amount of lives and that's it - every move you make counts.

Sunpossible Functionality Scripts

I've provided some scripts below; just make some new scripts in Game Maker, name them the bold names, and copy-pasta the code into the script. These make it easy to access the various values associated with Sunpossible mode, so you don't have to worry about the underlying code when you're designing your game.

sunAskForLives

--- Code: ---global.lifeCount = min(floor(abs(real(string_digits(get_string("How many lives?","50"))))),1000000);

--- End code ---
The first thing you might be saying is "WTF why so many parentheses dude" and to that I say... blame Paragus for breaking my code :Kappa:
When you call this function, It will prompt the player with an input box, asking them how many lives they want to play with. The reason there's so many extra functions in there is to make sure you get a nice number out of it, even if the player enters "-3.14159KappaFace" It takes all the digits out of the string the player entered (string_digits), makes it a number (real), makes it positive (abs), drops the fractional part (floor) and makes sure it's less than one million (min) - so if the player entered "-3.14159KappaFace" they would get 314159 lives.

Call this function whenever you want to ask the player how many lives they want - I recommend the warpStart's Player Collision event, but anywhere else that makes logical sense works too.

sunIsSunpossibleMode

--- Code: ---return global.lifeCount > 0
--- End code ---
This function is just a simple way for you to see if it's sunpossible mode or not - If you never set global.lifeCount, it'll be zero, and we consider that not sunpossible mode.

sunGetRemainingLives

--- Code: ---return global.lifeCount - global.death[global.savenum]
--- End code ---
This one's pretty simple too; call it to get the number of lives remaining.

sunSaveLives

--- Code: ---var tem;
tem = global.lifeCount;
file_bin_write_byte(argument0,floor(tem/10000));
tem -= floor(tem/10000)*10000;
file_bin_write_byte(argument0,floor(tem/100));
tem -= floor(tem/100)*100;
file_bin_write_byte(argument0,tem);
--- End code ---
This is a copy of the code from saveGame that saves the player's position. This allows you to save values greater than 255; you can save up to 1,000,000 in fact ;)

sunLoadLives

--- Code: ---global.lifeCount = file_bin_read_byte(argument0)*10000;
global.lifeCount += file_bin_read_byte(argument0)*100;
global.lifeCount += file_bin_read_byte(argument0);
--- End code ---
This is the reverse of the script above - it rebuilds the life count from the file in the opposite way that we encoded it.

Hooking Sunpossible Mode Into the Engine

Now that you've got your supporting scripts, let's hook them into the engine:

In the saveGame script, right before the call to file_bin_close, add this code:

--- Code: ---sunSaveLives(f);
--- End code ---
And that's all you have to do to save your life count to your save file!

In the saveExe script, right before the call to file_bin_close, add this code:

--- Code: ---sunLoadLives(f);
--- End code ---
And again, your life count has been loaded from the save file! MAKE SURE you save & load all the values in the same order - Game Maker doesn't know what each value in the file is, it relies on you to load them in the same order you saved them.

In the saveExe script, at the very end, add this code:

--- Code: ---if (sunIsSunpossibleMode() && (sunGetRemainingLives() <= 0)) {
    with (player) instance_destroy();
    room_goto(rSunpossibleEnd);
}
--- End code ---
This code checks as soon as you load the game (or when you press R after you die) and if it's sunpossible mode and you're out of lives; it sends you to a room to be used as a "permanent game over" room, which I named "rSunpossibleEnd". You could do anything else here, like deleting the save file or whatever, but this is the condition that cheks to see if the player is out of lives

In the World's "Press R-key" event, add this code:

--- Code: ---if (sunIsSunpossibleMode() && room != rSunpossibleEnd && instance_exists(player)) {
    global.death[global.savenum] += 1;
    saveDeathTime();
}
--- End code ---
This code is optional. It forces the player to lose a life if he's still alive when he presses R - perfect for those m9 traps :Kappa:


And that's all you should have to do to implement Sunpossible mode! Let me know if there are any bugs; I just typed this all directly into the post and so it's possible I typoed something :Kappa:

lemonxreaper:

--- Quote --- so if the player entered "-3.14159KappaFace" they would get 314159 lives.
--- End quote ---

Wouldn't they get 3 since the floor code would drop the rest of the numbers after the decimal point? or am I misinterpreting some of the code, such as real.

klazen108:
string_digits() will remove everything that's not 0,1,2,3,4,5,6,7,8 or 9; which means the negative sign and the decimal point won't be included. Technically that would make abs() and floor() unnecessary, but I figured why not include them just in case ;)

lemonxreaper:
haha yeah that stuff threw me off :atkHappy:
Never used string_digits before.

Starz0r:
Another Nice Tutorial.  :atkHappy:

Stickied! :BloodTrail:

Navigation

[0] Message Index

Go to full version