Why does loop crash and and not print? Unity3D - unity3d

Given a cubic space, this function searches for the next large empty space, places a marker there, and then quits.
However, the function doesn't even print the check message that exists prior to the loops starting, so i don't know how to debug it. The checking starts at 0,0,0 and spaces outside the voxel are returned as true, so it should default all the first loops and send messages back. The unity.exe process jams and i have to abort it.
Why doesn't it print? What else is wrong with it? Even if it is slow, i should be able to track progress within the loops? why wouldn't it?
function findvoidable() //find void space in voxel volume
{
var step = dist+1;
print("start"); WaitForFixedUpdate(); //this doesnt print
for ( var k : int = 0; k < mesher.PNGpaths.Length ; k+=step/2)
for ( var j = 0; j < mesher.tex.height ; j+=step/2)
for ( var i = 0; i < mesher.tex.width ; i+=step/2){
print("in schema");WaitForFixedUpdate();
if (wst( i , j , k )==false )
if (wst( i+step,j ,k )==false )
if (wst( i-step,j ,k )==false )
if (wst( i ,j+step,k )==false )
if (wst( i ,j-step,k )==false )
if (wst( i ,j ,k+step )==false )
if (wst( i ,j ,k-step )==false )
{
var cnt=0;
for ( var x = i-step; x < i+step ; x+=1)
for ( var y = j-step; y < j+step ; y+=1)
for ( var z = k-step; z < k+step ; z+=1)
{
if ( wst( x , y , z ) == false )
cnt+=1;
}
if ( cnt >= step*step*step-3 )
{
refCube.transform.position=Vector3(i,j,k);
break;break;break;break;break;break;
}
else
{
WaitForFixedUpdate();
refCube.transform.position=Vector3(i,j,k);
}
}
}
}

WaitForFixedUpdate is a Coroutine and is not supposed to be run like a normal method.
Instead, try "yield" statement:
yield WaitForFixedUpdate();
More info: https://docs.unity3d.com/ScriptReference/Coroutine.html

Related

How to create a layout that all its cells are initially empty and accessible by moving agents (such as equipment)?

In Copper Nickel Mine (Cloud) Simulation, In MinePanel Agent, there is a function Called setupTunnelLayout.
The original code in the above function is as following:
//create corridor of already empty rooms
`RoomBlock emptyRoom;
for ( int j = 0; j < nColumns; j++ ) {
emptyRoom = add_roomBlocks();
emptyRoom.jumpToCell( 0, j );
if ( j == nColumns - 1 )
emptyRoom.isStartBlock = true;
emptyRoom.isTunnel = true;
}`
But in my scenario all the cells are accessible initially, so all can be tunnel (path to move), not only the (0, j) row as the above example!
I was thinking I can change it as the followings; (1) or (2);
(1)
//create corridor of already empty rooms
RoomBlock emptyRoom;
for ( int j = 0; j < nColumns; j++ )
for (int i = 0; i < nRows; i++){
emptyRoom = add_roomBlocks();
emptyRoom.jumpToCell( 0, j );
emptyRoom.jumpToCell( i, 0 );
if ( j == nColumns - 1 )
emptyRoom.isStartBlock = true;
emptyRoom.isTunnel = true;
}
Or it can be like this;
(2)
//create corridor of already empty rooms
`RoomBlock emptyRoom;
for ( int j = 0; j < nColumns; j++ )
for (int i = 0; i < nRows; i++){
emptyRoom = add_roomBlocks();
emptyRoom.jumpToCell( i, j );
if ( j == nColumns - 1 )
emptyRoom.isStartBlock = true;
emptyRoom.isTunnel = true;
}`
'Can you please let me know if (1) or (2) are correct? Which one is preferred?
Thank you so much,
Neda.'
The second is preferable; there is no point jumping the agent to the column and then the row when you can directly move it to the exact cell coordinates.
In both cases though you are missing a closing } for your extra i loop (and you're missing an opening one in your second case).
//create corridor of already empty rooms
RoomBlock emptyRoom;
for (int j = 0; j < nColumns; j++) {
for (int i = 0; i < nRows; i++) {
emptyRoom = add_roomBlocks();
emptyRoom.jumpToCell(i, j);
if (j == nColumns - 1) {
emptyRoom.isStartBlock = true;
}
emptyRoom.isTunnel = true;
}
}
[I used the StackOverflow formatting to mark the full block as Java code. It's also best-practice (although AnyLogic doesn't do it many of their example models) to always use curly brackets for loops, etc. even if their body only has one line.]
Plus whether the change will do what you want or not obviously depends on the rest of the model and how it handles the grid of cells and tunnels.

