Quick question to #Godot #gamedev people (as usual right now, haha ).
-
Miredlyreplied to Natasha Nox πΊπ¦π΅πΈ last edited by
@Natanox Assuming your box object is being instantiated by your "Main" script, you could have a reference to your player node in the Main script so you can connect the signal on instantiation?
Or If the player is a singleton you could keep a global reference to it somewhere so you can always access it from any script. (this is how I would do it)
-
Feyterreplied to Natasha Nox πΊπ¦π΅πΈ last edited by
@Natanox I don't really get your problem here.
Do you want to know how to connect Signals or do you want to know how to get "tilesize" from player?
For the signal connecting stuff I would first point to:
https://docs.godotengine.org/en/stable/getting_started/step_by_step/signals.html -
Natasha Nox πΊπ¦π΅πΈreplied to Miredly last edited by
@Miredly Can I connect signals in reverse?
"box" is sending the signal (containing its own tilesize - if there are better ways to save / deliver information about instantiated nodes please let me know), "Player Character" is trying to connect to it. -
Natasha Nox πΊπ¦π΅πΈreplied to Feyter last edited by
@feyter "box" is emiting a signal containing "tilesize". I want "Player Character" to connect to it.
*however* the level name changes dynamically, so "box" might be called differently in the future.
Of course I already read the docs, my problem is more about understanding how nodes interact (or rather don't interact / only do so via global functions). Because I still struggle seeing them as simple classes with childs I could traverse, which they apparently are not.^^
-
Feyterreplied to Natasha Nox πΊπ¦π΅πΈ last edited by
@Natanox you don't connect a node to an signal.
You connect a function to a Signal. This connection could be done everywhere, where a reference to the signal and the function exists.
In your shown scene tree I would say best place for this would be "main".
-
Natasha Nox πΊπ¦π΅πΈreplied to Feyter last edited by
@feyter Eh yeah, sorry. Semantic issue, I meant the script of Player Character in general.
So I just figured out
get_parent().get_node("box").tilesize.connect(move)
in the Player Character script ("move" is a function), but obviously this is pretty convoluted. I obviously used the "connect" method wrongly earlier, it just didn't threw me an error doing so.
I think I got it now though, thanks!
-
Luke Millerreplied to Natasha Nox πΊπ¦π΅πΈ last edited by
@Natanox I'm a massive believer in a singleton file and placing my signals in there.
eg I create: globals.gd and add it as a Globals (Project Settings->Globals). Inside globals.gd I have:
@signal immediate_broadcast_tilesize
In Box I do:
Globals.immediate_broadcast_tilesize.emit(my_data)In Character _ready I do:
Globals.immediate_broadcast_tilesize.connect(handle_immediate_broadcast_tilesize)
I do this route because my code was spaghettifying connecting parents to children's children
-
Jonathan Hartleyreplied to Natasha Nox πΊπ¦π΅πΈ last edited by
@Natanox FWIW, if you decide to, you CAN access other nodes from anywhere. To do so, you just have to figure how to reference them.
Something like get_node("../OtherNode") might work to reference another child of our parent.
-
Jonathan Hartleyreplied to Jonathan Hartley last edited by
@Natanox but having said that, I would have expected all the ideas you describe above could be made to work. I suspect some detail is making them not work, a typo maybe?
-
Jonathan Hartleyreplied to Jonathan Hartley last edited by
@Natanox On reflection, I would not use a signal for this. If you disagree, that is fine, doing so will work just fine if you iron out the details.
But what I would do is have code in player character look up the tile_size value from box whenever they need to use it. ...
-
Jonathan Hartleyreplied to Jonathan Hartley last edited by
@Natanox in box, have a variable for the tile size:
var tile_size : int
And populate this in _ready or whatever.
Then in player character, there are many ways to use that value. I'm not at a keyboard so spitballing now, but something like:
get_node("../box").tile_size
Or you could mark "box" as a globally accessible node from the right click menu on it (I think it marks the node with a % symbol?) and then just refer to it using:
%box.tile_size
or maybe it is:
$box.tile_size
-
Jonathan Hartleyreplied to Jonathan Hartley last edited by
@Natanox I wish we could pair on this to just get you working. Aha, in fact, if it isn't too weird for you, I'll happily jump on a Google meet or something so we could both see your code and try to make this work. I'm in Central US timezone, busy workday coming up, but could happily make it work.
-
Natasha Nox πΊπ¦π΅πΈreplied to Jonathan Hartley last edited by
@tartley Oh wow, thanks for the offering. π€ Unfortunately (or, well, fortunately I'd say) I don't have Google Meet. Best I can do is Matrix, Signal as well as Discord (as last resort). I'm more of a "text person" though.
You will eventually see my code though, my learning project is a small Snake clone I want to open-source once it is completed. Perhaps I do it earlier if I need even more help, that way anyone interested could just clone the repo.
-
Ponder Stibbons π§π·π©πͺreplied to Luke Miller last edited by
@upmultimedia
Thank you for counter arguing the church of "globals are evil". Some people can't get fracking over it.
@Natanox -
Natasha Nox πΊπ¦π΅πΈreplied to Ponder Stibbons π§π·π©πͺ last edited by
@blackcoffeerider Makes most sense with signals indeed! Although now I also learned a lot about Godot doing it the other way. π€
With bigger projects containing lots of entities I'll probably do it the way @upmultimedia said. Perhaps with something like a dictionary containing arrays filled with all the signals and perhaps additional info of enemies or other entities. That way I could cycle through them with a simple 'for' loop if necessary.