How to Detect TriggerEnter without OnTriggerEnter Function Unity - physics

I have a empty game object attached with a obstacles script. This script contain List of all the instantiate object references. I want to check whether the instantiate objects enter into trigger or not using this single script.
Since unity execute OnTriggerFunction() if a monobehavior script is attache to it. I don't want to add the script in all instantiate object to trigger check.
Here is my sample code
public List<AsteroidStructure> asteroidStructure = new List<AsteroidStructure> ();
public void AsteroidCollision (List<AsteroidStructure> asteroidStructure){
}
This script is attached to single empty game object and list variable contains all the references of instantiate object. I want to check the trigger of all game object in Asteroid collision function. I am calling this function in every fixed update.

Related

How to add an outside callable member functions to an anonymous object?

This is the first time I ask a question, please excuse my mistake on asking a question, and tell me how to ask a better question if that happens.
What I am asking:
How can I add a member function, into an anonymous object, which is callable outside this object? Or this requirement is too strange that shouldn't be implemented?
Consider this object expression below:
variable = object : AClass()
{
// inside the declaration of anonymous object
override fun aFunction(i: Int)
{
// do something inside aFunction
}
}.apply {
// do something inside apply
}
How can I add a function fun bFunction(i: Int), that just belongs to variable, not belongs to AClass, that can be callable by directly using
variable.bFunction(1)
? Or, it is impossible to do so?
What I have tried (and, of course, not satisfy what I want):
I can create an extension function fun AClass.bFunction(i: Int) to solve it, however this maybe "unfair" to other instance of AClass, since they actually don't need that.
I can create a class instead of using an anonymous object locally, however it seems too heavy to create a class for ONE variable.
I have tried adding bFunction inside the declaration of anonymous object, where the position is mentioned above. However I can just only access this function inside .apply {}, not outside this assignment.
I have tried adding bFunction inside .apply {}, however it also make me just access it inside .apply {}, not outside this assignment.
To be more specific of the original question (which is solved by myself, while typing this question) to avoid an X - Y question:
I am struggling on painting on Java GUI components. What I want to achieve is like a "canvas" or a "paper" inside a JFrame, where I can paint anything I like on it, by pixel control, totally using Kotlin.
After some searching, I found that the core solution to the question of, "if I draw something by using contentPane.graphics.drawXXX series function, this draw will be disappear when (I resize or minimize this JFrame, or just called this draw function too early, when this JFrame is showing (note: we can use Thread.sleep to delay too-early call))", is by overriding paint function. Then, in order to override it, I use var image = BufferedImage(...) to store what I have painted, and use an anonymous object
painting = object : JFrame()
{
override fun paint(g: Graphics?)
{
contentPane.graphics.drawImage(image, 0, 0, this)
}
}.apply { /* do some initialization */ }
to override paint function, and draw this buffered image into contentPane, which seems achieved what I want, and just a "static" painting.
I was wondering how can I add a updatePainting() function to this painting, which make me can just modify this buffered image, then call this function to update the painting, as a "dynamic" painting. However, during the time I am typing this question, I accidentally find that, just modify this image, will automatically update this painting, no need to manually update this painting. So this original question actually solved: this bFunction is not necessary in my current situation. But, I just wondering, are there any way to implement this things that can fit this (may be strange) issue?
Actually, this code (with bFunction inside the declaration of anonymous object) will work perfectly well and will make bFunction accessible outside... but only if variable is local or private. In both cases it has to be initialized like this together with declaration, i.e. val variable = object : AClass() ....
Public/protected properties need a type which can be used outside the class which declares them, unsurprisingly. In that case you'll have to name the subclass.
The page you link covers this
Note that anonymous objects can be used as types only in local and private declarations. If you use an anonymous object as a return type of a public function or the type of a public property, the actual type of that function or property will be the declared supertype of the anonymous object, or Any if you didn't declare any supertype. Members added in the anonymous object will not be accessible.

pin_ptr of List rather than array

I use pin_ptr for cli::array types and everything works fine.
Is it possible to do the same with System::Collection::Generic::List which I believe is a contiguous block of memory?
The obvious
List<double>^ stuff = gcnew List<double>( 10 );
cli::pin_ptr<double> resultPtr = &stuff[ 0 ];
gives a compiler error "error C2102: '&' requires l-value" presumably because the indexed property returns something that is not a l-value! So is there another way to do this. I have played around with interior_ptr as well but have not found anything that works yet.
I know that I could call ToArray on the List but the whole point is to not copy stuff around.
No, this is not possible.
True, a List does use an array behind the scenes, but the [] operator is different. With an array, [] is simple pointer math, but with a List, [] is a full-fledged method call. That's why the & isn't working: you can take the address of an array location, but you can't take the address of a value returned from a method.
Think about it like this: If they wanted to, they could change the implementation of List without changing its external interface. It would be possible to change List to store the list contents in memory gzip-compressed. In that case, stuff[0] is generated on-the-fly by the [] method which does the decompression, so there is no single memory location that contains stuff[0] to pin.
Edit
Yes, internal to the List class, the contents are contiguous in memory. You can see this in the source that Microsoft has provided. However, the List class does not make that array public: The public interface to the List class is the public methods & properties, only. The public methods & properties present a contract, and the array that the values are stored in are not part of that contract. Microsoft would never do this, but they could do a gzip-compressed implementation of List, and the public contract of the List class wouldn't change. You should only write your code to the public methods & properties of a class, not to the internals that may change at any time.

