How to change speed running while running?

16-bit Fighter

Active member
I'd like a player to change his speed running while running if his MP is under a certain number. I unsuccessfully tried the code below in the running anim of a char whose running speed is 18 in his header:

Code:
@script
	void self = getlocalvar("self");
	void MP = getentityproperty(self,"mp");
     	       if (MP<10)
	{ 
		changeentityproperty(self, "running", 14);
     	}
	@end_script

Thanks in advance!
 
Hello 16-bit Fighter!

I tested your script and it worked, but need to consider two points:

1- First, you will need to reset the default running speed if the character regain mp
2 - Scripted running speed will works with decimal numbers and 10 times smaller of the original value. So, a running speed of 14 will be 1.4 and 18 will be 1.8, like the manual says

speedf {float} ~Determines entity's speed. ~This have same effect with 'speed' but this one allows {int} less than 5 even negative value. ~Moreover, decimal values are allowed with this. However its value is 10 times speed's value. For instance, 'speedf 1.5' equals to 'speed 15'.

And here is the code:
Code:
@script
	void self = getlocalvar("self");
	int MP = getentityproperty(self,"mp");
		if(MP<10){
			changeentityproperty(self, "running", 1.4);
     	        }else{
			changeentityproperty(self, "running", 1.8);
		}
	@end_script

I hope it helps!
 
You're right! Thanks Kratus!
I'm surprised though, why is running speed an integer in the header (18) and a float (1.8) in the script?

I can take advantage of this topic to ask for help in order to make my MP system a bit more complex. I want running to consume MP. For the moment my way is to put the command MPcost in a frame but it's not ideal. I tried another way by adding a line about mpset to your script:
Code:
	@script
	void self = getlocalvar("self");
	void MP = getentityproperty(self,"mp");
	changeentityproperty(self, "mpset", 100, 1, 100, -1);
	if(MP<10){
		changeentityproperty(self, "running", 1.4);
     	        }else{
		changeentityproperty(self, "running", 1.8);
     	}
	@end_script
It works but once the char stops running his mp recover rate stay -1 instead of 2. So I wonder if the the best way is to put a script in the header instead.

A last thing I want to implement is to make the run anim delay changed (from 5 to 7 and vice versa) depending on if  the MP is under 10 or not. It'd allow to make a kind of realistic difference with maximum speed running animation.
 
16-bit Fighter said:
I'm surprised though, why is running speed an integer in the header (18) and a float (1.8) in the script?

The float is real value used by engine. The text command is multiplied by 0.1 as it's read in. This predated me. My guess is they just thought humans would rather input a larger whole number.

DC
 
16-bit Fighter said:
You're right! Thanks Kratus!
I'm surprised though, why is running speed an integer in the header (18) and a float (1.8) in the script?

I can take advantage of this topic to ask for help in order to make my MP system a bit more complex. I want running to consume MP. For the moment my way is to put the command MPcost in a frame but it's not ideal. I tried another way by adding a line about mpset to your script:
Code:
	@script
	void self = getlocalvar("self");
	void MP = getentityproperty(self,"mp");
	changeentityproperty(self, "mpset", 100, 1, 100, -1);
	if(MP<10){
		changeentityproperty(self, "running", 1.4);
     	        }else{
		changeentityproperty(self, "running", 1.8);
     	}
	@end_script
It works but once the char stops running his mp recover rate stay -1 instead of 2. So I wonder if the the best way is to put a script in the header instead.

A last thing I want to implement is to make the run anim delay changed (from 5 to 7 and vice versa) depending on if  the MP is under 10 or not. It'd allow to make a kind of realistic difference with maximum speed running animation.

No problem 16-bit Fighter!  ;)

In this case you can use "edelay" adjust, but it will change all animations delay and not only running animation. However, I suggest you to use it in a event that happens anytime, like "ondrawscript" or "keyscript", that will check your mp and adjust speed/delay in real time and working like a "stamina".

