How to make a function stays
I want to make like building mechanic to my game, but if I just "if(buttonclick){drawBlock}", the block just stays when the button is being clicked, and disappear when the button released
22 Replies
Can you show some code?
which part?
u could use a simple boolean variable, which is set to true on button click. during rendering u would check the variable to determine if u want draw the block or not
but that only works once
and the block that drawn are the same
I want it to draw difference entity everytime it getclick
hmm, ur description is a bit too vague for me to really help ya, can ya give more details?
if I do your way,the block that drawn is the same
if I click it again, the nothing will happen
(i belive)
well, if ya want it to toggle, then just use
shouldDrawBlock = !shouldDrawBlock;
in the button click handlerbut it still only work once right?
cuz after it true for once, its going to be flse again
which make the block dissappear
that should persist the state unless ur
shouldDrawBlock
is a local variable instead of a member of ur classwdym?
any example code?
something like
ur block would be an instance of a type that inherits from
GameObject
and be stored in Game._objects
now each block (and other game objects) have their own visibility state and they will be only rendered if its true.why does it need to an abstract class?
because its a base type that doesnt know how to draw itself, that would be defined by subclasses
and ofc its just a rough example
GetGameObjectFromMouseCoordsOrSomething();
part is for the obj pos right?like that?
now it's doing this
so when u click on the block u actually want to hide it, and when u click on it again u want to show it, right?
if yes, that would mean, that u would first want to find the correct
Ground
tile u clicked on
i dunno anything about Raylib, so if (Raylib.IsMouseButtonPressed(MouseButton.MOUSE_BUTTON_LEFT))
could cause some flickering, then u would have to write some mechanic to only trigger it on actual changes
for the latter something like:
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
oh, then forget all about the
IsVisble
, then ya can simple add the new block to object list before rendering
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
well i guess u have a drawing method like
to draw all ur entities.
by adding the block to the
_objects
list, it will be rendered on the next frame