How to remove a single number from a list with multiples of that number

As I'm a beginner in coding I wanted to try to find the first three repeated numbers in a list. My problem is that in my code when there is a number repeated three, the code breaks.
The usual, remove, pop, and del, don't work as they delete one element in the list.
import random
r = random.randint
string = ""
def first_repeat(myList):
myList = sorted(list(myList))
print(myList)
number = 0
final_numbers = []
loop = 0
while loop < 2:
try:
if number == 0:
number += 1
else:
if myList[loop] == myList[loop-1]:
final_numbers.append(myList[loop])
else:
myList.pop(loop)
myList.pop (loop-1)
number = 0
if loop == 0 :
loop += 1
else:
loop -= 1
if len(final_numbers) > 3:
return final_numbers[0], final_numbers[1], final_numbers[2]
if len(myList) <=1:
loop += 2
except:
continue
return final_numbers
for n in range(20):
string = string+str(r(0,9))
print(first_repeat(string))
the expected result should be at the first three repeated numbers.
I added some print statements so you can go through your program and find out where the logic is wrong with your code.
import random
r = random.randint
string = ""
def first_repeat(myList):
myList = sorted(list(myList))
print(myList)
number = 0
final_numbers = []
loop = 0
while loop < 2:
print( 'inside while loop: loop = {}'.format( loop ))
try:
if number == 0:
number += 1
else:
if myList[loop] == myList[loop-1]:
print( 'in -> if myList[loop] == myList[loop-1]' )
final_numbers.append(myList[loop])
print( 'final_numbers: [{}]'.format( ','.join( final_numbers )))
else:
print( 'in first -> else' )
myList.pop(loop)
myList.pop (loop-1)
number = 0
print( 'myList: [{}]'.format( ','.join( myList ) ))
if loop == 0 :
loop += 1
else:
loop -= 1
if len(final_numbers) > 3:
print( 'returning final numbers' )
print( final_numbers )
return final_numbers[0], final_numbers[1], final_numbers[2]
if len(myList) <=1:
loop += 2
except:
continue
print( 'at end of this loop final numbers is: [{}]'.format( ','.join( final_numbers)))
print( 'press any key to continue loop: ')
input()
return final_numbers
for n in range(20):
string = string+str(r(0,9))
print(first_repeat(string))
Following is a method to do it taking advantage of pythons defaultdict
https://docs.python.org/2/library/collections.html#collections.defaultdict
#import defaultdict to keep track of number counts
from collections import defaultdict
#changed parameter name since you are passing in a string, not a list
def first_repeat( numbers_string ):
#create a dictionary - defaulddict( int ) is a dictionary with keys
#instantiated to 0 - (instead of throwing a key error)
number_count = defaultdict( int )
#convert your string to a list of integers - look up list iterations
numbers = [ int( s ) for s in list( numbers )]
# to store the repeated numbers
first_three_repeats = []
for number in numbers:
# for each number in the list, increment when it is seen
number_count[number] += 1
#at first occurence of 3 numbers, return the number
if number_count[number] == 2:
first_three_repeats.append( number )
if len( first_three_repeats ) == 3:
return first_three_repeats
#if here - not three occurrences of repeated numbers
return []
for n in range(20):
string = string+str(r(0,9))
print( findFirstThreeNumbers( string ))

Two variables in for loop using Swift