Here is the edelay explanation in the manual:
edelay {Mode} {Factor} {Cap Min} {Cap Max} {Range Min} {Range Max} ~Adjusts entity's delay in all animations. Very useful if you want to make Haste or Slow effect ala Dungeon & Dragons without making new models. ~{Mode} defines how {factor} is applied to delay. 0 = Original delay + {factor} 1 = Original delay * {factor} ~{Factor} is the value you want to apply to delay. ~{Cap Min} is possible shortest delay. ~{Cap Max} is possible longest delay. ~{Range Min} is the shortest original delay that will be adjusted. Useful to prevent short delays such as 1 centisecond delay to be shortened. ~{Range Max} is the longest original delay that will be adjusted. Useful to prevent long delays such as 500 centiseconds delay to be extended.

Here is the code:
Code:
@script
	void self = getlocalvar("self");
	int MP = getentityproperty(self,"mp");
	changeentityproperty(self, "mp", MP-1);
		if(MP<10){
			changeentityproperty(self, "running", 1.4);
			changeentityproperty(self, "edelay", 1, 1.5, 1, 999, 1, 999);
     	    }else{
			changeentityproperty(self, "running", 1.8);
			changeentityproperty(self, "edelay", 1, 1, 1, 999, 1, 999);
		}
	@end_script
 
Thx, edelay is a good idea!
But MP-1 doesn't match what I want because the lost MP speed is too high (each frame one MP is gone, so I prefer to use mpset). Moreover, I still have the decreasing MP problem after running. And now the next animations became slow for the same reason. It's why I think I maybe gotta use a script in the header instead of in the run animation. But first it's time for me to sleep. ^^
 
16-bit Fighter said:
Thx, edelay is a good idea!
But MP-1 doesn't match what I want because the lost MP speed is too high (each frame one MP is gone, so I prefer to use mpset). Moreover, I still have the decreasing MP problem after running. And now the next animations became slow for the same reason. It's why I think I maybe gotta use a script in the header instead of in the run animation. But first it's time for me to sleep. ^^
Yes I agree to you, and that's exactly why I suggest to use it in ondraw script or key script. In these event scripts you have a lot more potential, for example you can change mp cost rate using the openborvariant "elapsed_time" and define any rate that you want. And you can define "edelay" for running animation only too. Here is an example:

Code:
void running()
{//Change running speed and edelay properties according to MP
	void self 	= getlocalvar("self");
	void ani	= getentityproperty(self, "animationID");
	int mp		= getentityproperty(self, "mp");
	int cost 	= 1; //DEFAULT COST VALUE
	float rate	= 20; //DEFAULT RATE INTERVAL
	float time	= openborvariant("elapsed_time");
	
	if(ani == openborconstant("ANI_RUN")){ //IS IN RUNNING ANIMATION??
		if(time%rate == 0){ //ADJUST COST RATE BY CHANGING RATE INTERVAL BETWEEN EACH SUBTRACTION. BIG RATE = BIG INTERVAL
			changeentityproperty(self, "mp", mp-cost); //CHANGE MP ACCORDING TO COST VALUE
		}
		
		if(mp < 10){ //LESS THAN 10 MP??
			changeentityproperty(self, "running", 1.4); //SLOW RUNNING SPEED
			changeentityproperty(self, "edelay", 1, 1.5, 1, 999, 1, 999); //ADD MORE DELAY
		}else{//MORE OR EQUAL THAN 10 MP??
			changeentityproperty(self, "running", 1.8); //DEFAULT RUNNING SPEED
			changeentityproperty(self, "edelay", 1, 1, 1, 999, 1, 999); //DEFAULT DELAY
		}
	}else{ //IS NOT IN RUNNING ANIMATION??
		changeentityproperty(self, "running", 1.8); //MAINTAIN DEFAULT RUNNING SPEED
		changeentityproperty(self, "edelay", 1, 1, 1, 999, 1, 999); //MAINTAIN DEFAULT DELAY
	}
}

Another method to change mp cost rate is changing MP policy of all characters. For example, if you have a game that uses 10 mp as default maximum mp for all entities, the cost of  "-1" will be high. However, if you change all characters maximum mp value from 10 to 100 (10x), the cost of "-1" will turn into a small number.
 
Damon Caskey said:
The float is real value used by engine. The text command is multiplied by 0.1 as it's read in. This predated me. My guess is they just thought humans would rather input a larger whole number.
Thanks BTW!

Kratus said:
Yes I agree to you, and that's exactly why I suggest to use it in ondraw script or key script. In these event scripts you have a lot more potential, for example you can change mp cost rate using the openborvariant "elapsed_time" and define any rate that you want. And you can define "edelay" for running animation only too. Here is an example:

