How to make car add points/laps as it goes over finish line? - game-maker

I am programming a simple top-down car game on game-maker where the purpose of the game is to get as many points/laps by driving the car around the oval shaped track.
The car drives with speed = 5 and changes direction with the code direction = direction + 2;
image_angle = direction;
If you couldn't tell already, i'm new to coding however i have been searching for a solution for at least a fortnight and haven't found anything- How can i make the game add a point every time that the car goes through the finish line? I suppose there would have to be a collision event between the car and the finish line but i do not understand the code that would take place in order to add a point and if I did code the game to add a point i anticipate that there will be a bug where by several points will be added (instead of 1) when the car goes over the finish line as it is "colliding" the whole way as it goes over.
How can I make a point be added every time the car goes around the track?
If you need any further information i'd be glad to help. Thank-you.
With Kake_Fisk's help i set up 3 midway lines around the track and created a collision event between the car and the midway lines: prntscr.com/c4uecl - the problem is that add_point doesn't actually do anything and i would be glad to have some help. Another problem i am encountering is that he car was originally meant to turn with the mouse button 1 but when i tried that the car only turned when i aimed on it if you get what i'm saying, do you have a solution for that also?

obj_car:
creation event:
points = 0; //instance variable
step event:
if(place_meeting(x,y,obj_line)) {
points += 1;
}
if(points == 3) {
room_goto(rm_win);
}

First, you have an other problem you need to think about. What stops the player from just driving forth and back over the goal line? You would need to add invisible lines throughout the track. If all these lines have been passed, you know the player has driven the car through the whole track.
You want to trigger some code once, when the car hits the goal line. By utilizing the previous system, this can be done quite easily. In collision with goal line, use something like this:
// If all the invisible lines in the track has been visited
if (midway_lines == 3)
{
// Sets the variable back to 0 so this piece of code only gets executed once
midway_lines = 0;
add_point();
}

Related

Hi, I need an algorithm to tell a sprite to change the image when the in-game text finishes appearing (in GameMaker studio 1.4)

I need an algorithm to tell a sprite to end as soon as the text finishes appearing, basically I need to make a cutscene which describes the beginning of a story within the game, but since I'm using gamemaker, I don't know how to do it, can someone help me?
For cutscenes and automated image sequences you usually have some sort of variables, that handle the states and counters for displaying sprite's sub-images.
But firstly I like to use mainly two main time-related variables in my controller object usually named sys (counter and counterTime):
c = 0; // and for stepEvent: c += 1; // so it goes +1 per step
ct = 0; // and for stepEvent: ct+= 1 / room_speed; // so it goes +1 per second
During the cutscene you might want to stop everything else that moves:
is_cutscene = false; // objects might not be able to move when this is true
// you still have to implement that by yourself
// player can't move if is_cutscene, nothing happens!!
(except cutscene buttons etc...)
So now when the player gets to a 'cutscene'y object and triggers some cutscene_1 = true eg. you can have the image/text/sound/animation object come to life/create/active... That object might have in the create event:
duration = 6; // 6 seconds the scene lasts
start_time = sys.ct // and in Step: if sys.ct > (start_time + duration)
// then -> cutscene advance/destroy
Ofcourse - this is super simple and now you could only say implement:
walk close to a pop-up object
show an image for 6 seconds
it dissappears
And it might not be big trouble to implement it... BUT THIS may also be the foundation for more advanced multi-step sequence and cuts.

Enemies Overlapping in Game Maker: Studio, How Do I Fix This?

The AI of my enemies that I made for my game is simple. They just follow the player (more precisely, they look in the direction of the player and go forward)
Step Event:
if (instance_exists(obj_player)){
direction = point_direction(x,y,obj_player.x,obj_player.y);
}
speed = spd;
But they keep on overlapping each other and go on top of the player. I've tried researching but all the forums said was to use place_free() and xprevious & yprevious, but I have no idea how to use them. How do I fix this?
Thanks :)
You can read about this on the gamemaker documentation : https://docs.yoyogames.com/source/dadiospice/002_reference/movement%20and%20collisions/collisions/place_free.html
basically, what you want to do is avoid moving your instance if that means causing a collision. x_previous and y_previous will be used to cancel the move by going back to the previous position.
But I think it's better to check the place before moving, so I would add at the end of you script :
if (place_free(x+hspeed, y+vspeed)) speed = spd;
else speed = 0;
that way, the ennemy will stop instead of stepping above an other instance.
A little upgrade would be the following : if there is a collision detected, check if you can move along a single axis instead (x or y) and do it.

GML -> check for a colliding instance's variable, then do action

I've been trying to do some GML scripting but got totally stuck at some point.
I want enemies to attack my main character but without overlapping. So, I would say.
//enemy is moving left-to-right...
if place_meeting(x+1, y, enemy){ //if there's a collision with another enemy
if (other enemy).is_attacking{ // ???
// checks if the colliding enemy is attacking, if true, it should attack as well...
is_attacking=true;
}else{
//walks
}
This is an image that describes what I'm trying to get (note that enemies know they should be attacking even though they're not in direct contact with the main character, just because an enemy besides is attacking)
I was finally able to do it thanks to the instance place function.
I will post the code in case someone need somethin similar
if place_meeting(x+5, y, malo){ //malo means enemy on spanish, i use to write multilingual code, lol :P
var inst;
inst=instance_place(x+15,y,malo); //x varies on sprite size. it basically returns the unique id of the object that's 15 pixels on the right of self (malo)
with (inst){
if (is_attacking){
other.is_attacking=true; //if collided enemy is attacking, then this(other) enemy should attack too. search more about the width statement if you don't catch this part
}else{
other.is_attacking=false;
hspeed=1;
}
}
}else if place_meeting(x+3, y, character){
is_attacking=true;
}else{
is_attacking=false;
hspeed=1;
}
and the result

