C
C#3y ago
daveeska

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
qqdev
qqdev3y ago
Can you show some code?
daveeska
daveeskaOP3y ago
which part?
cap5lut
cap5lut3y ago
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
bool shouldDrawBlock = false;

void OnMouseButtonDown() // ur button click handler
{
shouldDrawBlock = true;
}

void Render() // ur render method
{
if (shouldDrawBlock)
{
drawBlock();
}
}
bool shouldDrawBlock = false;

void OnMouseButtonDown() // ur button click handler
{
shouldDrawBlock = true;
}

void Render() // ur render method
{
if (shouldDrawBlock)
{
drawBlock();
}
}
daveeska
daveeskaOP3y ago
but that only works once and the block that drawn are the same I want it to draw difference entity everytime it getclick
cap5lut
cap5lut3y ago
hmm, ur description is a bit too vague for me to really help ya, can ya give more details?
daveeska
daveeskaOP3y ago
if I do your way,the block that drawn is the same if I click it again, the nothing will happen (i belive)
cap5lut
cap5lut3y ago
well, if ya want it to toggle, then just use shouldDrawBlock = !shouldDrawBlock; in the button click handler
daveeska
daveeskaOP3y ago
but it still only work once right? cuz after it true for once, its going to be flse again which make the block dissappear
cap5lut
cap5lut3y ago
that should persist the state unless ur shouldDrawBlock is a local variable instead of a member of ur class
daveeska
daveeskaOP3y ago
wdym? any example code?
cap5lut
cap5lut3y ago
something like
public abstract class GameObject
{
public bool IsVisible { get; set; } = true;

public abstract void Draw();
}

public class Game
{
private readonly List<GameObject> _objects = new(); // fill with some actual objects

public void OnMouseClick()
{
GameObject gameObject = GetGameObjectFromMouseCoordsOrSomething();
gameObject.IsVisible = !gameObject.IsVisible;
}

public void Draw()
{
foreach (var gameObject in _objects)
{
if (gameObject.IsVisible)
{
gameObject.Draw();
}
}
}
}
public abstract class GameObject
{
public bool IsVisible { get; set; } = true;

public abstract void Draw();
}

public class Game
{
private readonly List<GameObject> _objects = new(); // fill with some actual objects

public void OnMouseClick()
{
GameObject gameObject = GetGameObjectFromMouseCoordsOrSomething();
gameObject.IsVisible = !gameObject.IsVisible;
}

public void Draw()
{
foreach (var gameObject in _objects)
{
if (gameObject.IsVisible)
{
gameObject.Draw();
}
}
}
}
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.
daveeska
daveeskaOP3y ago
why does it need to an abstract class?
cap5lut
cap5lut3y ago
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
daveeska
daveeskaOP3y ago
GetGameObjectFromMouseCoordsOrSomething(); part is for the obj pos right?
daveeska
daveeskaOP3y ago
like that?
daveeska
daveeskaOP3y ago
daveeska
daveeskaOP3y ago
now it's doing this
cap5lut
cap5lut3y ago
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
// update state
if (Raylib.IsMouseButtonPressed(MouseButton.MOUSE_BUTTON_LEFT))
{
var pos = Raylib.GetMousePosition();
foreach (var obj in _objects)
{
if (/*check if pos is inside of the tile boundaries*/)
{
obj.IsVisible = !tile.IsVisible;
break;
}
}
}

// render scene
foreach (var obj in _objects)
{
if (obj.IsVisible)
{
obj.Draw();
}
}
// update state
if (Raylib.IsMouseButtonPressed(MouseButton.MOUSE_BUTTON_LEFT))
{
var pos = Raylib.GetMousePosition();
foreach (var obj in _objects)
{
if (/*check if pos is inside of the tile boundaries*/)
{
obj.IsVisible = !tile.IsVisible;
break;
}
}
}

// render scene
foreach (var obj in _objects)
{
if (obj.IsVisible)
{
obj.Draw();
}
}
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:
public class ObservableValue<T>
{
private T _value;

public ObservableValue(T initialValue)
{
_value = initialValue;
}

public T Value
{
get => _value;
set
{
if (value is null)
{
if (_value is null) return;
}
else if (value.Equals(_value))
{
return;
}

var oldValue = _value;
_value = value;
OnChange?.Invoke(oldValue, value);
}
}

public event Action<T, T>? OnChange;
}
public class ObservableValue<T>
{
private T _value;

public ObservableValue(T initialValue)
{
_value = initialValue;
}

public T Value
{
get => _value;
set
{
if (value is null)
{
if (_value is null) return;
}
else if (value.Equals(_value))
{
return;
}

var oldValue = _value;
_value = value;
OnChange?.Invoke(oldValue, value);
}
}

public event Action<T, T>? OnChange;
}
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
cap5lut
cap5lut3y ago
oh, then forget all about the IsVisble, then ya can simple add the new block to object list before rendering
if (Raylib.IsMouseButtonPressed(MouseButton.MOUSE_BUTTON_LEFT))
{
_objects.Add(new Block {
Position = Raylib.GetMousePosition()
});
}
if (Raylib.IsMouseButtonPressed(MouseButton.MOUSE_BUTTON_LEFT))
{
_objects.Add(new Block {
Position = Raylib.GetMousePosition()
});
}
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
cap5lut
cap5lut3y ago
well i guess u have a drawing method like
public void Draw()
{
foreach (var gameObject in _objects)
{
gameObject.Draw();
}
}
public void Draw()
{
foreach (var gameObject in _objects)
{
gameObject.Draw();
}
}
to draw all ur entities. by adding the block to the _objects list, it will be rendered on the next frame
Want results from more Discord servers?
Add your server