How to use two variables in for loop?
for j,k in zip(range(x,0,-1),range(y,-1,-1)
I want to implement this in Swift.
If your range is a python function, then the Swift-y solution will be:
let x = 100
let y = 99
let rx = reverse(0...x)
let ry = reverse(-1...y)
for (j,k) in zip(rx, ry) {
println(j, k)
}
if you're looping over a dictionary you can loop like this
for (key,value) in dictionary {
}
if an array etc. you're going to have to use a c style for loop
just sub in whatever start and end indices you need
for var j = 0 , k = 0; j < 10 && k < 10; j++ , k++ {
}
EDIT
missed the zip in there. You can loop like this
for (j,k) in zip(range1, range2) {
}

SDL: draw a half-transparent rectangle

I cannot figure out how to draw a half-transparent red rectangle onto the screen surface.
Here's the code I have so far:
#!/usr/bin/perl
use SDL;
use SDL::Video;
use SDL::Surface;
use SDL::Rect;
# the size of the window box or the screen resolution if fullscreen
my $screen_width = 800;
my $screen_height = 600;
SDL::init(SDL_INIT_VIDEO);
# setting video mode
my $screen_surface = SDL::Video::set_video_mode($screen_width, $screen_height, 32, SDL_ANYFORMAT|SDL_SRCALPHA);
# drawing something somewhere
my $mapped_color = SDL::Video::map_RGBA($screen_surface->format(), 255, 0, 0, 128); #should be half-transparent, I suppose?
SDL::Video::fill_rect($screen_surface,
SDL::Rect->new($screen_width / 4, $screen_height / 4,
$screen_width / 2, $screen_height / 2), $mapped_color);
# update an area on the screen so its visible
SDL::Video::update_rect($screen_surface, 0, 0, $screen_width, $screen_height);
sleep(5); # just to have time to see it
It results in the red opaque rectangle on the black background, which is not what I am trying to achieve.
You cannot use SDL_FillRect for transparency. The function will overwrite the surface with the value color. Think of it as a "memset".
Quote from docs: If the color value contains an alpha value then the destination is simply "filled" with that alpha information, no blending takes place.
Use SDL_BlitSurface to get transparency. First create a texture fill it with color and then blit.
I made a little test case for practice:
#include <SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
SDL_Surface* CreateSurface( int width , int height )
{
uint32_t rmask , gmask , bmask , amask ;
/* SDL interprets each pixel as a 32-bit number, so our masks must depend
on the endianness (byte order) of the machine */
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
SDL_Surface* surface = SDL_CreateRGBSurface( 0 , width , height , 32 , rmask , gmask , bmask , amask ) ;
if( surface == NULL )
{
( void )fprintf(stderr, "CreateRGBSurface failed: %s\n", SDL_GetError() );
exit(1);
}
return surface ;
}
void Quit( void )
{
SDL_Quit() ;
exit(0) ;
}
int main(int argc, char *argv[])
{
( void )argc ;
( void )argv ;
int init = !( SDL_Init( SDL_INIT_EVERYTHING ) );
if( !init )
Quit() ;
SDL_Surface* screen = SDL_SetVideoMode( 800 , 600 , 32 , 0 ) ;
if( !screen )
Quit() ;
int run = true ;
while( run )
{
SDL_Event event ;
while( SDL_PollEvent( &event ) )
{
switch( event.type )
{
case SDL_QUIT:
run = false ;
break;
}
}
SDL_Surface* s = CreateSurface( 300 ,300 ) ;
( void )SDL_FillRect( s , NULL , 0xAA0000FF ) ;
SDL_Rect rect = { 100 , 100 } ;
( void )SDL_BlitSurface( s , NULL , screen , &rect ) ;
rect.x = 200 ;
rect.y = 200 ;
( void )SDL_FillRect( s , NULL , 0x440000FF ) ;
( void )SDL_BlitSurface( s , NULL , screen , &rect ) ;
SDL_FreeSurface( s ) ;
( void )SDL_Flip( screen ) ;
SDL_Delay( 15 ) ;
( void )SDL_FillRect( screen , NULL , 0x00FFFF ) ;
}
Quit() ;
return 0;
}

coffeescript for in bug?

On coffeescript loop 'for'
eg.
if 1 < x, code like below:
console.debug i for i in [1..0]
Generated code is:
var i;
for (i = 1; i >= 0; i--) {
console.debug(i);
}
if 1 > x,code like below:
console.debug i for i in [1..2]
Generated code is:
var i;
for (i = 1; i <= 2; i++) {
console.debug(i);
}
If i want write that javascript.How to ?
for(var i=1;i<=0;i++){
console.debug(i);
}
Because i don't know the condition is greater than left side or less than left side.
But i just want it i++
What's wrong with me?
EDIT BELOW:
For coffeescript's feature,I add condition before the loop or add condition on for loop.
eg:
if x - y >=1
console.debug i for i in [1..x-y]
or
console.debug i for i in [1..x-y] and x-y >=1
That's my way.Some one have good advice?
It looks like you want to do this:
console.debug i for i in [1..x-y] by 1
Which gets compiled to:
var i, _i, _ref;
for (i = _i = 1, _ref = x - y; _i <= _ref; i = _i += 1) {
console.debug(i);
}
for(var i=1;i<=0;i++){
console.debug(i);
}
is equivalent to
var i = 1;
while(true) {
console.debug(i);
i++;
}
which in coffeescript is written as
i = 1
while true
console.debug(i);
i++;