Pulling out player's position per second in game maker

I am making a game in gamemaker, I need to pull out player's position or coordinates per second in text file or excel sheet or CSV file. I am new to gamemaker and I don't know much about how to pull out information from the game.
I would really appreciate any help in this matter.
Thank you.
Game Maker's File I/O is not the best. However, since there are 30 steps in each second (by default, you can make sure in Room > Settings), you can make a script to record the object.x and object.y every 30 steps (or even every step, for that matter). I'm not sure if you want to input or output from file, so if you explain more I can probably expand on this.
Edit #1
In your first room's creation code, put this:
globalvar playersteps;
playersteps=0;
Then in your player object, put this in a code snippet in the Step Event (replace objectname with the object's name in the code):
var file;
file = file_text_open_write(working_directory + "\log.txt")
if (playersteps) { playersteps -= 1 } else {
file_text_write_real(file,objectname.x)
file_text_write_string(file,",")
file_text_write_real(file,objectname.y)
file_text_writeln(file)
playersteps=30
}
file_text_close(file)

After Effects Expressions - controling comps from main comp using an "Expressions Layer"

THE GOAL I WANT TO ACHEIVE:
Control AE timelines using ONE EXPRESSION LAYER (much like using Actionscript) to trigger frequently used comps such as blinking, walking, flying etc... for cartoon animation.
I want animate a the blinking of a cartoon character. (and other actions, explained below) Rather than "re posting" the comp or key frames movements every time I want a blink or a particular action, I want to create a script where I can trigger the Blink comp to play. Is this possible? (Sidenote: A random blink through entire movie would be nice) but I still want to know how to do this for the reasons below.
Ideally: I would like to create an "Expressions layer" in the main comp to TRIGGER other comps to play. At certain points I would like to add triggers to call frequently used comps that contain actions like.. Blinking, Walking, Flying, Look Left and Right etc...
IT WOULD BE AMAZING IF somehow we could trigger other comps to begin, repeat, stop, maybe reverse, and do this all from one Main Comp using an expression layer.
WHY DO IT THIS WAY?
Why not just paste a comp in the spot you want it to play every time you want such action? Well in after effects if you wanted a "blink comp" to play 40 times in two minutes you would have to create 40 layers, or pate the key frames on that comp 40 times. Wouldn't it be awesome to trigger or call it from one one layer when you wanted it from one expressions layer?
We do something like this in Flash using Actionscript all the time. It would be awesome if there was a method out there to achieve this effect. This would be an OUTSTANDING tutorial and I believe it would be very popular if someone did it. It could be used for a MULTITUDE of amazing effects and could save a ton of time for everyone. Heck, help me figure this out and perhaps I will make a tutorial.
Thank you all ye "overflowing Stackers" who contribute! :)
I found the answer and that is...
IT'S NOT POSSIBLE.
After Effects expressions can not control other timelines. Unfortunately you have to put an expression on each layer you want to affect.
The next best solution, and to achieve something close to what I was asking can be found on this link: motionscript.com/design-guide/marker-sync.html
We can only hope that Adobe will someday give the power to expressions like they did with action-script.
HOPEFULLY SOON! Anyone reading this who works for Adobe please plead our case. Thanks
Part 1: Reference other layers in pre-Comps
Simply replace "thisComp" with "comp("ComName")"
To reference Effect-Controllers between compositions, follow the below formula:
comp("ComName").layer("LayerWithExpression").effect("EffectControlerName")("EffectControllerType")
More In-depth Answer: Adobe's Docs - Skip to the Layer Sub-objects part
As I understand the Adobe documentation, only Layers can be accessed,
not footage. What this means is that you will need to create your
expression link utilizing a pre-Comp. Footage can not access this so
that also means no nulls, adjustment layers, etc.
As an added bonus, if you use the essential graphics panel, you can put all the controllers in one pre-comp, but have the controls available no matter which comp you are in. Just select it in the Essential-Graphics dropdown.
Part 2: Start/End based on other layers within pre-comps:
Regarding the next part where you want the expressions to activate based on other compositions, I recommend using the in-out Point expression.
inPoint | Return type: Number. Returns the In point of the layer, in seconds.
outPoint | Return type: Number. Returns the Out point of the layer, in seconds.
If you utilize the start time overrides you can pull this from:
startTime | Return type: Number. Returns the start time of the layer, in seconds.
Alternate Option:
I would recommend avoiding this as the keyframes are basically referenced as an index, so things can get messed up if you add one ahead of a keyframe you were already using - def incorporate some error handling.
Refer to the Key attributes and methods (expression reference) Here
Part 3: Interpolation & Time Reversal
You can time reverse the layer in the rightclick->time, otherwise this is all interpolation expressions like loop out etc - you can loopOut("FOO") a pre-comp if you not only cut it correctly, but also enable time remapping.
then use this to loop those keyframes;
try{ timeStart = thisProperty.key(1).time; duration = thisProperty.key(thisProperty.numKeys).time-timeStart; pingPong =
false; //change to true value if you want to loop animationn back &
forth quant=Math.floor((time-timeStart)/duration);
if(quant<0) quant = 0
if(quant%2 == 1 && pingPong == true){
t = 2*timeStart+ (quant+1)*duration - time;
}
else{
t = time-quant*duration;
}
}
catch(err){
t = time;
}
thisProperty.valueAtTime(t)

Resources