Re: Error when using `import`.

2024-02-24 Thread Liam McGillivray via Digitalmars-d-learn
On Saturday, 24 February 2024 at 10:34:25 UTC, Richard (Rikki) Andrew Cattermole wrote: A few things. Module names should be lower case. I capitalised the first letter in the class names so that I can make instances of them in lowercase. Should I rename the classes, modules, and filenames

Error when using `import`.

2024-02-24 Thread Liam McGillivray via Digitalmars-d-learn
I am trying to make a tactical role-playing game which I am likely to open-source. Right now the code is very simple, with only 3 files each containing a class with the same name. The files are `Map.d`, `Tile.d`, and `Unit.d`. Each of these files contains an `import` line to access one of the

Re: Error when using `import`.

2024-02-26 Thread Liam McGillivray via Digitalmars-d-learn
I don't know whether I should continue this topic or start a new one now that the problem mentioned in the title is fixed. I have now uploaded some of the code to [a GitHub repository](https://github.com/LiamM32/Open_Emblem). To make this game usable, I will need a library for graphics and

Re: Error when using `import`.

2024-02-26 Thread Liam McGillivray via Digitalmars-d-learn
On Sunday, 25 February 2024 at 03:23:03 UTC, Paul Backus wrote: You can't give a class the same name as the file it's in. If you do, then when you try to use it from another file, the compiler will get confused and think you're referring to the file instead of the class (that's what "import is

Re: Error when using `import`.

