Jump to content

Passing function into class for later use

SilicateWielder

Hello,

 

I am currently beginning development on a game, however while writing the code for the GUI I wanted to implement the ability to "inject" code into frame objects as a method of rendering objects within said frames. Any and all methods of doing so that I have googled or though of thus far have failed. Not only do I need ot inject the code into the class but the injected code also has to be able to access variables within said class. If anyone could help me or teach me how to do this it would be appreciated.

 

This is basically how the code for frames.js looks; just without all of the code that actually does stuff (I'm not on my home PC)

function frame (parameters) {
	this.func = null;
	//Code goes here
}

frame.prototype = {}

frame.prototype.render = function (parameters) {
	//Code goes here
	if (this.func != null) {
		this.func();
	}
}

frame.prototype.move = function (parameters) {
	//Code goes here
}

frame.prototype.bind = function (func) {
	this.func = new func()
}

 

Currently frame.bind has been removed from the code and right now I'm just running the script within the main loop that performs all of my rendering. It produces the following result with some sample code. 

 

screenshot_4.png

My procrastination is the bane of my existence.

I make games and stuff in my spare time.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

So your function that you're binding will need to have access to the frame that calls it?

 

Can you explain me what does "new func()" do? I have never seen it and I legitimately asking. But even not knowing that I can propose a solution.

 

frame.prototype.bind = function (func) {
	this.func = func;
}
frame.prototype.render = function (parameters) {
	//Code goes here
	if (this.func !== null) {
		this.func(this);
	}
}

So notice I'm passing frame instance to your bound function call, so the function you wanna bind has to take frame instance as an argument.

 

P.S.: Just to be sure, it is javascript, right?

Link to comment
Share on other sites

Link to post
Share on other sites

(Also assuming this is Javascript)

 

Create a var that's a function then pass it in like any argument. For example:

var foobar = function(test){
  console.log("This is a test:", test);
}

var foobar_user = function(callback){ 
  callback(1); 
}

foobar_user(foobar);

Will print out "This is a test: 1"

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, M.Yurizaki said:

(Also assuming this is Javascript)

 

Create a var that's a function then pass it in like any argument. For example:


var foobar = function(test){
  console.log("This is a test:", test);
}

var foobar_user = function(callback){ 
  callback(1); 
}

foobar_user(foobar);

Will print out "This is a test: 1"

so then I should be able to pass foobar to another variable for reference? i.e:

var test = function(code) {
	this.code = code;
}

test.prototype = {}

test.prototype.run = function() {
	this.code();
}

And so test.run(); would return output from said function, correct?

My procrastination is the bane of my existence.

I make games and stuff in my spare time.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, M.Yurizaki said:

Your thingy is blank :P

had to cheat in order to get code tags in. :P I just updated the post

My procrastination is the bane of my existence.

I make games and stuff in my spare time.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, UnbrokenMotion said:

had to cheat in order to get code tags in. :P I just updated the post

Yeah. Though you have to make sure when you call the referenced function to include the arguments as well, if any. For example, if you passed in foobar and did test.run, it would fail since foobar is expecting an argument.

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, M.Yurizaki said:

Yeah. Though you have to make sure when you call the referenced function to include the arguments as well, if any. For example, if you passed in foobar and did test.run, it would fail since foobar is expecting an argument.

So even if I were to run test(foobar("This is a test")); I would still have to include parameters when running test.run();?

 

Is it also possible to have a function that I am passing use this.variableNameHere to reference a variable within the object it has been passed to? Because writing the code like I have below isn't the most ideal solution.

statsFrame = new Frame (200, 400, palette[0], palette[1], "Player Stats;");

testFrame = new Frame(width, height, colorA, colorB, title);

testFrame.bind(foobar("This is a test");

while (true) {
	statsFrame.render(true);
	testFrame.render(true, testFrame.pos); //This code here.

	FlipScreen();
}

 

My procrastination is the bane of my existence.

I make games and stuff in my spare time.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Your original approach was ok, just the execution was bad. Try not to ignore my post and you may find it useful.

Link to comment
Share on other sites

Link to post
Share on other sites

37 minutes ago, UnbrokenMotion said:

So even if I were to run test(foobar("This is a test")); I would still have to include parameters when running test.run();?

 

Is it also possible to have a function that I am passing use this.variableNameHere to reference a variable within the object it has been passed to? Because writing the code like I have below isn't the most ideal solution.

Basically when you pass a function as an argument, using it is just like any function call. So you don't have to have a parameter for test.run, but when you use the passed-in function, you still need to adhere to its needs. So test.run run needs to call:

this.code();

With an argument. So you should do something like this:

test.prototype.run = function() {
	var some_arg = 1
	this.code(some_arg);
}

 

I would suggest playing around with the idea to get a handle on how this works :P

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, M.Yurizaki said:

Basically when you pass a function as an argument, using it is just like any function call. So you don't have to have a parameter for test.run, but when you use the passed-in function, you still need to adhere to its needs. So test.run run needs to call:


this.code();

With an argument. So you should do something like this:


test.prototype.run = function() {
	var some_arg = 1
	this.code(some_arg);
}

 

I would suggest playing around with the idea to get a handle on how this works :P

Ah okay, so I should be able to just write test.run as this; assuming that the parameters I need are stored locally within test.

 


test.prototype.run = function() {

  this.code(some_arg);

}

 

Thank you, this has been greatly helpful.

My procrastination is the bane of my existence.

I make games and stuff in my spare time.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

39 minutes ago, Mr_KoKa said:

Your original approach was ok, just the execution was bad. Try not to ignore my post and you may find it useful.

So, assuming the approach remains the same, how could I improve the execution?

My procrastination is the bane of my existence.

I make games and stuff in my spare time.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, UnbrokenMotion said:

So, assuming the approach remains the same, how could I improve the execution?

My first post has it all, small fix to bind function, and small addition to calling bound function in render function. Your function bound by bind function would have full access to frame object as it would be passed to it by argument.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×