I've been trying to track the velocities of vehicles in a specific road, this road have parking lots. This is the code i made to track each vehicle in a certain population, this code is running in a event block that runs every 15 seconds.
for (i = 0 ;i < car.size(); i++){
if(car(i).isCarOn(road1)){
car(i).id.setText(String.valueOf(i));
vCar = autos(i).getSpeed();
text = text + System.lineSeparator() + "Car (" + i + "):" + vCar;
}else if (car(i).isCarOn(road)){
car(i).id.setText(String.valueOf(i));
vCar = car(i).getSpeed();
text2 = text2 + System.lineSeparator() + "Car (" + i + "):" + vCar;
}
}
the issue is that when a car is changing from road to parking lot, it throws an NullPointerException error, is there a way to fix this issue track the speed of those vehicles? Also it would be great if there is an explanation for this error.
Thanks in advance
Ok, after your clarifications it's almost certain that the error occurs because the cars are getting into the road too fast and kept on the queue of the carSource.
When cars are in this queue they cannot be accessed as cars even though they exist in the car population... This is maybe weird, but it's how things are.
Now if you want to know speeds in a specific road, you should get the cars in that road:
for (Car c : road.getCars(true)){//true means forward lane
vCar=c.getSpeed();
}
for (Car c : road.getCars(false)){//false means backwards lane
vCar=c.getSpeed();
}
Nevertheless, to continue doing what you are doing, I did a video a long time ago on this topic.. unfortunately the video is a bit long (7 minutes) and boring (I was testing my youtube abilities), but it's related to your problem... it's a trick to avoid problems with this carSource queue:
https://www.youtube.com/watch?v=EgYqraEibD4
Related
I'm working on a game (top-down shooter) and have run into a bit of a snag. Up to this point, I've spawned enemies with functions that just work with delays:
Wave One Function - Delay 3, spawn enemies //
Wave Two Function - Delay 6, spawn enemies
I do this because I haven't found a way to wait for all actions in a given function to complete before calling the next - as things stand, functionWaveOne calls functionWaveTwo, which calls functionWaveThree (etc).
This has worked until now. What's changed is I want two enemies to remain on-screen and until they're dead, I don't want the next wave to come. My initial solution to this was to have a running count of how many enemies died in wave four:
Detect collision -> Apply Damage -> Check if dead -> If yes, deadWaveFourEnemies++
Here's my problem: I have a primary weapon that's two parallel lasers, so they have potential to collide with an enemy at exactly the same time. This results in false positives, making the dead enemy count go higher than it should. I even tried adding an "am I alive" function to the enemy class, but to no avail.
Have any of you got a recommendation on how to either call functions in a better way or to get around these false positives? Thanks!
In case it helps:
if([enemySprite.name isEqual: #"shooter"]){
enemyShooter *enemyClass = (enemyShooter *)enemySprite;
itDied = [enemyClass doDamageWithAmount:secondaryWeaponDamage];
scoreIncrease = [enemyClass getScoreIncrease];
stillAlive = [enemyClass amIAlive];
}
[weaponSprite removeFromParent];
if(itDied){
[self increasePlayerScoreBy:scoreIncrease];
if(inWaveFour == 1 && stillAlive == 1){
waveFourKilled++;
NSLog(#"Seconday / Number killed: %i", waveFourKilled);
}
}
I'm trying to write a rule that applies on a set of facts based on the content of another fact. I have simplified the problem to a House with Rooms. Let's say we have something like:
House(id);
Room(id, houseId, floor, side, paint);
Now, if I want to trigger a rule on all the Houses with all Rooms in left side painted on green I would write something like:
rule "Left side 1st floor green"
when
$h: House()
forall($r: Room(houseId=$h.id, floor==1, side=="left")
Room(id == $r.id, paint == "green"))
then
//Do whatever on rule triggering
end
But what if the objects in the working memory are organized in this way:
House(id, List<> roomIds);
Room(id, floor, side, paint);
How can I write a foreach condition (or any other approach) to make the same consideration on the rooms for a given house? Does it make sense or should I better try to reorganize my objects in advance to have the relationship expressed the other way around?
Thanks
Assuming in House the List<> roomIds is a List of Room.id(s), then you can do something like:
rule "Left side 1st floor green"
when
$h: House(/* .. conditions for specific house? .. */)
$r : Room($h.roomIds contains id, floor==1, side=="left", paint == "green")
then
//Do whatever on rule triggering
end
However this is a bit unefficient, and I do agree as others replied, changing a bit the business/data model would make writing this kind of rule more idiomatic and more efficient. For example if the House did have the list of Rooms, you can also use OOPath to navigate as desired the structure.
According to your rule, you do not have houses with rooms. According to what you have defined you have houses in your working memory and you have rooms in your working memory and you try to match these.
Why don't you have a List of Rooms in your House? That would make much more sense:
House(houseId, List<Room> rooms)
Room(roomId, floor, side, paint)
Then your rule would be:
rule "Left side 1st floor green"
when
$houses : House ($rooms : rooms, $houseId : houseId)
$room : Room ($roomId : roomId, floor==1 && side=="left" && paint == "green") from $rooms
then
//Rule would trigger for each room left side 1st floor green rooms
System.out.println("House "+$houseId+" has following left side 1st floor green room: "+$roomId);
end
Basically I am creating a game that spawns a ball after a certain amount of time has passed. However, as time goes on and the player score increases, balls that are more difficult to kill will spawn.
Prior to this, all the spawn methods I have set in place function as I would like them to, but they were contained in a switch statement that calculated the probability a ball would spawn.
//pseudo:
let spawnProbability = arc4random_uniform(100)
switch spawnProbability {
case 0…60: //60% spawn chance
//spawn small ball
case 61…90: //~30% spawn chance
//spawn medium ball
case 91…100: //~9% chance
//spawn large ball
default:
break
}
This worked as a plan B, but I'd like to implement what I originally had designed which was the method described above.
My thought process to accomplish what I originally had in mind is:
for loop that spawns small balls until the score counter reached a certain amount
after the score counter reached, say, 100pts, another for loop would be executed in addition to the first loop. So, medium and small balls spawn after a delay.
after the score counter reached another mark, large balls would begin to spawn on top of medium balls and small balls.
Here's what I mean: (pseudocode again)
for(var i = 0; i < 10; i++) {
//delay
//spawn small ball
}
if score >= 100 {
for (var x = 0; x < 10; x++) {
//delay
//spawn medium ball
}
}
// . . . and so on
I've put alot of thought into how I could accomplish what I want but I can't come up with a better approach to this problem. I understand why this isn't working and why multiple for loops can't be executed with delay in the same class at the same time (or maybe they can and I'm just going about it in the wrong way) but I'm stuck in the sense that I don't know a better way to implement the spawn technique that I want.. Any help is greatly appreciated!!
I have an obj_roulette, which contains 4 subimages, with value 2-5 and image_number 0-3. The value result from roulette stored as var global.roulette.
Then, I make many obj_meteorite, which contains 4 subimages too, spawn from above with random x value and random image_number. Player can shoot them with left-mouse click.
This is what I want:
If image_number obj_roulette is 0, and player shoot obj_meteorite with image_number 0, score +10.
If image_number obj_roulette is 0, and player shoot obj_meteorite with image_number 1, score -10.
I don't know how to check collision between mouse_x/mouse_y and object image_number, and how to match obj_roulette image_number and obj_meteorite image_number.
Is it using collision checking? If it yes, then maybe the examples in these links can help:
link 1
link 2
Please explain your answer. Thanks.
I assume this is the kind of game where you click with your mouse and hit exactly where the mouse was clicked. And as I understand it from your question. If the mouse is clicked and obj_roulette's image_index is the same as obj_meteorite, you want to add 10 to the score. If not, you want to subtract 10 from the score. And you need help converting your pseudo-code into gml.
// Check if obj_meteorite was clicked
if (mouse_check_button_released(mb_left) && position_meeting(mouse_x, mouse_y, obj_meteorite))
{
// Check wheter or not obj_meteorite's and obj_roulette's image_index is the same
if (obj_meteorite.image_index == obj_roulette.image_index)
{
// Add 10 to the score
score += 10;
}
else
{
// Subtract 10 from the score
score -= 10;
}
}
If this is not what you want, I suggest editing your question to make it more clear. Preferably explain shortly what your game is actually about.
I using pyEphem to calculate RA/Decs of satellites and I'm confused by the different
values computed and described on
http://rhodesmill.org/pyephem/radec.html
this bit of code
sat=ephem.readtle("SATNAME ", \
"1 38356U 12030A 14148.90924578 .00000000 00000-0 10000-3 0 5678",\
"2 38356 0.0481 47.9760 0002933 358.9451 332.7970 1.00270012 3866")
gatech = ephem.Observer()
gatech.lon, gatech.lat = '-155.47322222', '19.82561111'
gatech.elevation = 4194
gatech.date = '2014/01/02 07:05:52'
sat.compute(gatech)
print 'a_ra=',sat.a_ra,'a_dec=',sat.a_dec,'g_ra=',sat.g_ra,'g_dec=',sat.g_dec,'ra=',sat.ra,'dec=',sat.dec
gives
a_ra= 0:52:40.75 a_dec= -3:15:23.7 g_ra= 1:14:10.55 g_dec= 0:06:09.8 ra= 0:53:23.57 dec= -3:10:50.5
if I change JUST the observers location to say
gatech.lon, gatech.lat = '-5.47322222', '19.82561111'
I get
a_ra= 1:15:36.95 a_dec= -2:32:29.9 g_ra= 1:14:10.55 g_dec= 0:06:09.8 ra= 1:16:19.75 dec= -2:28:04.6
I thought the observers position only came into the calculation of sat.ra and sat.dec
so was suprised to see a_ra and a_dec had changed.
What am I missing?
Thanks
Ad
Per the last paragraph of the “body.compute(observer)” section of the Quick Reference:
http://rhodesmill.org/pyephem/quick.html#body-compute-observer
For earth satellite objects, the astrometric coordinates [meaning a_ra and a_dec] are topocentric instead of geocentric, since there is little point in figuring out where the satellite would appear on a J2000 (or whatever epoch you are using) star chart for an observer sitting at the center of the earth.
And in the issue that has been opened about this behavior, the project is open to suggestions about where this text can appear more prominently to prevent future confusion for users:
https://github.com/brandon-rhodes/pyephem/issues/55