Code:
void running()
{//Change running speed and edelay properties according to MP
	void self 	= getlocalvar("self");
	void ani	= getentityproperty(self, "animationID");
	int mp		= getentityproperty(self, "mp");
	int cost 	= 1; //DEFAULT COST VALUE
	float rate	= 20; //DEFAULT RATE INTERVAL
	float time	= openborvariant("elapsed_time");
	
	if(ani == openborconstant("ANI_RUN")){ //IS IN RUNNING ANIMATION??
		if(time%rate == 0){ //ADJUST COST RATE BY CHANGING RATE INTERVAL BETWEEN EACH SUBTRACTION. BIG RATE = BIG INTERVAL
			changeentityproperty(self, "mp", mp-cost); //CHANGE MP ACCORDING TO COST VALUE
		}
		
		if(mp < 10){ //LESS THAN 10 MP??
			changeentityproperty(self, "running", 1.4); //SLOW RUNNING SPEED
			changeentityproperty(self, "edelay", 1, 1.5, 1, 999, 1, 999); //ADD MORE DELAY
		}else{//MORE OR EQUAL THAN 10 MP??
			changeentityproperty(self, "running", 1.8); //DEFAULT RUNNING SPEED
			changeentityproperty(self, "edelay", 1, 1, 1, 999, 1, 999); //DEFAULT DELAY
		}
	}else{ //IS NOT IN RUNNING ANIMATION??
		changeentityproperty(self, "running", 1.8); //MAINTAIN DEFAULT RUNNING SPEED
		changeentityproperty(self, "edelay", 1, 1, 1, 999, 1, 999); //MAINTAIN DEFAULT DELAY
	}
}
Sorry, yesterday I was tired and I forget to retain a part of your text...
Thank you for this new script! I need time to understand well all of this but I still tried it. Tell me what I did wrong because it shut down, please. I put this in the header of the char:
Code:
ondrawscript 		data/scripts/running.c
And I put all your code in the running.c file.