how to insert tokens or contexts in my listener class in antlr4

I have an antlr4 grammar file that parses a BASIC language. Is there a way to insert more code in my extended baseListener class?
For example, if I am parsing this code:
10 print "hello world"
%include "moreCode.bas"
print "after include"
moreCode.bas could be something like:
for t% = 1% to 10%
print t%
next t%
I need to detect the include command and include the contents into the file being walked and continue walking it as a whole.
So I was thinking that in my enterIncludeCommand method in my listener class I would start a new parser for moreCode.bas and then somehow insert the tokens/contexts into my current one.
What is the correct way of doing this?
There is no one right pattern. That said, one effective way is to have your main initiate the parser by always calling through a constructor that takes a state object and a source path as parameters
public class BasicParser {
public static void main(String[] args) {
...
StateModel state = new StateModel()
RecurseParser rp = new RecurseParser(state, pathname);
...
}
}
public class RecurseParser {
public RecurseParser(StateModel state, String pathname) {
this.state = state;
this.pathname = pathname; // source text to parse
...
}
public StateModel getResults() {
return this.state
}
In your enterIncludeStatement method, create and run a new RecurseParser instance directly. In your exitIncludeStatement, retrieve the new current state and, as appropriate, validate/check for errors.
Since the state model encapsulates your symbol table, etc., you maintain continuity as you walk through the forest -- recursion is effectively free.
Should mention that, relative to the symbol table, treat executing an include essentially the same as calling a subroutine.
Related: Symbol Table
I have two solutions for this and I took the last one I am going to mention. Also GRosenBerg has a great idea too.
1) use the TokenStreamRewriter and in the enterIncludeStatement use the rewriter insertBefore, insertAfter and/or replace methods. At the end of the walk of that particular listener object, call the rewriter getText() and that will give you the combined string. You will have to reparse that text to go the next listener pass.
2) In the enterIncludeStatement method of the listener class, get the include file name, run the lexer/parser on it and then take the first StatementContext(in my case) and inject it into the current tree using the IncludeContext.AddChile(myStatement). Looping for each statement line in that include file. The tricky part is to include the statements in the correct place but you will end up with a complete tree that you can walk with the next listener class phase.
I used option 2 and its working for me so far however I'm not sure using the addChild method is the best way since I am really inserting siblings not children. Given this siblings/childrens issue then maybe grosenberg's recursive idea would be the best.

GPARs async functions and passing references that are being updated by another thread

I am using GPARs asynchronous functions to fire off a process as each line in a file is parsed.
I am seeing some strange behavior that makes me wonder if I have an issue with thread safety.
Let's say I have a current object that is being loaded up with values from the current row in an input spreadsheet, like so:
Uploader {
MyRowObject currentRowObject
}
Once it has all the values from the current row, I fire off an async closure that looks a bit like this:
Closure processCurrentRowObject = { ->
myService.processCurrentRowObject (currentRowObject)
}.asyncFun()
It is defined in the same class, so it has access to the currentRowObject.
While that is off and running, I parse the next row, and start by creating a new object:
MyObject currentObject = new MyObject()
and start loading it up with values.
I assumed that this would be safe, that the asynchronous function would be pointing to the previous object. However, I wonder if because I am letting the closure bind to the reference, if somehow the reference is getting updated in the async function, and I am pulling the object instance out from under it, so to speak - changing it while it's trying to work on the previous instance.
If so, any suggestions for fixing? Or am I safe?
Thanks!
I'm not sure I fully understand your case, however, here's a quick tip.
Since it is always dangerous to share a single mutable object among threads, I'd recommend to completely separate the row objects used for different rows:
final localRowObject = currentRowObject
currentRowObject = null
Closure processCurrentRowObject = { ->
myService.processCurrentRowObject (localRowObject)
}.asyncFun()

How to remove the bounce off colliding objects in box2d?

I am trying to delete an object in box2d when two objects collide.
When my two objects do collide, one of the object bounces off the other. It does delete the other object, but I want it to make it look like it went through rather than a bounce.
I have my body Def type set to b2_staticBody.
You should set the body's fixture to be a sensor:
fixture->SetSensor(true);
You then create a contact listener (class MyContactListener : public b2ContactListener) that detects collisions in the BeginContact method and checks if one of the colliding objects is of this special kind. A good way of doing that is by using these two methods:
/// Get the user data pointer that was provided in the body definition.
void* GetUserData() const;
/// Set the user data. Use this to store your application specific data.
void SetUserData(void* data);
You need to be a bit familiar with C++ to pull it off.