2024-02-26 Thread Liam McGillivray via Digitalmars-d-learn
On Tuesday, 27 February 2024 at 03:06:19 UTC, Steven Schveighoffer wrote: If you are going for game development, I would recommend raylib-d (https://code.dlang.org/packages/raylib-d), which is my wrapper around the very good raylib library. For doing GUI, raygui is supported, but I also can

Re: Error when using `import`.

2024-02-29 Thread Liam McGillivray via Digitalmars-d-learn
I now have the Raylib functions working by using `toStrinz`. I pushed some updates to the repository. I made the main project a source library so that I can experiment with different graphics library front-ends. I put have the front-end using Raylib in the `raylib_frontend` directory. It

Re: Error when using `import`.

2024-02-27 Thread Liam McGillivray via Digitalmars-d-learn
On Tuesday, 27 February 2024 at 03:43:56 UTC, Liam McGillivray wrote: Raylib looks promising. I installed it along with your Raylib-d. I managed to build the example you provided with dub, but trying to use it in it's own dub project in a separate directory isn't working. Just copying and

Re: Error when using `import`.

2024-02-28 Thread Liam McGillivray via Digitalmars-d-learn
There's something very strange going on when using Raylib-D. I tried using the raylib function `LoadTexture` like this: ``` tileSprites[i] = LoadTexture("../sprites/" ~ spriteName); ``` I got the following error: ``` Error: function `raylib.LoadTexture(const(char)* fileName)` is not callable

Array types and index types

2024-02-27 Thread Liam McGillivray via Digitalmars-d-learn
In D, it appears that dynamic arrays (at least by default) use a ulong as their key type. They are declared like this: ``` string[] dynamicArray; ``` I imagine that using a 64-bit value as the key would be slower than using 32 bits or 16 bits, and 64 bits is way overkill for nearly

Can a D library have some types determined by the client program?

2024-03-07 Thread Liam McGillivray via Digitalmars-d-learn
In a source library written in D, is it possible to have some objects, variables, pointers etc which are determined by the program using the library? An example of where this would be useful is in the library I am currently writing. I have a class called `Map`, which holds an array of

Re: Can a D library have some types determined by the client program?

2024-03-07 Thread Liam McGillivray via Digitalmars-d-learn
On Thursday, 7 March 2024 at 22:18:40 UTC, Richard (Rikki) Andrew Cattermole wrote: There are two ways to do this. 1. Use templates. https://tour.dlang.org/tour/en/basics/templates 2. Use a factory function. https://tour.dlang.org/tour/en/basics/delegates ```d class Map(ATile : Tile) {

Re: Can a D library have some types determined by the client program?

2024-03-07 Thread Liam McGillivray via Digitalmars-d-learn
On Friday, 8 March 2024 at 03:19:59 UTC, Richard (Rikki) Andrew Cattermole wrote: On 08/03/2024 4:09 PM, Liam McGillivray wrote: Thank you. Is this first example you gave the template? Is the syntax `(ATile : Tile)` saying that ATile must be a derived class of Tile? If this isn't worse in

Re: Can a D library have some types determined by the client program?

2024-03-09 Thread Liam McGillivray via Digitalmars-d-learn
Update on two things: One is that I now better understand what it means that D objects are "reference by default". This means that references *can* be null if they are declared with a class. In my commits last night, I have changed many pointers into references. I think my time will be

Challenge: Make a data type for holding one of 8 directions allowing increment and overflow

2024-03-11 Thread Liam McGillivray via Digitalmars-d-learn
I am in need of a data type for holding direction information; one of 8 directions on a single axis. They are named in terms of compass directions. If D had a 4-bit datatype, I would just use this and do `+=2` whenever I want to change the datatype, but it doesn't. Perhaps this would be a

Re: Can a D library have some types determined by the client program?

2024-03-09 Thread Liam McGillivray via Digitalmars-d-learn
I have made a new branch of my project called "templates-interfaces" which reworks some things, and turns the Map class into an interface and template. It is now functioning like the master branch, but I think the code should now be (arguably) easier to follow. At least that's true for the

Re: Can a D library have some types determined by the client program?

2024-03-10 Thread Liam McGillivray via Digitalmars-d-learn
On Sunday, 10 March 2024 at 04:39:33 UTC, Liam McGillivray wrote: https://github.com/LiamM32/Open_Emblem/tree/templates-interfaces I will probably merge it into master soon. I have put up a merge request for these changes I have made to the library and the Raylib front-end. I would be

Re: Challenge: Make a data type for holding one of 8 directions allowing increment and overflow

2024-03-12 Thread Liam McGillivray via Digitalmars-d-learn
On Tuesday, 12 March 2024 at 06:38:28 UTC, Richard (Rikki) Andrew Cattermole wrote: By taking advantage of integer wrapping and a bitwise and, its quite a simple problem to solve! Challenge for the reader: add support for binary operations and toString support.

Re: Challenge: Make a data type for holding one of 8 directions allowing increment and overflow

2024-03-14 Thread Liam McGillivray via Digitalmars-d-learn
On Thursday, 14 March 2024 at 01:58:46 UTC, Richard (Rikki) Andrew Cattermole wrote: The cost of an add + increment then a bitwise and is only 2-4 cycles on a Haswell cpu. Depending upon if its working solely in registers (via inlining) or its operating on ram. Whereas if you need to do

Re: Reworking the control flow for my tactical role-playing game

2024-03-21 Thread Liam McGillivray via Digitalmars-d-learn
On Thursday, 21 March 2024 at 16:48:39 UTC, Steven Schveighoffer wrote: On Sunday, 17 March 2024 at 00:14:55 UTC, Liam McGillivray wrote: As many of you know, I have been trying to write a tactical role-playing game (a mix of turn-based stategy & RPG) in D. This is the furthest I have ever

Optimization when using a 2-dimensional array of objects

2024-03-21 Thread Liam McGillivray via Digitalmars-d-learn
In the [game I am currently making](https://github.com/LiamM32/Open_Emblem/blob/master/oe-raylib/source/app.d), I have a `Map` class (actually a combination of an interface & class template, but I'll call it a "class" for simplicity), in which there will probably be only one instance running at a

Re: Challenge: Make a data type for holding one of 8 directions allowing increment and overflow

2024-03-16 Thread Liam McGillivray via Digitalmars-d-learn
On Friday, 15 March 2024 at 17:25:09 UTC, Daniel N wrote: On Tuesday, 12 March 2024 at 05:38:03 UTC, Liam McGillivray wrote: I am in need of a data type for holding direction information; one of 8 directions on a single axis. They are named in terms of compass directions. If D had a 4-bit

Reworking the control flow for my tactical role-playing game

2024-03-16 Thread Liam McGillivray via Digitalmars-d-learn
As many of you know, I have been trying to write a tactical role-playing game (a mix of turn-based stategy & RPG) in D. This is the furthest I have ever gotten in making an interactive program from the main function up. Right now, it is not yet playable as a game, but you can interact with it

Re: Challenge: Make a data type for holding one of 8 directions allowing increment and overflow

2024-03-16 Thread Liam McGillivray via Digitalmars-d-learn
On Friday, 15 March 2024 at 00:21:42 UTC, H. S. Teoh wrote: On Thu, Mar 14, 2024 at 11:39:33PM +, Liam McGillivray via Digitalmars-d-learn wrote: [...] I tried to rework the functions to use bitwise operations, but it was difficult to figure out the correct logic. I decided that it's

Re: Error when using `import`.

2024-03-05 Thread Liam McGillivray via Digitalmars-d-learn
I have made some progress on this. For the raylib front-end, I tried making a class called `Mission` which inherits `Map`. This class handles the graphics, input, and other game events. The program now compiles without errors, and there are some graphics. I have pushed these updates to the

Re: Error when using `import`.

2024-03-05 Thread Liam McGillivray via Digitalmars-d-learn
There's something that I'm trying to do that D may or may not be capable of. In the Map class, there is a 2-dimensional array called `grid`, where the Tile objects are stored. The Mission class inherits the Map class. In the Mission class, I want the `grid` array to instead be composed of

Re: Can a D library have some types determined by the client program?

2024-03-08 Thread Liam McGillivray via Digitalmars-d-learn
On Friday, 8 March 2024 at 16:54:48 UTC, cc wrote: If you don't want Unit to be a template, you can just have Map derive from a basic interface or abstract class. You can also have every relevant class share similar templates, you just need to remember to supply the template arguments

Why am I getting segfaults when doing `foreach` with arrays of references?

2024-03-08 Thread Liam McGillivray via Digitalmars-d-learn
With [my game project](https://github.com/LiamM32/Open_Emblem), I have been getting segmentation faults that are unexplainable at my knowledge level. They seem to happen when doing a "foreach" loop through an array of references. Skip to the bolded text if you don't want to read too much, as

Re: Why am I getting segfaults when doing `foreach` with arrays of references?

2024-03-08 Thread Liam McGillivray via Digitalmars-d-learn
On Saturday, 9 March 2024 at 06:37:02 UTC, Richard (Rikki) Andrew Cattermole wrote: Something that I have noticed that you are still doing that was pointed out previously is having a pointer to a class reference. Stuff like ``Tile* currentTile;`` when it should be ``Tile currentTile;`` A

Re: Why am I getting segfaults when doing `foreach` with arrays of references?

2024-03-09 Thread Liam McGillivray via Digitalmars-d-learn
On Saturday, 9 March 2024 at 07:49:52 UTC, Liam McGillivray wrote: But that begs the question; why? Don't dynamic arrays always start with a length of 0? If the array was only extended when valid objects were appended using the append operator `~=`, and none of those objects were deleted (as I

Re: Challenge: Make a data type for holding one of 8 directions allowing increment and overflow

2024-03-13 Thread Liam McGillivray via Digitalmars-d-learn
On Tuesday, 12 March 2024 at 06:38:28 UTC, Richard (Rikki) Andrew Cattermole wrote: By taking advantage of integer wrapping and a bitwise and, its quite a simple problem to solve! Challenge for the reader: add support for binary operations and toString support. Last night I pushed the

Re: Reworking the control flow for my tactical role-playing game

2024-03-24 Thread Liam McGillivray via Digitalmars-d-learn
On Saturday, 23 March 2024 at 04:32:29 UTC, harakim wrote: * You should probably not do this, but it might give you some ideas for later. What I would do is make a separate thread for managing the UI state and push events to that thread through the mailbox. I have done this once (on my third

Re: Optimization when using a 2-dimensional array of objects

2024-03-22 Thread Liam McGillivray via Digitalmars-d-learn
On Friday, 22 March 2024 at 07:34:33 UTC, monkyyy wrote: Is one option more efficient than the other? You should probaly do the lazyest thing, factor out your "ispassable" logic, like what your walking n of 3, n of 8, n of 15? so long as you dont do something insane it will be fast on a

Re: Can a D library have some types determined by the client program?

2024-03-25 Thread Liam McGillivray via Digitalmars-d-learn
On Thursday, 7 March 2024 at 22:18:40 UTC, Richard (Rikki) Andrew Cattermole wrote: There are two ways to do this. 1. Use templates. https://tour.dlang.org/tour/en/basics/templates Thank you for teaching me how to do this. This is where I first learned to use templates in D, and I have been

Re: How do I use libdparser, or any library for editing D code?

2024-03-25 Thread Liam McGillivray via Digitalmars-d-learn
Unfortunately there's no "edit" option here, but the library I was referring to is actually "libdparse".

Re: Unittests pass, and then an invalid memory operation happens after?

2024-03-27 Thread Liam McGillivray via Digitalmars-d-learn
On Wednesday, 27 March 2024 at 22:14:16 UTC, H. S. Teoh wrote: What's the definition of this.map, this.faction, and this.currentTile? As was said, and can be found on the linked repository, they are references to class objects. On Thursday, 28 March 2024 at 01:47:27 UTC, Steven

Re: Unittests pass, and then an invalid memory operation happens after?

2024-04-03 Thread Liam McGillivray via Digitalmars-d-learn
On Friday, 29 March 2024 at 01:18:22 UTC, H. S. Teoh wrote: Take a look at the docs for core.memory.GC. There *is* a method GC.free that you can use to manually deallocate GC-allocated memory if you so wish. Keep in mind, though, that manually managing memory in this way invites

How best to implement items and weapons in my RPG. Nested classes? Delegates?

2024-04-03 Thread Liam McGillivray via Digitalmars-d-learn
For my tactical role-playing game, I want to finally implement items and weapons. These are objects that a `Unit` object can make use of. Just as tools are often said to be an extension of their user, I want items to be an extension of the unit who uses them. I'm trying to figure out how to

"Error: `TypeInfo` cannot be used with -betterC" on a CTFE function

2024-04-07 Thread Liam McGillivray via Digitalmars-d-learn
I'm making a modification to a D binding for a C library. I made a CTFE function which takes a function declaration with one or more `const(char)*` or `char*` parameters and makes an overload that accepts D strings. While this function is only used during compile time, unfortunately, I have

Re: Unittests pass, and then an invalid memory operation happens after?

2024-03-28 Thread Liam McGillivray via Digitalmars-d-learn
On Thursday, 28 March 2024 at 04:46:27 UTC, H. S. Teoh wrote: The whole point of a GC is that you leave everything up to it to clean up. If you want to manage your own memory, don't use the GC. D does not force you to use it; you can import core.stdc.stdlib and use malloc/free to your heart's

Re: Reworking the control flow for my tactical role-playing game

2024-03-23 Thread Liam McGillivray via Digitalmars-d-learn
On Saturday, 23 March 2024 at 04:32:29 UTC, harakim wrote: You should engineer the system in a way that makes sense to you. The currency of finishing programs is motivation and that comes from success and believing you will succeed. If you're implementing XYZ pattern from someone else, if you

Re: Reworking the control flow for my tactical role-playing game

2024-03-23 Thread Liam McGillivray via Digitalmars-d-learn
On Saturday, 23 March 2024 at 04:32:29 UTC, harakim wrote: This comment points to a symptom of the circular dependency: //Always set `destroy` to false when calling from the Unit destructor, to avoid an infinite loop. I was just doing some work on the AI system, and I had a segfault every

Unittests pass, and then an invalid memory operation happens after?

2024-03-27 Thread Liam McGillivray via Digitalmars-d-learn
In my current [game project](https://github.com/LiamM32/Open_Emblem), [something strange](https://github.com/LiamM32/Open_Emblem/issues/20) has happened as of a recent commit. When running `dub test`, all the unittests appear to pass, but then after the last unittest has concluded an "Invalid

Re: Reworking the control flow for my tactical role-playing game

2024-03-23 Thread Liam McGillivray via Digitalmars-d-learn
On Saturday, 23 March 2024 at 23:59:18 UTC, Liam McGillivray wrote: I replaced `destroy(unit)` with `map.deleteUnit(unit)`, and it solved the problem. Nevermind. It turns out this was because the call to the Unit destructor was missing in `Map.deleteUnit`. The segfault happens whenever a

Re: "Error: `TypeInfo` cannot be used with -betterC" on a CTFE function

2024-04-07 Thread Liam McGillivray via Digitalmars-d-learn
On Sunday, 7 April 2024 at 08:59:55 UTC, Richard (Rikki) Andrew Cattermole wrote: Unfortunately runtime and CTFE are the same target in the compiler. :-( Will this ever be changed? ```d template Foo(Args) { enum Foo = () { return Args.init; }(); } ```

Re: "Error: `TypeInfo` cannot be used with -betterC" on a CTFE function

2024-04-08 Thread Liam McGillivray via Digitalmars-d-learn
On Monday, 8 April 2024 at 08:12:22 UTC, Richard (Rikki) Andrew Cattermole wrote: ```d template Foo(Args) { enum Foo = () {     return Args.init; }(); } ``` Something like that should work instead. I'm sorry, but I can't comprehend any of your example. What would be fed into

Re: "Error: `TypeInfo` cannot be used with -betterC" on a CTFE function

2024-04-08 Thread Liam McGillivray via Digitalmars-d-learn
On Tuesday, 9 April 2024 at 00:02:02 UTC, Richard (Rikki) Andrew Cattermole wrote: ```d enum Value = (a, b) { return a + b; }(1, 2); ``` This alone should be a CTFE only function. But if we want template parameters, we'd need to wrap it with the template. ```d template Value(int a,

Re: "Error: `TypeInfo` cannot be used with -betterC" on a CTFE function

2024-04-09 Thread Liam McGillivray via Digitalmars-d-learn
On Sunday, 7 April 2024 at 08:59:55 UTC, Richard (Rikki) Andrew Cattermole wrote: Unfortunately runtime and CTFE are the same target in the compiler. So that function is being used for both, and hence uses GC (appending). Are you sure that string appending was really the problem that

Re: "Error: `TypeInfo` cannot be used with -betterC" on a CTFE function

2024-04-09 Thread Liam McGillivray via Digitalmars-d-learn
On Tuesday, 9 April 2024 at 23:50:36 UTC, Richard (Rikki) Andrew Cattermole wrote: The string mixin triggers CTFE, if ``EnumPrefixes`` wasn't templated, that would cause codegen and hence error. If you called it in a context that wasn't CTFE only, it would codegen even with template and would

Re: "Error: `TypeInfo` cannot be used with -betterC" on a CTFE function

2024-04-11 Thread Liam McGillivray via Digitalmars-d-learn
On Tuesday, 9 April 2024 at 12:45:55 UTC, Richard (Rikki) Andrew Cattermole wrote: On 09/04/2024 12:48 PM, Liam McGillivray wrote: I suppose this was a good new thing to learn, though I'm still quite far from being able to construct a function from another function using a template. I

Making one struct work in place of another for function calls.

2024-04-16 Thread Liam McGillivray via Digitalmars-d-learn
I have two structs that serve roughly the same purpose, and I would like one to be accepted when the other is declared as a function parameter. To better understand what I mean, take the following example, where I have a function, and two structs. ``` struct typeA { // Some member

Re: Making one struct work in place of another for function calls.

2024-04-16 Thread Liam McGillivray via Digitalmars-d-learn
On Wednesday, 17 April 2024 at 02:39:25 UTC, Paul Backus wrote: This is called [row polymorphism][1], and it does not exist in D. You could approximate it by making `someFunction` a template, and accepting any type `T` that has the necessary members instead of only accepting `typeB`. But

Doing a `static foreach` or `foreach` through enum members in a template or CTFE function, while disabling deprecation warnings

2024-04-19 Thread Liam McGillivray via Digitalmars-d-learn
I know that DStep generates CTFE functions to automatically make aliases for enum members so that the can be used without the enum name, as is done in C. DStep does it with a CTFE function, though it should also be possible with a mixin template. Here is my attempt so far, using a mixin

Re: Doing a `static foreach` or `foreach` through enum members in a template or CTFE function, while disabling deprecation warnings

2024-04-19 Thread Liam McGillivray via Digitalmars-d-learn
Well, someone on the Discord server has been helping me attempt this, but while I managed to get a solution that compiles without errors, I still get the deprecation warning. Here is what I ended up with: ``` template enumMixin(alias Enum) { static foreach(m; __traits(allMembers, Enum))

Re: Doing a `static foreach` or `foreach` through enum members in a template or CTFE function, while disabling deprecation warnings

2024-04-19 Thread Liam McGillivray via Digitalmars-d-learn
On Friday, 19 April 2024 at 22:24:17 UTC, Liam McGillivray wrote: ``` template enumMixin(alias Enum) { static foreach(m; __traits(allMembers, Enum)) static if (!__traits(isDeprecated, __traits(getMember, Enum, m))) { mixin("alias "~m~" = __traits(getMember, Enum, m);"); }

Re: "Error: `TypeInfo` cannot be used with -betterC" on a CTFE function

2024-04-14 Thread Liam McGillivray via Digitalmars-d-learn
On Friday, 12 April 2024 at 15:24:38 UTC, Steven Schveighoffer wrote: ```d void InitWindow(int width, int height, ref string title) { InitWindow(width, height, cast(const(char)*)title); } ``` This is invalid, a string may not be zero-terminated. You can't just cast. Well, it did work

Re: How can I put the current value of a variable into a delegate?

2024-05-08 Thread Liam McGillivray via Digitalmars-d-learn
On Monday, 6 May 2024 at 16:41:38 UTC, Steven Schveighoffer wrote: On Monday, 6 May 2024 at 06:29:49 UTC, Liam McGillivray wrote: Delegates can be a pain, as they often have results different from what one would intuitively expect. This can easily result in bugs. Here's a line that caused a

D doesn't have weak references. So how can I make a associative array of objects without preventing their destruction?

2024-05-08 Thread Liam McGillivray via Digitalmars-d-learn
A "weak reference" (in the sense that I'm referring to) is a feature in some programming languages for a reference to an object that doesn't prevent the GC from destroying that object. My current understanding is that D doesn't have weak references, though I've found some posts in this forum

Re: How can I put the current value of a variable into a delegate?

2024-05-08 Thread Liam McGillivray via Digitalmars-d-learn
On Wednesday, 8 May 2024 at 12:29:05 UTC, Rene Zwanenburg wrote: Interestingly enough C# used to have the same behaviour but MS decided to go for a breaking change in C# 5; now it behaves as most people expect. Wow! I wonder if D would be willing to allow such a breaking change with the

How can I put the current value of a variable into a delegate?

2024-05-06 Thread Liam McGillivray via Digitalmars-d-learn
Delegates can be a pain, as they often have results different from what one would intuitively expect. This can easily result in bugs. Here's a line that caused a bug that took me awhile to find: ``` foreach(card; unitCards) card.submitted = delegate() => selectUnit(card.unit); ``` Each

Re: Phobos function to remove all occurances from dynamic array?

2024-04-30 Thread Liam McGillivray via Digitalmars-d-learn
On Wednesday, 1 May 2024 at 01:24:55 UTC, Lance Bachmeier wrote: Does filter do what you need? https://dlang.org/phobos/std_algorithm_iteration.html#.filter It seems to do it with the following line: ``` allObjects = allObjects.filter!(element => element !is this).array; ``` So I've found