And what does exactly mean draw in OP (I'm not familiar with it at all yet) ? What's the difference with spawn? Reading the manual doesn't help in this case.

Kratus said:
Another method to change mp cost rate is changing MP policy of all characters. For example, if you have a game that uses 10 mp as default maximum mp for all entities, the cost of  "-1" will be high. However, if you change all characters maximum mp value from 10 to 100 (10x), the cost of "-1" will turn into a small number.
Yes, you're right, I didn't think about it. But it's weird to see the jauge in the same time increase (because of the mpset mp recover) and (mostly) decrease.
 
You did everything right, but if you will use only this script in the character "ondraw" script file, need to exchange the name of script from "void running()" to "void main()". I tested in some chars in SOR2X and it worked well.

And about "draw" or "spawn" terms in scripts like "ondraw" or "onspawn", it means at which moment this script will run. For example, the "onspawn" script in the header of any character means that all scripts inside "data/scripts/onspawn.c" will run at the moment the entity is spawned only, once.

So, "ondrawscripts" will run at each moment the character sprite is drawn in the screen, that is, anytime. But use ondrawscripts with caution because it will be like "updatescripts" and run at every engine cicle. For example, you can use it in tasks that you need to verify entity status at anytime (like MP) but with not too heavy operations, or it will use a lot of memory and crash the game.

Here is the script updated with "void main()"
Code:
void main()
{//Change running speed and edelay properties according to MP
	void self 	= getlocalvar("self");
	void ani	= getentityproperty(self, "animationID");
	int mp		= getentityproperty(self, "mp");
	int cost 	= 1; //DEFAULT COST VALUE
	float rate	= 20; //DEFAULT RATE INTERVAL
	float time	= openborvariant("elapsed_time");
	
	if(ani == openborconstant("ANI_RUN")){ //IS IN RUNNING ANIMATION??
		if(time%rate == 0){ //ADJUST COST RATE BY CHANGING RATE INTERVAL BETWEEN EACH SUBTRACTION. BIG RATE = BIG INTERVAL
			changeentityproperty(self, "mp", mp-cost); //CHANGE MP ACCORDING TO COST VALUE
		}
		
		if(mp < 10){ //LESS THAN 10 MP??
			changeentityproperty(self, "running", 1.4); //SLOW RUNNING SPEED
			changeentityproperty(self, "edelay", 1, 1.5, 1, 999, 1, 999); //ADD MORE DELAY
		}else{//MORE OR EQUAL THAN 10 MP??
			changeentityproperty(self, "running", 1.8); //DEFAULT RUNNING SPEED
			changeentityproperty(self, "edelay", 1, 1, 1, 999, 1, 999); //DEFAULT DELAY
		}
	}else{ //IS NOT IN RUNNING ANIMATION??
		changeentityproperty(self, "running", 1.8); //MAINTAIN DEFAULT RUNNING SPEED
		changeentityproperty(self, "edelay", 1, 1, 1, 999, 1, 999); //MAINTAIN DEFAULT DELAY
	}
}
 
Thanks a lot Kratus, it works like a charm and it's visually cool! :D

So I understand that "ondraw" means as long as the entity is seen on the screen, the script continously runs, thus I should limit this kind of scripts.

In a second time, I'll have to think about make the script works for all the chars (each one has its own running speed). The goal is to make the decreased run speed between walk speed and default running speed (a sort of average speed). It's the first step that seems more complicated to me, IOW making the script always know what is the default running speed of the char. I don't know if I'm clear.
 
16-bit Fighter said:
Thanks a lot Kratus, it works like a charm and it's visually cool! :D

So I understand that "ondraw" means as long as the entity is seen on the screen, the script continously runs, thus I should limit this kind of scripts.

In a second time, I'll have to think about make the script works for all the chars (each one has its own running speed). The goal is to make the decreased run speed between walk speed and default running speed (a sort of average speed). It's the first step that seems more complicated to me, IOW making the script always know what is the default running speed of the char. I don't know if I'm clear.
No problem, I´m happy that this script works for you. I understand your point and will post a better code that will work for all characters. Then, let´s go!

This script below will detect any character running speed and save it in a local variable. It will calculate any amout of maximum mp that each character
has and split in 3 steps:

- default speed, more/equal than 2/3 of mp
- average speed, less than 2/3 and more/equal than 1/3 of mp
- slow speed, less than 1/3 of mp

So, the saved variable in the character first spawn will be used for reset to default running speed when needed. And to avoid excessive operations, I added a line to stop the subtraction if the entity has no mp.

Plus, I added 3 "drawstring" lines in the end to check if the script is working correctly. When you finish all tests, you can erase it.
To use this script for all characters, you need to define the same "ondrawscript" file in the header of each character.

You can modify any value as you wish, like the speed reduction (current is speed/2 or speed/4).

Here is the script:
Code:
void main()
{//Change running speed and edelay properties according to MP
	void self 	= getlocalvar("self");
	void ani	= getentityproperty(self, "animationID"); //GET CURRENT ANIMATION
	int maxMp	= getentityproperty(self, "maxmp"); //GET MAXIMUM MP
	int mp		= getentityproperty(self, "mp"); //GET CURRENT MP
	int cost 	= 1; //DEFAULT COST VALUE
	float speed = getentityproperty(self, "running", "speed"); //GET CURRENT RUNNING SPEED
	float rate	= 20; //DEFAULT RATE INTERVAL
	float time	= openborvariant("elapsed_time"); //GET CURRENT TIME
	
	if(getlocalvar("defaultSpeed"+self) == NULL()){ //VARIABLE "DEFAULT SPEED" IS NULL?? FIRST CHARACTER SPAWN IN THE GAME?? 
		setlocalvar("defaultSpeed"+self, speed); //CREATE A VARIABLE AND SAVE DEFAULT SPEED
	}
	
	if(ani == openborconstant("ANI_RUN")){ //IS IN RUNNING ANIMATION??
		if(mp > 0){ //CHECK IF MP IS ZERO TO PAUSE SCRIPT EXECUTION WITH NO MP
			if(time%rate == 0){ //ADJUST COST RATE BY CHANGING RATE INTERVAL BETWEEN EACH SUBTRACTION. BIG RATE = BIG INTERVAL
				changeentityproperty(self, "mp", mp-cost); //CHANGE MP ACCORDING TO COST VALUE
			}
		}
		
		if(mp >= maxMp/1.5){ //MORE OR EQUAL THAN 2/3 MP?? IF YOUR MAX MP IS 30, THIS WILL BE EQUAL TO 20 MP
			changeentityproperty(self, "running", getlocalvar("defaultSpeed"+self)); //DEFAULT RUNNING SPEED
			changeentityproperty(self, "edelay", 1, 1, 1, 999, 1, 999); //DEFAULT DELAY
		}else if(mp < maxMp/1.5 && mp >= maxMp/3){ //LESS THAN 2/3 MP AND MORE OR EQUAL THAN 1/3 MP?? BETWEEN 10 AND 19 MP
			changeentityproperty(self, "running", getlocalvar("defaultSpeed"+self)/2); //AVERAGE RUNNING SPEED
			changeentityproperty(self, "edelay", 1, 1.5, 1, 999, 1, 999); //AVERAGE DELAY
		}else{//LESS THAN 1/3 MP?? IF YOUR MAX MP IS 30, THIS WILL BE EQUAL TO 9 MP OR BELOW
			changeentityproperty(self, "running", getlocalvar("defaultSpeed"+self)/4); //SLOW RUNNING SPEED
			changeentityproperty(self, "edelay", 1, 2, 1, 999, 1, 999); //SLOW DELAY
		}
	}else{ //IS NOT IN RUNNING ANIMATION??
		changeentityproperty(self, "running", getlocalvar("defaultSpeed"+self)); //DEFAULT RUNNING SPEED
		changeentityproperty(self, "edelay", 1, 1, 1, 999, 1, 999); //DEFAULT DELAY
	}
	
	//DRAW VALUES IN THE SCREEN TO CHECK IF THE SCRIPT IS WORKING. ERASE THIS 3 LINES AFTER THE TESTS ARE DONE!!!
	drawstring(100, 100, 0, speed); //SHOW CURRENT RUNNING SPEED APPLIED IN THE CHARACTER IN REAL TIME
	drawstring(100, 110, 0, getlocalvar("defaultSpeed"+self)); //SHOW DEFAULT SPEED SAVED INTO A VARIABLE TO CHECK IF IS ALL RIGHT
	drawstring(100, 120, 0, mp); //DRAW CURRENT MP TO CHECK IF THE SCRIPT IS ACTIVATING IN THE DEFINED MP VALUES
}
 
Kratus Nice script! I will save it for later use.

About this:
But use ondrawscripts with caution because it will be like "updatescripts" and run at every engine cicle.
It's not exactly that. Ondrawscripts runs everytime a sprite is being draw on the screen, not in every engine cycle - this is what update/updated scripts are for. Updated will run when the engine update cyle ends, where update will run at every engine cycle (200 times per second, if I am not wrong). The engine uses a set of flags to run asynchronous updates.

But yeah, those type of scripts needs to be very optmized, or they can lower the overall performance. And it's not an OpenBOR issue.
 
O Ilusionista said:
Kratus Nice script! I will save it for later use.

About this:
But use ondrawscripts with caution because it will be like "updatescripts" and run at every engine cicle.
It's not exactly that. Ondrawscripts runs everytime a sprite is being draw on the screen, not in every engine cycle - this is what update/updated scripts are for. Updated will run when the engine update cyle ends, where update will run at every engine cycle (200 times per second, if I am not wrong). The engine uses a set of flags to run asynchronous updates.

But yeah, those type of scripts needs to be very optmized, or they can lower the overall performance. And it's not an OpenBOR issue.

Thanks O Ilusionista to show me the correct information and sorry by the incorrect explanation. I understand that update/updated and ondraw are not the same and I meant that about the speed of ondraw script and not the moment that it runs. Although they not run in the same moment and has different purposes, both need caution because they will repeat a lot of time. And I didn't say that it is a OpenBOR issue... But thanks anyway.

So, I have a question. At what speed does ondraw script runs?
Because if the speed it´s more than 1 engine cicle, I think that the sprite will fail sometimes (or not), but both ondraw and updated not fails. Even ondraw script not runs synced with each engine cicle, maybe both runs at the same speed... Or I´m wrong??
 
Kratus you are welcome, buddy.
And I didn't say that it is a OpenBOR issue.
I know, neither I blamed you for that :) I was reffering some users that whenever they fail to do something, they automatically blame the engine.

