Lots of games use coins or other collectable objects to increase the score. Using this script you will be able to create many of them, using very little resources. The game will load super fast.
Important: connect the valueprint to the counter object
If something goes wrong with the order of linking, just delete the script object, add a new one and start to link again in the correct order.
The coins.txt needs to be in the root of the 3drad flder (not in the 3drad_res). If you compile your prject, don't forget to copy over the coin.txt to the compild folder.
You only need to adjust the number of coins, and the ammount of points. Rest should wok directly.
If you like this code made by Loop, please donate (go to gamesatnight.com, at the top of the page "Gold members" and subscribe for 10$. You will also get to play some cool 3drad games and have access to the FPS maker for 3drad).
Here is a demo to see it in action:
http://girlsvault.com/collectables.zip// COIN COLLECTOR v.1 by loop -------------------------------
//-----------------------------------------------------------
// INSTRUCTIONS ---------------------------------------------
//-----------------------------------------------------------
// link this script to the following objects, in this exact order:
// - the player
// - the coin skinmesh
// - the counter that keep the score
// - the camera
// - optional: any other object you want to start when a coin is collected, in ex: sounds, particles
//-----------------------------------------------------------
// SETTINGS -------------------------------------------------
//-----------------------------------------------------------
int Ncoins = 20; //how much coins??
int coinsRadius=2; //the radius of the coins
int pointsPerCoin=10; //how much points per coin
// to collect the coins, a vertical intersecting line is used as the player body.
// this line goes from the feet to the head of the player:
int playerFeet=0; //if the center of the player are the feets (like happens with Andro) then this is = 0, else is a NEGATIVE number = distance from center to the feet
int playerHead=2; //the head of the player, this is a positive number: the height of the player
//the path of the file that contain the coordinates for the coins
string coinsFile="coins.txt";
//-----------------------------------------------------------
Vector3 playerLoc;
Vector3[] coins(0);
bool[] collected(0);
Vector3 intersectionPoint1,intersectionPoint2;
Quaternion coinsOrientation;
int i,j;
string filePath,coordinates,coordinateX;
int fileHandle;
void Main()
{
if (iInitializing()) {
coins.resize(Ncoins);
collected.resize(Ncoins);
iObjectImpostersCreate(OBJ_22,Ncoins);
iObjectOrientation(OBJ_22,coinsOrientation);
filePath = coinsFile;
fileHandle = iFileReadOpen(filePath);
if (fileHandle != -1) {
for (i=0; i<Ncoins; i++) {
iFileStringRead(fileHandle,coordinates);
iStringMid(coordinateX,coordinates,0,iStringFind(coordinates,",",0,true));
iStringMid(coordinates,coordinates,iStringFind(coordinates,",",0,true)+1,iStringLen(coordinates));
coins[i].x=iStringVal(coordinateX);
iStringMid(coordinateX,coordinates,0,iStringFind(coordinates,",",0,true));
iStringMid(coordinates,coordinates,iStringFind(coordinates,",",0,true)+1,iStringLen(coordinates));
coins[i].y=iStringVal(coordinateX);
coins[i].z=iStringVal(coordinates);
collected[i]=false;
iObjectImposterHide(OBJ_22,i);
iObjectImposterSet(OBJ_22,i,coinsOrientation,coins[i]);
}
iFileClose(fileHandle);
}
}
iObjectLocation(OBJ_0,playerLoc);
for (i=0; i<Ncoins; i++) {
if (!collected[i]) {
if (iSphereVisible(OBJ_66,coins[i],coinsRadius)) iObjectImposterShow(OBJ_22,i); else iObjectImposterHide(OBJ_22,i);
if (iSphereSegmentIntersect(coins[i],coinsRadius,Vector3(playerLoc.x,playerLoc.y+playerFeet,playerLoc.z),Vector3(playerLoc.x,playerLoc.y+playerHead,playerLoc.z),intersectionPoint1,intersectionPoint2)>0) {
iObjectImposterHide(OBJ_22,i);
OUT_44=IN_44+pointsPerCoin;
for (j=4; j<iObjectHandle(-1); j++) {
iObjectLocationSet(iObjectHandle(j),coins[i]);
iObjectStart(iObjectHandle(j));
}
collected[i]=true;
}
}
}
}