So, I have a question. At what speed does ondraw script runs?
Because if the speed it´s more than 1 engine cicle, I think that the sprite will fail sometimes (or not), but both ondraw and updated not fails. Even ondraw script not runs synced with each engine cicle, maybe both runs at the same speed... Or I´m wrong??
We would need to call Damon Caskey  here to answer this. As far as I know, first is UPDATE, then UPDATED and only them ONDRAW. But ondrawn uses asynchronous updates.
 
Your code is brillant and readable Kratus! Thanks again!

My MP system will be a little different because I still have to define it once for all. But for the moment in the one I think there are two running speeds, once being the average of walk speed and default running speed, and the number of MP below which the decreased running speed is activated doesn't depend on a percentage value but is specific according to what (whom?) char is of. It's not ideal, so I'll probably change it for percetage values. But I'll think about it in a few days.
 
O Ilusionista said:
Kratus you are welcome, buddy.
And I didn't say that it is a OpenBOR issue.
I know, neither I blamed you for that :) I was reffering some users that whenever they fail to do something, they automatically blame the engine.
O Ilusionista
No problem my friend, it's all right  ;) I will pay more attention in my explanations to not miss any information.
I understand your point about protecting the engine and agree to you, I would do the same thing.

About the updated/ondraw scripts speed, it can be useful to see what script is better for each use. I will call DC to learn more about it

16-bit Fighter
I'm glad you liked it. Now that you have the base script structure you can customize anything as you wish, like the number of speeds, change division by percentage or fixed values, etc...
We're here if need more help  ;)
 
Kratus said:
16-bit Fighter
I'm glad you liked it. Now that you have the base script structure you can customize anything as you wish, like the number of speeds, change division by percentage or fixed values, etc...
We're here if need more help  ;)
Yes, you give me a good base to work with and I'm pleased to have some help quickly. ^^ Coding is not sexy to me but I like to think about game mechanics, so I'm motivated to learn little by little.
 
I don't know if I'd better create a new topic but it's still about the same onspawn script. I'd like to add some code to make the mp rate stop when the number of enemies is under this of players.

I added two integers:

Code:
int ecount	= openborvariant("count_enemies"); //Get how many enemies are active
int pcount	= openborvariant("count_players"); //Get how many players are active

If I add this code, the engine shut down:

Code:
	if (ecount < pcount) {
		changeentityproperty(self, "mprate", 0);
	}

If I add this code, the mp still recover (mp is an integer from getentityproperty(self, "mp") of course):
Code:
	if (ecount < pcount) {
		changeentityproperty(self, "mp", mp);
	}
 
16-bit Fighter said:
I don't know if I'd better create a new topic but it's still about the same onspawn script. I'd like to add some code to make the mp rate stop when the number of enemies is under this of players.

I added two integers:

Code:
int ecount	= openborvariant("count_enemies"); //Get how many enemies are active
int pcount	= openborvariant("count_players"); //Get how many players are active

If I add this code, the engine shut down:

Code:
	if (ecount < pcount) {
		changeentityproperty(self, "mprate", 0);
	}

If I add this code, the mp still recover (mp is an integer from getentityproperty(self, "mp") of course):
Code:
	if (ecount < pcount) {
		changeentityproperty(self, "mp", mp);
	}

Hello 16-bit Fighter
After some tests, I think that "mprate" cannot be changed directly, only using "mpset".
I have a similar script in SOR2X in didhit section. Some moves cannot fill mp and I use the following simple method to block the mprate:

Code:
changeentityproperty(self, "mp", mp-mpRate); //MP RATE IS BLOCKED
This script check current "mp" and "mprate". Then, each time the player gain mp+mprate, the script will subtract by the same mprate value.
Example:

MP = 10
MPRATE = 2
EACH HIT WILL ADD +2 MP, BUT WILL REDUCE -2 MP. THE RESULT IS: 10+2-2 = 10

An alternative method is changing "mpset" but you will need to save the default "mprate" in a variable to reset after
 
Thanks! I chose the first method and it's been perfect since I set it to work every 2 seconds. Before today I thought mprate was activated each second...

Beside, I noticed that what is thrown by an enemy is considered as an enemy. But I don't know how to substract this kind of entity to the count. :/
 
Back
Top Bottom